| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CollectionUtils |
|
| 1.6;1.6 | ||||
| CollectionUtils$1 |
|
| 1.6;1.6 | ||||
| CollectionUtils$IterableEnumeration |
|
| 1.6;1.6 | ||||
| CollectionUtils$IterableEnumeration$1 |
|
| 1.6;1.6 | ||||
| CollectionUtils$IterableIterator |
|
| 1.6;1.6 |
| 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.Enumeration; | |
| 19 | import java.util.Iterator; | |
| 20 | ||
| 21 | /** | |
| 22 | * Yet another utility class to help work with Collections. This class contains helper methods not found in any of the Collection | |
| 23 | * utilities rice currently uses. | |
| 24 | * | |
| 25 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 26 | * | |
| 27 | */ | |
| 28 | public class CollectionUtils { | |
| 29 | ||
| 30 | /** do not call. */ | |
| 31 | 0 | private CollectionUtils() { |
| 32 | 0 | throw new UnsupportedOperationException("do not call"); |
| 33 | } | |
| 34 | ||
| 35 | /** | |
| 36 | * Creates an Iterable view of a Iterator. This allows Iterators to be used in a foreach loop. | |
| 37 | * | |
| 38 | * <pre> | |
| 39 | * {@code | |
| 40 | * Iterator<String> i ... | |
| 41 | * | |
| 42 | * for(String s : CollectionUtils.toIterable(i)) [ | |
| 43 | * System.out.println("i love for each " + s); | |
| 44 | * } | |
| 45 | * } | |
| 46 | * </pre> | |
| 47 | * | |
| 48 | * @param <T> the type of the Iterable | |
| 49 | * @param i the iterator to wrap | |
| 50 | * @return the iterable | |
| 51 | */ | |
| 52 | public static <T> Iterable<T> toIterable(Iterator<T> i) { | |
| 53 | 0 | return new IterableIterator(i); |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * Creates an Iterable view of a Enumeration. This allows Enumerations to be used in a foreach loop. | |
| 58 | * | |
| 59 | * <pre> | |
| 60 | * {@code | |
| 61 | * Enumeration<String> e ... | |
| 62 | * | |
| 63 | * for(String s : CollectionUtils.toIterable(e)) [ | |
| 64 | * System.out.println("i love for each " + s); | |
| 65 | * } | |
| 66 | * } | |
| 67 | * </pre> | |
| 68 | * | |
| 69 | * @param <T> the type of the Iterable | |
| 70 | * @param e the enumeration to wrap | |
| 71 | * @return the iterable | |
| 72 | */ | |
| 73 | public static <T> Iterable<T> toIterable(Enumeration<T> e) { | |
| 74 | 0 | return new IterableEnumeration(e); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * An adapter from an Enumeration to Iterable. | |
| 79 | * | |
| 80 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 81 | * | |
| 82 | * @param <T> the type of Enumeration and Iterable | |
| 83 | */ | |
| 84 | 0 | private static class IterableEnumeration<T> implements Iterable<T> { |
| 85 | private final Enumeration<T> e; | |
| 86 | 0 | private IterableEnumeration(final Enumeration<T> e) { |
| 87 | 0 | if (e == null) { |
| 88 | 0 | throw new IllegalArgumentException("the enumeration is null"); |
| 89 | } | |
| 90 | ||
| 91 | 0 | this.e = e; |
| 92 | 0 | } |
| 93 | ||
| 94 | public Iterator<T> iterator() { | |
| 95 | 0 | return new Iterator<T>() { |
| 96 | public boolean hasNext() { | |
| 97 | 0 | return e.hasMoreElements(); |
| 98 | } | |
| 99 | public T next() { | |
| 100 | 0 | return e.nextElement(); |
| 101 | } | |
| 102 | public void remove() { | |
| 103 | 0 | throw new UnsupportedOperationException("this iterator does not support remove"); |
| 104 | } | |
| 105 | }; | |
| 106 | } | |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * An adapter from an Iterator to Iterable. | |
| 111 | * | |
| 112 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 113 | * | |
| 114 | * @param <T> the type of Iterator and Iterable | |
| 115 | */ | |
| 116 | 0 | private static class IterableIterator<T> implements Iterable<T> { |
| 117 | private final Iterator<T> i; | |
| 118 | 0 | private IterableIterator(final Iterator<T> i) { |
| 119 | 0 | if (i == null) { |
| 120 | 0 | throw new IllegalArgumentException("the iterator is null"); |
| 121 | } | |
| 122 | ||
| 123 | 0 | this.i = i; |
| 124 | 0 | } |
| 125 | ||
| 126 | public Iterator<T> iterator() { | |
| 127 | 0 | return i; |
| 128 | } | |
| 129 | } | |
| 130 | } |