001 /*
002 * Copyright 2008-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package griffon.util;
017
018 import java.util.*;
019
020 /**
021 * <p>Utility class that simplifies creating collections in Java.</p>
022 * <p><strong>Creating Maps</strong><br/>
023 * <pre>
024 * Map<String, Object> m = map()
025 * .e("foo", foo)
026 * .e("bar", bar);
027 * </pre></p>
028 *
029 * <p><strong>Creating Lists</strong><br/>
030 * <pre>
031 * List<String> l = list()
032 * .e("foo")
033 * .e("bar");
034 * </pre></p>
035 *
036 * <p><strong>Creating Maps</strong><br/>
037 * <pre>
038 * Set<String> s = set()
039 * .e("foo")
040 * .e("bar");
041 * </pre></p>
042 *
043 * @author Andres Almiray
044 * @since 2.0.0
045 */
046 public final class CollectionUtils {
047 public static <T> List<T> reverse(List<T> input) {
048 List<T> output = new ArrayList<>(input);
049 Collections.reverse(output);
050 return output;
051 }
052
053 public static <T> List<T> reverse(Collection<T> input) {
054 List<T> output = new ArrayList<>(input);
055 Collections.reverse(output);
056 return output;
057 }
058
059 @SuppressWarnings({"rawtypes", "unchecked"})
060 public static <K, V> Map newMap(Object... keysAndValues) {
061 if (keysAndValues == null) {
062 return Collections.emptyMap();
063 }
064 if (keysAndValues.length % 2 == 1) {
065 throw new IllegalArgumentException("Must have an even number of keys and values");
066 }
067
068 Map<K, V> map = new HashMap<>();
069 for (int i = 0; i < keysAndValues.length; i += 2) {
070 map.put((K) keysAndValues[i], (V) keysAndValues[i + 1]);
071 }
072 return map;
073 }
074
075 @SafeVarargs
076 public static <T> Set<T> newSet(T... values) {
077 if (values == null) {
078 return Collections.emptySet();
079 }
080
081 return new HashSet<>(Arrays.asList(values));
082 }
083
084 @SafeVarargs
085 public static <T> List<T> newList(T... values) {
086 if (values == null) {
087 return Collections.emptyList();
088 }
089
090 return new ArrayList<>(Arrays.asList(values));
091 }
092
093 public static <K, V> MapBuilder<K, V> map() {
094 return map(new LinkedHashMap<K, V>());
095 }
096
097 public static <K, V> MapBuilder<K, V> map(Map<K, V> delegate) {
098 return new MapBuilder<>(delegate);
099 }
100
101 public static <E> ListBuilder<E> list() {
102 return list(new ArrayList<E>());
103 }
104
105 public static <E> ListBuilder<E> list(List<E> delegate) {
106 return new ListBuilder<>(delegate);
107 }
108
109 public static <E> SetBuilder<E> set() {
110 return set(new HashSet<E>());
111 }
112
113 public static <E> SetBuilder<E> set(Set<E> delegate) {
114 return new SetBuilder<>(delegate);
115 }
116
117 public static class MapBuilder<K, V> implements Map<K, V> {
118 private final Map<K, V> delegate;
119
120 public MapBuilder(Map<K, V> delegate) {
121 this.delegate = delegate;
122 }
123
124 public MapBuilder<K, V> e(K k, V v) {
125 delegate.put(k, v);
126 return this;
127 }
128
129 public int size() {
130 return delegate.size();
131 }
132
133 public boolean isEmpty() {
134 return delegate.isEmpty();
135 }
136
137 public boolean containsKey(Object o) {
138 return delegate.containsKey(o);
139 }
140
141 public boolean containsValue(Object o) {
142 return delegate.containsValue(o);
143 }
144
145 public V get(Object o) {
146 return delegate.get(o);
147 }
148
149 public V put(K k, V v) {
150 return delegate.put(k, v);
151 }
152
153 public V remove(Object o) {
154 return delegate.remove(o);
155 }
156
157 public void putAll(Map<? extends K, ? extends V> map) {
158 delegate.putAll(map);
159 }
160
161 public void clear() {
162 delegate.clear();
163 }
164
165 public Set<K> keySet() {
166 return delegate.keySet();
167 }
168
169 public Collection<V> values() {
170 return delegate.values();
171 }
172
173 public Set<Entry<K, V>> entrySet() {
174 return delegate.entrySet();
175 }
176
177 @Override
178 public boolean equals(Object o) {
179 return delegate.equals(o);
180 }
181
182 @Override
183 public int hashCode() {
184 return delegate.hashCode();
185 }
186 }
187
188 public static class ListBuilder<E> implements List<E> {
189 private final List<E> delegate;
190
191 public ListBuilder(List<E> delegate) {
192 this.delegate = delegate;
193 }
194
195 public ListBuilder<E> e(E e) {
196 delegate.add(e);
197 return this;
198 }
199
200 public int size() {
201 return delegate.size();
202 }
203
204 public boolean isEmpty() {
205 return delegate.isEmpty();
206 }
207
208 public boolean contains(Object o) {
209 return delegate.contains(o);
210 }
211
212 public Iterator<E> iterator() {
213 return delegate.iterator();
214 }
215
216 public Object[] toArray() {
217 return delegate.toArray();
218 }
219
220 public <T> T[] toArray(T[] ts) {
221 return delegate.toArray(ts);
222 }
223
224 public boolean add(E e) {
225 return delegate.add(e);
226 }
227
228 public boolean remove(Object o) {
229 return delegate.remove(o);
230 }
231
232 public boolean containsAll(Collection<?> objects) {
233 return delegate.containsAll(objects);
234 }
235
236 public boolean addAll(Collection<? extends E> es) {
237 return delegate.addAll(es);
238 }
239
240 public boolean addAll(int i, Collection<? extends E> es) {
241 return delegate.addAll(i, es);
242 }
243
244 public boolean removeAll(Collection<?> objects) {
245 return delegate.removeAll(objects);
246 }
247
248 public boolean retainAll(Collection<?> objects) {
249 return delegate.retainAll(objects);
250 }
251
252 public void clear() {
253 delegate.clear();
254 }
255
256 @Override
257 public boolean equals(Object o) {
258 return delegate.equals(o);
259 }
260
261 @Override
262 public int hashCode() {
263 return delegate.hashCode();
264 }
265
266 public E get(int i) {
267 return delegate.get(i);
268 }
269
270 public E set(int i, E e) {
271 return delegate.set(i, e);
272 }
273
274 public void add(int i, E e) {
275 delegate.add(i, e);
276 }
277
278 public E remove(int i) {
279 return delegate.remove(i);
280 }
281
282 public int indexOf(Object o) {
283 return delegate.indexOf(o);
284 }
285
286 public int lastIndexOf(Object o) {
287 return delegate.lastIndexOf(o);
288 }
289
290 public ListIterator<E> listIterator() {
291 return delegate.listIterator();
292 }
293
294 public ListIterator<E> listIterator(int i) {
295 return delegate.listIterator(i);
296 }
297
298 public List<E> subList(int i, int i1) {
299 return delegate.subList(i, i1);
300 }
301 }
302
303 public static class SetBuilder<E> implements Set<E> {
304 private final Set<E> delegate;
305
306 public SetBuilder(Set<E> delegate) {
307 this.delegate = delegate;
308 }
309
310 public SetBuilder<E> e(E e) {
311 delegate.add(e);
312 return this;
313 }
314
315 public int size() {
316 return delegate.size();
317 }
318
319 public boolean isEmpty() {
320 return delegate.isEmpty();
321 }
322
323 public boolean contains(Object o) {
324 return delegate.contains(o);
325 }
326
327 public Iterator<E> iterator() {
328 return delegate.iterator();
329 }
330
331 public Object[] toArray() {
332 return delegate.toArray();
333 }
334
335 public <T> T[] toArray(T[] ts) {
336 return delegate.toArray(ts);
337 }
338
339 public boolean add(E e) {
340 return delegate.add(e);
341 }
342
343 public boolean remove(Object o) {
344 return delegate.remove(o);
345 }
346
347 public boolean containsAll(Collection<?> objects) {
348 return delegate.containsAll(objects);
349 }
350
351 public boolean addAll(Collection<? extends E> es) {
352 return delegate.addAll(es);
353 }
354
355 public boolean retainAll(Collection<?> objects) {
356 return delegate.retainAll(objects);
357 }
358
359 public boolean removeAll(Collection<?> objects) {
360 return delegate.removeAll(objects);
361 }
362
363 public void clear() {
364 delegate.clear();
365 }
366
367 @Override
368 public boolean equals(Object o) {
369 return delegate.equals(o);
370 }
371
372 @Override
373 public int hashCode() {
374 return delegate.hashCode();
375 }
376 }
377 }
|