Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CollectionUtils |
|
| 2.090909090909091;2.091 | ||||
CollectionUtils$1 |
|
| 2.090909090909091;2.091 | ||||
CollectionUtils$IterableEnumeration |
|
| 2.090909090909091;2.091 | ||||
CollectionUtils$IterableEnumeration$1 |
|
| 2.090909090909091;2.091 | ||||
CollectionUtils$IterableIterator |
|
| 2.090909090909091;2.091 |
1 | /* | |
2 | * Copyright 2007-2010 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.core.util; | |
17 | ||
18 | import java.util.Collection; | |
19 | import java.util.Enumeration; | |
20 | import java.util.Iterator; | |
21 | ||
22 | /** | |
23 | * Yet another utility class to help work with Collections. This class contains helper methods not found in any of the Collection | |
24 | * utilities rice currently uses. | |
25 | * | |
26 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
27 | * | |
28 | */ | |
29 | public final class CollectionUtils { | |
30 | ||
31 | /** do not call. */ | |
32 | 0 | private CollectionUtils() { |
33 | 0 | throw new UnsupportedOperationException("do not call"); |
34 | } | |
35 | ||
36 | /** | |
37 | * Performs a "brute force" comparison of collections by testing whether the collections contain each other. | |
38 | * This circumvents any particular uniqueness or ordering constraints on the collections | |
39 | * (for instance, lists that are unordered but contain the same elements, where a hashset would not suffice | |
40 | * for comparison purposes because it enforces element uniqueness) | |
41 | */ | |
42 | public static boolean collectionsEquivalent(Collection<?> a, Collection<?> b) { | |
43 | 0 | if (a == null && b == null) { |
44 | 0 | return true; |
45 | } | |
46 | 0 | if (a == null ^ b == null) { |
47 | 0 | return false; |
48 | } | |
49 | 0 | return a.containsAll(b) && b.containsAll(a); |
50 | } | |
51 | ||
52 | /** | |
53 | * Creates an Iterable view of a Iterator. This allows Iterators to be used in a foreach loop. | |
54 | * | |
55 | * <pre> | |
56 | * {@code | |
57 | * Iterator<String> i ... | |
58 | * | |
59 | * for(String s : CollectionUtils.toIterable(i)) [ | |
60 | * System.out.println("i love for each " + s); | |
61 | * } | |
62 | * } | |
63 | * </pre> | |
64 | * | |
65 | * @param <T> the type of the Iterable | |
66 | * @param i the iterator to wrap | |
67 | * @return the iterable | |
68 | */ | |
69 | public static <T> Iterable<T> toIterable(Iterator<T> i) { | |
70 | 0 | return new IterableIterator<T>(i); |
71 | } | |
72 | ||
73 | /** | |
74 | * Creates an Iterable view of a Enumeration. This allows Enumerations to be used in a foreach loop. | |
75 | * | |
76 | * <pre> | |
77 | * {@code | |
78 | * Enumeration<String> e ... | |
79 | * | |
80 | * for(String s : CollectionUtils.toIterable(e)) [ | |
81 | * System.out.println("i love for each " + s); | |
82 | * } | |
83 | * } | |
84 | * </pre> | |
85 | * | |
86 | * @param <T> the type of the Iterable | |
87 | * @param e the enumeration to wrap | |
88 | * @return the iterable | |
89 | */ | |
90 | public static <T> Iterable<T> toIterable(Enumeration<T> e) { | |
91 | 0 | return new IterableEnumeration<T>(e); |
92 | } | |
93 | ||
94 | /** | |
95 | * An adapter from an Enumeration to Iterable. | |
96 | * | |
97 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
98 | * | |
99 | * @param <T> the type of Enumeration and Iterable | |
100 | */ | |
101 | 0 | private static class IterableEnumeration<T> implements Iterable<T> { |
102 | private final Enumeration<T> e; | |
103 | 0 | private IterableEnumeration(final Enumeration<T> e) { |
104 | 0 | if (e == null) { |
105 | 0 | throw new IllegalArgumentException("the enumeration is null"); |
106 | } | |
107 | ||
108 | 0 | this.e = e; |
109 | 0 | } |
110 | ||
111 | @Override | |
112 | public Iterator<T> iterator() { | |
113 | 0 | return new Iterator<T>() { |
114 | @Override | |
115 | public boolean hasNext() { | |
116 | 0 | return e.hasMoreElements(); |
117 | } | |
118 | @Override | |
119 | public T next() { | |
120 | 0 | return e.nextElement(); |
121 | } | |
122 | @Override | |
123 | public void remove() { | |
124 | 0 | throw new UnsupportedOperationException("this iterator does not support remove"); |
125 | } | |
126 | }; | |
127 | } | |
128 | } | |
129 | ||
130 | /** | |
131 | * An adapter from an Iterator to Iterable. | |
132 | * | |
133 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
134 | * | |
135 | * @param <T> the type of Iterator and Iterable | |
136 | */ | |
137 | 0 | private static class IterableIterator<T> implements Iterable<T> { |
138 | private final Iterator<T> i; | |
139 | 0 | private IterableIterator(final Iterator<T> i) { |
140 | 0 | if (i == null) { |
141 | 0 | throw new IllegalArgumentException("the iterator is null"); |
142 | } | |
143 | ||
144 | 0 | this.i = i; |
145 | 0 | } |
146 | ||
147 | @Override | |
148 | public Iterator<T> iterator() { | |
149 | 0 | return i; |
150 | } | |
151 | } | |
152 | } |