1 | package com.framsticks.params; |
---|
2 | |
---|
3 | import com.framsticks.util.lang.Numbers; |
---|
4 | |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.Collection; |
---|
7 | import java.util.LinkedList; |
---|
8 | import java.util.List; |
---|
9 | |
---|
10 | /** |
---|
11 | * @author Piotr Sniegowski |
---|
12 | */ |
---|
13 | public class ArrayListAccess extends ListAccess { |
---|
14 | |
---|
15 | List<Object> list; |
---|
16 | |
---|
17 | public ArrayListAccess(AccessInterface elementAccess) { |
---|
18 | super(elementAccess); |
---|
19 | } |
---|
20 | |
---|
21 | @Override |
---|
22 | public ArrayListAccess cloneAccess() { |
---|
23 | return new ArrayListAccess(elementAccess.cloneAccess()); |
---|
24 | } |
---|
25 | |
---|
26 | @Override |
---|
27 | public List<?> createAccessee() { |
---|
28 | return new ArrayList<Object>(); |
---|
29 | } |
---|
30 | |
---|
31 | @Override |
---|
32 | public Param getParam(int i) { |
---|
33 | return Param.build().id(Integer.toString(i)).name(elementAccess.getId()).type("o " + elementAccess.getId()).finish(); |
---|
34 | } |
---|
35 | |
---|
36 | @Override |
---|
37 | public Param getParam(String id) { |
---|
38 | Integer i = Numbers.parse(id, Integer.class); |
---|
39 | return (i == null ? null : getParam(i)); |
---|
40 | } |
---|
41 | |
---|
42 | @Override |
---|
43 | public String getId() { |
---|
44 | return "l " + elementAccess.getId(); |
---|
45 | } |
---|
46 | |
---|
47 | @Override |
---|
48 | public int getParamCount() { |
---|
49 | return list.size(); |
---|
50 | } |
---|
51 | |
---|
52 | @Override |
---|
53 | public <T> T get(int i, Class<T> type) { |
---|
54 | if (i < list.size()) { |
---|
55 | return type.cast(list.get(i)); |
---|
56 | } |
---|
57 | return null; |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public <T> T get(String id, Class<T> type) { |
---|
62 | return get(Integer.parseInt(id), type); |
---|
63 | } |
---|
64 | |
---|
65 | @Override |
---|
66 | public <T> T get(ValueParam param, Class<T> type) { |
---|
67 | return get(param.getId(), type); |
---|
68 | } |
---|
69 | |
---|
70 | @Override |
---|
71 | public <T> int set(int i, T value) { |
---|
72 | while (i >= list.size()) { |
---|
73 | list.add(null); |
---|
74 | } |
---|
75 | list.set(i, value); |
---|
76 | return 0; |
---|
77 | } |
---|
78 | |
---|
79 | @Override |
---|
80 | public <T> int set(String id, T value) { |
---|
81 | return set(Integer.parseInt(id), value); |
---|
82 | } |
---|
83 | |
---|
84 | @Override |
---|
85 | public <T> int set(ValueParam param, T value) { |
---|
86 | return set(param.getId(), value); |
---|
87 | } |
---|
88 | |
---|
89 | @Override |
---|
90 | public void clearValues() { |
---|
91 | list.clear(); |
---|
92 | } |
---|
93 | |
---|
94 | /** covariant */ |
---|
95 | @Override |
---|
96 | public List<Object> getSelected() { |
---|
97 | return list; |
---|
98 | } |
---|
99 | |
---|
100 | @SuppressWarnings("unchecked") |
---|
101 | @Override |
---|
102 | public ArrayListAccess select(Object object) { |
---|
103 | list = (List<Object>) object; |
---|
104 | return this; |
---|
105 | } |
---|
106 | |
---|
107 | @Override |
---|
108 | public String computeIdentifierFor(Object selected) { |
---|
109 | return Integer.toString(list.size()); |
---|
110 | } |
---|
111 | |
---|
112 | @Override |
---|
113 | public Collection<Param> getParams() { |
---|
114 | List<Param> result = new LinkedList<Param>(); |
---|
115 | if (list == null) { |
---|
116 | return result; |
---|
117 | } |
---|
118 | for (int i = 0; i < list.size(); ++i) { |
---|
119 | result.add(getParam(i)); |
---|
120 | } |
---|
121 | return result; |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | } |
---|