1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.googlecode.mgwt.collection.shared.java;
17
18 import java.util.ArrayList;
19 import java.util.Set;
20
21 import com.googlecode.mgwt.collection.shared.LightArray;
22
23
24
25
26
27
28
29
30 public class JavaLightArray<T> implements LightArray<T> {
31
32 private ArrayList<T> list;
33
34
35
36
37 public JavaLightArray() {
38 list = new ArrayList<T>();
39 }
40
41 @Override
42 public T shift() {
43 return list.remove(0);
44 }
45
46 @Override
47 public T get(int index) {
48
49 if (index < 0 || index >= list.size())
50 return null;
51 return list.get(index);
52 }
53
54 @Override
55 public void set(int index, T value) {
56
57 if (index < 0)
58 return;
59 if (index >= list.size()) {
60 for (int i = list.size(); i < index; i++) {
61 list.add(null);
62 }
63 list.add(value);
64 } else {
65 list.set(index, value);
66 }
67
68 }
69
70 @Override
71 public int length() {
72 return list.size();
73 }
74
75 @Override
76 public void unshift(T value) {
77 list.add(0, value);
78
79 }
80
81 @Override
82 public void push(T value) {
83 list.add(value);
84
85 }
86
87
88
89
90
91
92
93
94 public static <T> JavaLightArray<T> fromSet(Set<T> set) {
95 JavaLightArray<T> array = new JavaLightArray<T>();
96 for (T t : set) {
97 array.push(t);
98 }
99 return array;
100 }
101
102 }