001 /*
002 * Copyright 2010-2012 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 0.9.4
045 */
046 public final class CollectionUtils {
047 @SuppressWarnings({"rawtypes", "unchecked"})
048 public static <K, V> Map newMap(Object... keysAndValues) {
049 if (keysAndValues == null) {
050 return Collections.emptyMap();
051 }
052 if (keysAndValues.length % 2 == 1) {
053 throw new IllegalArgumentException("Must have an even number of keys and values");
054 }
055
056 Map<K, V> map = new HashMap<K, V>();
057 for (int i = 0; i < keysAndValues.length; i += 2) {
058 map.put((K) keysAndValues[i], (V) keysAndValues[i + 1]);
059 }
060 return map;
061 }
062
063 public static <T> Set<T> newSet(T... values) {
064 if (values == null) {
065 return Collections.emptySet();
066 }
067
068 return new HashSet<T>(Arrays.asList(values));
069 }
070
071 public static <T> List<T> newList(T... values) {
072 if (values == null) {
073 return Collections.emptyList();
074 }
075
076 return new ArrayList<T>(Arrays.asList(values));
077 }
078
079 public static <K, V> MapBuilder<K, V> map() {
080 return map(new LinkedHashMap<K, V>());
081 }
082
083 public static <K, V> MapBuilder<K, V> map(Map<K, V> delegate) {
084 return new MapBuilder<K, V>(delegate);
085 }
086
087 public static <E> ListBuilder<E> list() {
088 return list(new ArrayList<E>());
089 }
090
091 public static <E> ListBuilder<E> list(List<E> delegate) {
092 return new ListBuilder<E>(delegate);
093 }
094
095 public static <E> SetBuilder<E> set() {
096 return set(new HashSet<E>());
097 }
098
099 public static <E> SetBuilder<E> set(Set<E> delegate) {
100 return new SetBuilder<E>(delegate);
101 }
102
103 public static class MapBuilder<K, V> implements Map<K, V> {
104 private final Map<K, V> delegate;
105
106 public MapBuilder(Map<K, V> delegate) {
107 this.delegate = delegate;
108 }
109
110 public MapBuilder<K, V> e(K k, V v) {
111 delegate.put(k, v);
112 return this;
113 }
114
115 public int size() {
116 return delegate.size();
117 }
118
119 public boolean isEmpty() {
120 return delegate.isEmpty();
121 }
122
123 public boolean containsKey(Object o) {
124 return delegate.containsKey(o);
125 }
126
127 public boolean containsValue(Object o) {
128 return delegate.containsValue(o);
129 }
130
131 public V get(Object o) {
132 return delegate.get(o);
133 }
134
135 public V put(K k, V v) {
136 return delegate.put(k, v);
137 }
138
139 public V remove(Object o) {
140 return delegate.remove(o);
141 }
142
143 public void putAll(Map<? extends K, ? extends V> map) {
144 delegate.putAll(map);
145 }
146
147 public void clear() {
148 delegate.clear();
149 }
150
151 public Set<K> keySet() {
152 return delegate.keySet();
153 }
154
155 public Collection<V> values() {
156 return delegate.values();
157 }
158
159 public Set<Entry<K, V>> entrySet() {
160 return delegate.entrySet();
161 }
162
163 @Override
164 public boolean equals(Object o) {
165 return delegate.equals(o);
166 }
167
168 @Override
169 public int hashCode() {
170 return delegate.hashCode();
171 }
172 }
173
174 public static class ListBuilder<E> implements List<E> {
175 private final List<E> delegate;
176
177 public ListBuilder(List<E> delegate) {
178 this.delegate = delegate;
179 }
180
181 public ListBuilder<E> e(E e) {
182 delegate.add(e);
183 return this;
184 }
185
186 public int size() {
187 return delegate.size();
188 }
189
190 public boolean isEmpty() {
191 return delegate.isEmpty();
192 }
193
194 public boolean contains(Object o) {
195 return delegate.contains(o);
196 }
197
198 public Iterator<E> iterator() {
199 return delegate.iterator();
200 }
201
202 public Object[] toArray() {
203 return delegate.toArray();
204 }
205
206 public <T> T[] toArray(T[] ts) {
207 return delegate.toArray(ts);
208 }
209
210 public boolean add(E e) {
211 return delegate.add(e);
212 }
213
214 public boolean remove(Object o) {
215 return delegate.remove(o);
216 }
217
218 public boolean containsAll(Collection<?> objects) {
219 return delegate.containsAll(objects);
220 }
221
222 public boolean addAll(Collection<? extends E> es) {
223 return delegate.addAll(es);
224 }
225
226 public boolean addAll(int i, Collection<? extends E> es) {
227 return delegate.addAll(i, es);
228 }
229
230 public boolean removeAll(Collection<?> objects) {
231 return delegate.removeAll(objects);
232 }
233
234 public boolean retainAll(Collection<?> objects) {
235 return delegate.retainAll(objects);
236 }
237
238 public void clear() {
239 delegate.clear();
240 }
241
242 @Override
243 public boolean equals(Object o) {
244 return delegate.equals(o);
245 }
246
247 @Override
248 public int hashCode() {
249 return delegate.hashCode();
250 }
251
252 public E get(int i) {
253 return delegate.get(i);
254 }
255
256 public E set(int i, E e) {
257 return delegate.set(i, e);
258 }
259
260 public void add(int i, E e) {
261 delegate.add(i, e);
262 }
263
264 public E remove(int i) {
265 return delegate.remove(i);
266 }
267
268 public int indexOf(Object o) {
269 return delegate.indexOf(o);
270 }
271
272 public int lastIndexOf(Object o) {
273 return delegate.lastIndexOf(o);
274 }
275
276 public ListIterator<E> listIterator() {
277 return delegate.listIterator();
278 }
279
280 public ListIterator<E> listIterator(int i) {
281 return delegate.listIterator(i);
282 }
283
284 public List<E> subList(int i, int i1) {
285 return delegate.subList(i, i1);
286 }
287 }
288
289 public static class SetBuilder<E> implements Set<E> {
290 private final Set<E> delegate;
291
292 public SetBuilder(Set<E> delegate) {
293 this.delegate = delegate;
294 }
295
296 public SetBuilder<E> e(E e) {
297 delegate.add(e);
298 return this;
299 }
300
301 public int size() {
302 return delegate.size();
303 }
304
305 public boolean isEmpty() {
306 return delegate.isEmpty();
307 }
308
309 public boolean contains(Object o) {
310 return delegate.contains(o);
311 }
312
313 public Iterator<E> iterator() {
314 return delegate.iterator();
315 }
316
317 public Object[] toArray() {
318 return delegate.toArray();
319 }
320
321 public <T> T[] toArray(T[] ts) {
322 return delegate.toArray(ts);
323 }
324
325 public boolean add(E e) {
326 return delegate.add(e);
327 }
328
329 public boolean remove(Object o) {
330 return delegate.remove(o);
331 }
332
333 public boolean containsAll(Collection<?> objects) {
334 return delegate.containsAll(objects);
335 }
336
337 public boolean addAll(Collection<? extends E> es) {
338 return delegate.addAll(es);
339 }
340
341 public boolean retainAll(Collection<?> objects) {
342 return delegate.retainAll(objects);
343 }
344
345 public boolean removeAll(Collection<?> objects) {
346 return delegate.removeAll(objects);
347 }
348
349 public void clear() {
350 delegate.clear();
351 }
352
353 @Override
354 public boolean equals(Object o) {
355 return delegate.equals(o);
356 }
357
358 @Override
359 public int hashCode() {
360 return delegate.hashCode();
361 }
362 }
363 }
|