source: java/main/src/main/java/com/framsticks/params/ArrayListAccess.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1package com.framsticks.params;
2
3import com.framsticks.params.types.ArrayListParam;
4import com.framsticks.util.UnimplementedException;
5import com.framsticks.util.lang.Numbers;
6
7import java.util.ArrayList;
8import java.util.Iterator;
9import java.util.List;
10import java.util.ListIterator;
11import static com.framsticks.params.SetStateFlags.*;
12
13/**
14 * @author Piotr Sniegowski
15 */
16public class ArrayListAccess extends ListAccess {
17
18        List<Object> list;
19
20
21        public ArrayListAccess(Access elementAccess) {
22                super(elementAccess);
23        }
24
25        @Override
26        public ArrayListAccess cloneAccess() {
27                return new ArrayListAccess(elementAccess.cloneAccess());
28        }
29
30        @Override
31        public List<?> createAccessee() {
32                return new ArrayList<Object>();
33        }
34
35        @Override
36        public CompositeParam getParam(int i) {
37                if ((i < 0) ||  (i >= list.size())) {
38                        return null;
39                }
40                return paramBuilder.id(Integer.toString(i)).finish(CompositeParam.class);
41        }
42
43        @Override
44        public CompositeParam getParam(String id) {
45                Integer i = Numbers.parse(id, Integer.class);
46                return (i == null ? null : getParam(i));
47        }
48
49        @Override
50        public String getTypeId() {
51                return "l " + elementAccess.getTypeId();
52        }
53
54        @Override
55        public int getParamCount() {
56                return list.size();
57        }
58
59        @Override
60        public <T> T get(int i, Class<T> type) {
61                if ((i < 0) ||  (i >= list.size())) {
62                        return null;
63                }
64                return type.cast(list.get(i));
65        }
66
67        @Override
68        public <T> T get(String id, Class<T> type) {
69                return get(Integer.parseInt(id), type);
70        }
71
72        @Override
73        public <T> T get(ValueParam param, Class<T> type) {
74                return get(param.getId(), type);
75        }
76
77        @Override
78        public <T> int set(int i, T value) {
79                if (value != null) {
80                        while (i >= list.size()) {
81                                list.add(null);
82                        }
83                        list.set(i, value);
84                        return 0;
85                }
86                if (i >= list.size()) {
87                        return PSET_HITMAX;
88                }
89                list.remove(i);
90                // for (int j = i + 1; j < list.size(); ++j) {
91                //      if (list.get(j) != null) {
92                //              list.set(i, null);
93                //              return 0;
94                //      }
95                // }
96                // while (list.size() > i) {
97                //      list.remove(i);
98                // }
99                return 0;
100        }
101
102        @Override
103        public <T> int set(String id, T value) {
104                return set(Integer.parseInt(id), value);
105        }
106
107        @Override
108        public <T> int set(ValueParam param, T value) {
109                return set(param.getId(), value);
110        }
111
112        @Override
113        public void clearValues() {
114                list.clear();
115        }
116
117        /** covariant */
118        @Override
119        public List<Object> getSelected() {
120                return list;
121        }
122
123        @SuppressWarnings("unchecked")
124        @Override
125        public ArrayListAccess select(Object object) {
126                list = ParamsUtil.selectObjectForAccess(this, object, List.class);
127                return this;
128        }
129
130
131        @Override
132        public Iterable<Param> getParams() {
133                return new Iterable<Param>() {
134
135                        @Override
136                        public Iterator<Param> iterator() {
137                                return new Iterator<Param>() {
138
139                                        protected final ListIterator<Object> internal = list.listIterator();
140
141                                        @Override
142                                        public boolean hasNext() {
143                                                return internal.hasNext();
144                                        }
145
146                                        @Override
147                                        public Param next() {
148                                                Param param = paramBuilder.id(Integer.toString(internal.nextIndex())).finish(CompositeParam.class);
149                                                internal.next();
150                                                return param;
151                                        }
152
153                                        @Override
154                                        public void remove() {
155                                                throw new UnimplementedException().msg("remove element from list").arg("list", ArrayListAccess.this);
156
157                                        }
158                                };
159                        }
160                };
161        }
162
163        @Override
164        public int getCompositeParamCount() {
165                return list.size();
166        }
167
168        @Override
169        public CompositeParam getCompositeParam(int number) {
170                return getParam(number);
171        }
172
173        @Override
174        public ParamBuilder buildParam(ParamBuilder builder) {
175                return builder.name(containedTypeName + " list").type(ArrayListParam.class).containedTypeName(containedTypeName);
176        }
177
178}
Note: See TracBrowser for help on using the repository browser.