1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.lang.reflect.Array;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.NoSuchElementException;
27
28
29
30
31
32
33
34 public class ConcatenatedLists<T> implements Collection<T> {
35 protected final ArrayList<List<T>> components = new ArrayList<List<T>>();
36 protected int size = 0;
37
38 public void addAllSublists(List<? extends List<T>> items) {
39 for (List<T> list : items) {
40 addSublist(list);
41 }
42 }
43
44 public void addSublist(List<T> items) {
45 if (!items.isEmpty()) {
46 this.components.add(items);
47 this.size += items.size();
48 }
49 }
50
51 @Override
52 public int size() {
53 return this.size;
54 }
55
56 @Override
57 public boolean isEmpty() {
58 return this.size == 0;
59 }
60
61 @Override
62 public boolean contains(Object o) {
63 for (List<T> component : this.components) {
64 if (component.contains(o)) return true;
65 }
66 return false;
67 }
68
69 @Override
70 public boolean containsAll(Collection<?> c) {
71 for (Object o : c) {
72 if (!contains(o)) return false;
73 }
74 return true;
75 }
76
77 @Override
78 public Object[] toArray() {
79 return toArray((Object[])Array.newInstance(Object.class, this.size));
80 }
81
82 @Override
83 @SuppressWarnings("unchecked")
84 public <U> U[] toArray(U[] a) {
85 U[] result = (a.length == this.size()) ? a
86 : (U[])Array.newInstance(a.getClass().getComponentType(), this.size);
87 int i = 0;
88 for (List<T> component : this.components) {
89 for (T t : component) {
90 result[i] = (U)t;
91 ++i;
92 }
93 }
94 return result;
95 }
96
97 @Override
98 public boolean add(T e) {
99 throw new UnsupportedOperationException();
100 }
101
102 @Override
103 public boolean remove(Object o) {
104 throw new UnsupportedOperationException();
105 }
106
107 @Override
108 public boolean addAll(Collection<? extends T> c) {
109 throw new UnsupportedOperationException();
110 }
111
112 @Override
113 public boolean removeAll(Collection<?> c) {
114 throw new UnsupportedOperationException();
115 }
116
117 @Override
118 public boolean retainAll(Collection<?> c) {
119 throw new UnsupportedOperationException();
120 }
121
122 @Override
123 public void clear() {
124 throw new UnsupportedOperationException();
125 }
126
127 @Override
128 public java.util.Iterator<T> iterator() {
129 return new Iterator();
130 }
131
132 @edu.umd.cs.findbugs.annotations.SuppressWarnings(
133 value="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD",
134 justification="nextWasCalled is using by StripeStoreFileManager")
135 public class Iterator implements java.util.Iterator<T> {
136 protected int currentComponent = 0;
137 protected int indexWithinComponent = -1;
138 protected boolean nextWasCalled = false;
139
140 @Override
141 public boolean hasNext() {
142 return (currentComponent + 1) < components.size()
143 || ((currentComponent + 1) == components.size()
144 && ((indexWithinComponent + 1) < components.get(currentComponent).size()));
145 }
146
147 @Override
148 public T next() {
149 if (!components.isEmpty()) {
150 this.nextWasCalled = true;
151 List<T> src = components.get(currentComponent);
152 if (++indexWithinComponent < src.size()) return src.get(indexWithinComponent);
153 if (++currentComponent < components.size()) {
154 indexWithinComponent = 0;
155 src = components.get(currentComponent);
156 assert src.size() > 0;
157 return src.get(indexWithinComponent);
158 }
159 }
160 this.nextWasCalled = false;
161 throw new NoSuchElementException();
162 }
163
164 @Override
165 public void remove() {
166 throw new UnsupportedOperationException();
167 }
168 }
169 }