| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CollectionUtils |
|
| 1.6153846153846154;1.615 |
| 1 | package org.jtheque.utils.collections; | |
| 2 | ||
| 3 | /* | |
| 4 | * This file is part of JTheque. | |
| 5 | * | |
| 6 | * JTheque is free software: you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation, either version 3 of the License. | |
| 9 | * | |
| 10 | * JTheque is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | * GNU General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU General Public License | |
| 16 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | */ | |
| 18 | ||
| 19 | import java.util.ArrayList; | |
| 20 | import java.util.Collection; | |
| 21 | import java.util.Collections; | |
| 22 | import java.util.Comparator; | |
| 23 | import java.util.Enumeration; | |
| 24 | import java.util.Iterator; | |
| 25 | import java.util.List; | |
| 26 | import java.util.ListIterator; | |
| 27 | import java.util.Map; | |
| 28 | import java.util.Map.Entry; | |
| 29 | ||
| 30 | /** | |
| 31 | * Provide some utilities operations on collections. | |
| 32 | * | |
| 33 | * @author Baptiste Wicht | |
| 34 | */ | |
| 35 | public final class CollectionUtils { | |
| 36 | 2 | private static final Object EMPTY_LIST = Collections.unmodifiableList(new ArrayList<Object>(0)); |
| 37 | ||
| 38 | /** | |
| 39 | * Construct a new CollectionUtils. This constructor is private because all the methods are | |
| 40 | * static. | |
| 41 | */ | |
| 42 | private CollectionUtils() { | |
| 43 | 0 | super(); |
| 44 | 0 | } |
| 45 | ||
| 46 | /** | |
| 47 | * Perform a closure on all the objects of the collection. | |
| 48 | * | |
| 49 | * @param collection The collection of the objects. | |
| 50 | * @param closure The closure. | |
| 51 | */ | |
| 52 | public static <T> void forAllDo(Iterable<T> collection, Closure<T> closure) { | |
| 53 | 0 | for (T object : collection) { |
| 54 | 0 | closure.execute(object); |
| 55 | } | |
| 56 | 0 | } |
| 57 | ||
| 58 | /** | |
| 59 | * Return a copy of the list. | |
| 60 | * | |
| 61 | * @param list The List to copy. | |
| 62 | * @param <T> The type contained in the list. | |
| 63 | * @return A copy of the list. | |
| 64 | */ | |
| 65 | public static <T> List<T> copyOf(Collection<T> list) { | |
| 66 | 2 | return new ArrayList<T>(list); |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * Return a collection expanded. | |
| 71 | * | |
| 72 | * @param <T> The type of object in the collection. | |
| 73 | * @param collection The collection to expand. | |
| 74 | * @param expander The expander object. | |
| 75 | * @return The expanded collection. | |
| 76 | */ | |
| 77 | public static <S, T> Collection<T> expand(Collection<S> collection, Expander<S, T> expander) { | |
| 78 | 0 | Collection<T> expanded = new ArrayList<T>(collection.size()); |
| 79 | ||
| 80 | 0 | for (S o : collection) { |
| 81 | 0 | expanded.add(expander.expand(o)); |
| 82 | } | |
| 83 | ||
| 84 | 0 | return expanded; |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * Filter a collection with a filter. | |
| 89 | * | |
| 90 | * @param collection The collection to filter. | |
| 91 | * @param filter The filter. | |
| 92 | * @param <T> The type of object in the collection. | |
| 93 | */ | |
| 94 | public static <T> void filter(Collection<T> collection, Filter<T> filter) { | |
| 95 | 0 | Iterator<T> i = collection.iterator(); |
| 96 | ||
| 97 | 0 | while (i.hasNext()) { |
| 98 | 0 | if (!filter.accept(i.next())) { |
| 99 | 0 | i.remove(); |
| 100 | } | |
| 101 | } | |
| 102 | 0 | } |
| 103 | ||
| 104 | /** | |
| 105 | * Reverse the order of a map. This method provide correct result only for map who retain the insertion order. | |
| 106 | * | |
| 107 | * @param map The map to reverse. | |
| 108 | * @param <T> The Key type. | |
| 109 | * @param <K> The value type. | |
| 110 | */ | |
| 111 | public static <T, K> void reverse(Map<T, K> map) { | |
| 112 | 0 | List<Entry<T, K>> entries = new ArrayList<Entry<T, K>>(map.entrySet()); |
| 113 | ||
| 114 | 0 | map.clear(); |
| 115 | ||
| 116 | 0 | for (int i = entries.size() - 1; i >= 0; i--) { |
| 117 | 0 | Entry<T, K> entry = entries.get(i); |
| 118 | ||
| 119 | 0 | map.put(entry.getKey(), entry.getValue()); |
| 120 | } | |
| 121 | 0 | } |
| 122 | ||
| 123 | /** | |
| 124 | * Reverse a list. | |
| 125 | * | |
| 126 | * @param list The list to reverse. | |
| 127 | */ | |
| 128 | public static void reverse(List<?> list) { | |
| 129 | 2 | Collections.reverse(list); |
| 130 | 2 | } |
| 131 | ||
| 132 | /** | |
| 133 | * Sort a list. | |
| 134 | * | |
| 135 | * @param list The list to sort. | |
| 136 | * @param comparator The comparator to use to sort the list. | |
| 137 | * @param <T> The type of object in the collection. | |
| 138 | */ | |
| 139 | public static <T> void sort(List<T> list, Comparator<T> comparator) { | |
| 140 | 0 | Collections.sort(list, comparator); |
| 141 | 0 | } |
| 142 | ||
| 143 | /** | |
| 144 | * Return an empty list. This list is unmodifiable. | |
| 145 | * | |
| 146 | * @param <T> The Type of object to store in the list. | |
| 147 | * @return The empty list. | |
| 148 | */ | |
| 149 | public static <T> List<T> emptyList() { | |
| 150 | 4 | return (List<T>) EMPTY_LIST; |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * Move the iterator to the first element. | |
| 155 | * | |
| 156 | * @param iterator The list iterator. | |
| 157 | * @param <T> The type of object stored in the iterator. | |
| 158 | */ | |
| 159 | public static <T> void goToFirst(ListIterator<T> iterator) { | |
| 160 | 0 | while (iterator.hasPrevious()) { |
| 161 | 0 | iterator.previous(); |
| 162 | } | |
| 163 | 0 | } |
| 164 | ||
| 165 | /** | |
| 166 | * Move the iterator to the last element. | |
| 167 | * | |
| 168 | * @param iterator The iterator. | |
| 169 | * @param <T> The type of object stored in the iterator. | |
| 170 | */ | |
| 171 | public static <T> void goToLast(Iterator<T> iterator) { | |
| 172 | 0 | while (iterator.hasNext()) { |
| 173 | 0 | iterator.next(); |
| 174 | } | |
| 175 | 0 | } |
| 176 | ||
| 177 | /** | |
| 178 | * Convert the enumeration to a collection. | |
| 179 | * | |
| 180 | * @param enumeration The enumeration to convert to Collection. | |
| 181 | * @param <T> The type of object stored in the enumeration. | |
| 182 | * @return A Collection containing all the elements of the enumeration. | |
| 183 | */ | |
| 184 | public static <T> Collection<T> toCollection(Enumeration<T> enumeration) { | |
| 185 | 0 | Collection<T> collection = new ArrayList<T>(25); |
| 186 | ||
| 187 | 0 | while (enumeration.hasMoreElements()) { |
| 188 | 0 | collection.add(enumeration.nextElement()); |
| 189 | } | |
| 190 | ||
| 191 | 0 | return collection; |
| 192 | } | |
| 193 | ||
| 194 | /** | |
| 195 | * Return the first element of the collection. | |
| 196 | * | |
| 197 | * @param collection The collection. | |
| 198 | * @param <T> The objects stored in the collection. | |
| 199 | * @return The first element of the collection or null if the collection is empty. | |
| 200 | */ | |
| 201 | public static <T> T first(Iterable<T> collection) { | |
| 202 | 2 | return collection.iterator().next(); |
| 203 | } | |
| 204 | } |