Coverage Report - org.kuali.rice.core.util.CollectionUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionUtils
0%
0/18
0%
0/20
2.286
CollectionUtils$1
N/A
N/A
2.286
CollectionUtils$IterableEnumeration
0%
0/7
0%
0/2
2.286
CollectionUtils$IterableEnumeration$1
0%
0/4
N/A
2.286
CollectionUtils$IterableIterator
0%
0/7
0%
0/2
2.286
 
 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.Collections;
 20  
 import java.util.Enumeration;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 
 26  
 /**
 27  
  * Yet another utility class to help work with Collections. This class contains helper methods not
 28  
  * found in any of the Collection utilities rice currently uses.
 29  
  * 
 30  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 31  
  * 
 32  
  */
 33  
 public final class CollectionUtils {
 34  
 
 35  
     /** do not call. */
 36  0
     private CollectionUtils() {
 37  0
         throw new UnsupportedOperationException("do not call");
 38  
     }
 39  
 
 40  
     /**
 41  
      * Performs a "brute force" comparison of collections by testing whether the collections contain
 42  
      * each other. This circumvents any particular uniqueness or ordering constraints on the
 43  
      * collections (for instance, lists that are unordered but contain the same elements, where a
 44  
      * hashset would not suffice for comparison purposes because it enforces element uniqueness)
 45  
      */
 46  
     public static boolean collectionsEquivalent(Collection<?> a, Collection<?> b) {
 47  0
         if (a == null && b == null) {
 48  0
             return true;
 49  
         }
 50  0
         if (a == null ^ b == null) {
 51  0
             return false;
 52  
         }
 53  0
         return a.containsAll(b) && b.containsAll(a);
 54  
     }
 55  
 
 56  
     /**
 57  
      * Creates an Iterable view of a Iterator. This allows Iterators to be used in a foreach loop.
 58  
      * 
 59  
      * <pre>
 60  
          * {@code
 61  
          *   Iterator<String> i ...
 62  
          * 
 63  
          *   for(String s : CollectionUtils.toIterable(i)) [
 64  
          *     System.out.println("i love for each " + s);        
 65  
          *   }
 66  
          * }
 67  
          * </pre>
 68  
      * 
 69  
      * @param <T> the type of the Iterable
 70  
      * @param i the iterator to wrap
 71  
      * @return the iterable
 72  
      */
 73  
     public static <T> Iterable<T> toIterable(Iterator<T> i) {
 74  0
         return new IterableIterator<T>(i);
 75  
     }
 76  
 
 77  
     /**
 78  
      * Creates an Iterable view of a Enumeration. This allows Enumerations to be used in a foreach
 79  
      * loop.
 80  
      * 
 81  
      * <pre>
 82  
          * {@code
 83  
          *   Enumeration<String> e ...
 84  
          * 
 85  
          *   for(String s : CollectionUtils.toIterable(e)) [
 86  
          *     System.out.println("i love for each " + s);        
 87  
          *   }
 88  
          * }
 89  
          * </pre>
 90  
      * 
 91  
      * @param <T> the type of the Iterable
 92  
      * @param e the enumeration to wrap
 93  
      * @return the iterable
 94  
      */
 95  
     public static <T> Iterable<T> toIterable(Enumeration<T> e) {
 96  0
         return new IterableEnumeration<T>(e);
 97  
     }
 98  
 
 99  
     /**
 100  
      * Functions as per {@link Collections#unmodifiableList(List)} with the exception that if the
 101  
      * given list is null, this method will return an unmodifiable empty list as per
 102  
      * {@link Collections#emptyList()}.
 103  
      * 
 104  
      * @param list the list for which an unmodifiable view is to be returned
 105  
      * @return an unmodifiable view of the specified list, or an unmodifiable empty list if the
 106  
      *         given list is null
 107  
      */
 108  
     public static <T> List<T> unmodifiableListNullSafe(List<? extends T> list) {
 109  0
         if (list == null) {
 110  0
             return Collections.emptyList();
 111  
         }
 112  0
         return Collections.unmodifiableList(list);
 113  
     }
 114  
 
 115  
     /**
 116  
      * Functions as per {@link Collections#unmodifiableSet(Set)} with the exception that if the
 117  
      * given set is null, this method will return an unmodifiable empty set as per
 118  
      * {@link Collections#emptySet()}.
 119  
      * 
 120  
      * @param set the set for which an unmodifiable view is to be returned
 121  
      * @return an unmodifiable view of the specified set, or an unmodifiable empty set if the given
 122  
      *         set is null
 123  
      */
 124  
     public static <T> Set<T> unmodifiableSetNullSafe(Set<? extends T> set) {
 125  0
         if (set == null) {
 126  0
             return Collections.emptySet();
 127  
         }
 128  0
         return Collections.unmodifiableSet(set);
 129  
     }
 130  
 
 131  
     /**
 132  
      * Functions as per {@link Collections#unmodifiableMap(Map)} with the exception that if the
 133  
      * given map is null, this method will return an unmodifiable empty map as per
 134  
      * {@link Collections#emptyMap()}.
 135  
      * 
 136  
      * @param set the set for which an unmodifiable view is to be returned
 137  
      * @return an unmodifiable view of the specified set, or an unmodifiable empty set if the given
 138  
      *         set is null
 139  
      */
 140  
     public static <K, V> Map<K, V> unmodifiableMapNullSafe(Map<? extends K, ? extends V> map) {
 141  0
         if (map == null) {
 142  0
             return Collections.emptyMap();
 143  
         }
 144  0
         return Collections.unmodifiableMap(map);
 145  
     }
 146  
 
 147  
     /**
 148  
      * An adapter from an Enumeration to Iterable.
 149  
      * 
 150  
      * @author Kuali Rice Team (rice.collab@kuali.org)
 151  
      * 
 152  
      * @param <T> the type of Enumeration and Iterable
 153  
      */
 154  0
     private static class IterableEnumeration<T> implements Iterable<T> {
 155  
         private final Enumeration<T> e;
 156  
 
 157  0
         private IterableEnumeration(final Enumeration<T> e) {
 158  0
             if (e == null) {
 159  0
                 throw new IllegalArgumentException("the enumeration is null");
 160  
             }
 161  
 
 162  0
             this.e = e;
 163  0
         }
 164  
 
 165  
         @Override
 166  
         public Iterator<T> iterator() {
 167  0
             return new Iterator<T>() {
 168  
                 @Override
 169  
                 public boolean hasNext() {
 170  0
                     return e.hasMoreElements();
 171  
                 }
 172  
 
 173  
                 @Override
 174  
                 public T next() {
 175  0
                     return e.nextElement();
 176  
                 }
 177  
 
 178  
                 @Override
 179  
                 public void remove() {
 180  0
                     throw new UnsupportedOperationException("this iterator does not support remove");
 181  
                 }
 182  
             };
 183  
         }
 184  
     }
 185  
 
 186  
     /**
 187  
      * An adapter from an Iterator to Iterable.
 188  
      * 
 189  
      * @author Kuali Rice Team (rice.collab@kuali.org)
 190  
      * 
 191  
      * @param <T> the type of Iterator and Iterable
 192  
      */
 193  0
     private static class IterableIterator<T> implements Iterable<T> {
 194  
         private final Iterator<T> i;
 195  
 
 196  0
         private IterableIterator(final Iterator<T> i) {
 197  0
             if (i == null) {
 198  0
                 throw new IllegalArgumentException("the iterator is null");
 199  
             }
 200  
 
 201  0
             this.i = i;
 202  0
         }
 203  
 
 204  
         @Override
 205  
         public Iterator<T> iterator() {
 206  0
             return i;
 207  
         }
 208  
     }
 209  
 }