Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Vosburgh2013-11-21 23:34:55 +0000
committerBrian Vosburgh2015-07-16 18:54:43 +0000
commit1213b41849f7f7c1d398f2ee4108b89050b9dca5 (patch)
treebbee76d39083fb6ba3d57f45723ab2a9e30084aa
parentb6576888ab373c78099ac1c3a1b46527cd3224f5 (diff)
downloadwebtools.dali-1213b41849f7f7c1d398f2ee4108b89050b9dca5.tar.gz
webtools.dali-1213b41849f7f7c1d398f2ee4108b89050b9dca5.tar.xz
webtools.dali-1213b41849f7f7c1d398f2ee4108b89050b9dca5.zip
rework IterableTools and IteratorTools to return more-abstract types
from various methods (typically interfaces instead of concrete classes)
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterable/IterableTools.java245
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterator/IteratorTools.java415
2 files changed, 485 insertions, 175 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterable/IterableTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterable/IterableTools.java
index 5d0d728da5..0b84ffbed0 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterable/IterableTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterable/IterableTools.java
@@ -44,7 +44,7 @@ public final class IterableTools {
* Return a bag corresponding to the specified iterable.
*/
public static <E> HashBag<E> bag(Iterable<? extends E> iterable) {
- return CollectionTools.bag(iterable);
+ return IteratorTools.bag(iterable.iterator());
}
/**
@@ -52,14 +52,14 @@ public final class IterableTools {
* The specified iterable size is a performance hint.
*/
public static <E> HashBag<E> bag(Iterable<? extends E> iterable, int iterableSize) {
- return CollectionTools.bag(iterable, iterableSize);
+ return IteratorTools.bag(iterable.iterator(), iterableSize);
}
/**
* Return a collection corresponding to the specified iterable.
*/
public static <E> HashBag<E> collection(Iterable<? extends E> iterable) {
- return CollectionTools.collection(iterable);
+ return IteratorTools.collection(iterable.iterator());
}
/**
@@ -67,7 +67,7 @@ public final class IterableTools {
* The specified iterable size is a performance hint.
*/
public static <E> HashBag<E> collection(Iterable<? extends E> iterable, int iterableSize) {
- return CollectionTools.collection(iterable, iterableSize);
+ return IteratorTools.collection(iterable.iterator(), iterableSize);
}
/**
@@ -349,7 +349,7 @@ public final class IterableTools {
* Return a list corresponding to the specified iterable.
*/
public static <E> ArrayList<E> list(Iterable<? extends E> iterable) {
- return ListTools.list(iterable);
+ return IteratorTools.list(iterable.iterator());
}
/**
@@ -357,7 +357,7 @@ public final class IterableTools {
* The specified iterable size is a performance hint.
*/
public static <E> ArrayList<E> list(Iterable<? extends E> iterable, int iterableSize) {
- return ListTools.list(iterable, iterableSize);
+ return IteratorTools.list(iterable.iterator(), iterableSize);
}
/**
@@ -386,7 +386,11 @@ public final class IterableTools {
* Return an iterable containing the sorted elements of the specified iterable.
*/
public static <E> Iterable<E> sort(Iterable<? extends E> iterable, Comparator<? super E> comparator) {
- return ListTools.sort(ListTools.list(iterable), comparator);
+ Iterator<? extends E> iterator = iterable.iterator();
+ if (iterator.hasNext()) {
+ return ListTools.sort(ListTools.list(iterator), comparator);
+ }
+ return emptyIterable();
}
/**
@@ -394,7 +398,11 @@ public final class IterableTools {
* The specified iterable size is a performance hint.
*/
public static <E> Iterable<E> sort(Iterable<? extends E> iterable, Comparator<? super E> comparator, int iterableSize) {
- return ListTools.sort(ListTools.list(iterable, iterableSize), comparator);
+ Iterator<? extends E> iterator = iterable.iterator();
+ if (iterator.hasNext()) {
+ return ListTools.sort(ListTools.list(iterator, iterableSize), comparator);
+ }
+ return emptyIterable();
}
/**
@@ -402,7 +410,7 @@ public final class IterableTools {
* @see Collection#toArray()
*/
public static Object[] toArray(Iterable<?> iterable) {
- return list(iterable).toArray();
+ return IteratorTools.toArray(iterable.iterator());
}
/**
@@ -411,7 +419,7 @@ public final class IterableTools {
* @see Collection#toArray()
*/
public static Object[] toArray(Iterable<?> iterable, int iterableSize) {
- return list(iterable, iterableSize).toArray();
+ return IteratorTools.toArray(iterable.iterator(), iterableSize);
}
/**
@@ -419,7 +427,7 @@ public final class IterableTools {
* @see Collection#toArray(Object[])
*/
public static <E> E[] toArray(Iterable<? extends E> iterable, E[] array) {
- return list(iterable).toArray(array);
+ return IteratorTools.toArray(iterable.iterator(), array);
}
/**
@@ -428,7 +436,7 @@ public final class IterableTools {
* @see Collection#toArray(Object[])
*/
public static <E> E[] toArray(Iterable<? extends E> iterable, int iterableSize, E[] array) {
- return list(iterable, iterableSize).toArray(array);
+ return IteratorTools.toArray(iterable.iterator(), iterableSize, array);
}
@@ -439,8 +447,12 @@ public final class IterableTools {
* in the specified iterables.
* @see SimultaneousIterable
*/
- public static <E, I extends Iterable<? extends E>> SimultaneousIterable<E> align(I... iterables) {
- return align(iterable(iterables), iterables.length);
+ public static <E, I extends Iterable<? extends E>> Iterable<List<E>> align(I... iterables) {
+ int len = iterables.length;
+ if (len == 0) {
+ return emptyIterable();
+ }
+ return align(iterable(iterables), len);
}
/**
@@ -448,8 +460,8 @@ public final class IterableTools {
* in the specified iterables.
* @see SimultaneousIterable
*/
- public static <E, I extends Iterable<? extends E>> SimultaneousIterable<E> align(Iterable<I> iterables) {
- return new SimultaneousIterable<E>(iterables);
+ public static <E, I extends Iterable<? extends E>> Iterable<List<E>> align(Iterable<I> iterables) {
+ return align(iterables, -1);
}
/**
@@ -457,7 +469,7 @@ public final class IterableTools {
* in the specified iterables.
* @see SimultaneousIterable
*/
- public static <E, I extends Iterable<? extends E>> SimultaneousIterable<E> align(Iterable<I> iterables, int iterablesSize) {
+ public static <E, I extends Iterable<? extends E>> Iterable<List<E>> align(Iterable<I> iterables, int iterablesSize) {
return new SimultaneousIterable<E>(iterables, iterablesSize);
}
@@ -466,8 +478,12 @@ public final class IterableTools {
* in the specified iterables.
* @see SimultaneousListIterable
*/
- public static <E, I extends ListIterable<E>> SimultaneousListIterable<E> alignList(I... iterables) {
- return alignList(iterable(iterables), iterables.length);
+ public static <E, I extends ListIterable<E>> ListIterable<List<E>> alignList(I... iterables) {
+ int len = iterables.length;
+ if (len == 0) {
+ return emptyListIterable();
+ }
+ return alignList(iterable(iterables), len);
}
/**
@@ -475,8 +491,8 @@ public final class IterableTools {
* in the specified iterables.
* @see SimultaneousListIterable
*/
- public static <E, I extends ListIterable<E>> SimultaneousListIterable<E> alignList(Iterable<I> iterables) {
- return new SimultaneousListIterable<E>(iterables);
+ public static <E, I extends ListIterable<E>> ListIterable<List<E>> alignList(Iterable<I> iterables) {
+ return alignList(iterables, -1);
}
/**
@@ -484,7 +500,7 @@ public final class IterableTools {
* in the specified iterables.
* @see SimultaneousListIterable
*/
- public static <E, I extends ListIterable<E>> SimultaneousListIterable<E> alignList(Iterable<I> iterables, int iterablesSize) {
+ public static <E, I extends ListIterable<E>> ListIterable<List<E>> alignList(Iterable<I> iterables, int iterablesSize) {
return new SimultaneousListIterable<E>(iterables, iterablesSize);
}
@@ -492,7 +508,7 @@ public final class IterableTools {
* Return an iterable that converts the specified iterable's element type.
* @see LateralIterableWrapper
*/
- public static <E1, E2> LateralIterableWrapper<E1, E2> cast(Iterable<E1> iterable) {
+ public static <E1, E2> Iterable<E2> cast(Iterable<E1> iterable) {
return new LateralIterableWrapper<E1, E2>(iterable);
}
@@ -500,7 +516,7 @@ public final class IterableTools {
* Return a list iterable that converts the specified list iterable's element type.
* @see LateralIterableWrapper
*/
- public static <E1, E2> LateralListIterableWrapper<E1, E2> cast(ListIterable<E1> iterable) {
+ public static <E1, E2> ListIterable<E2> cast(ListIterable<E1> iterable) {
return new LateralListIterableWrapper<E1, E2>(iterable);
}
@@ -508,7 +524,7 @@ public final class IterableTools {
* Return an iterable that converts the specified iterable's element type.
* @see SubIterableWrapper
*/
- public static <E1, E2 extends E1> SubIterableWrapper<E1, E2> downCast(Iterable<E1> iterable) {
+ public static <E1, E2 extends E1> Iterable<E2> downCast(Iterable<E1> iterable) {
return new SubIterableWrapper<E1, E2>(iterable);
}
@@ -516,7 +532,7 @@ public final class IterableTools {
* Return an iterable that converts the specified iterable's element type.
* @see SubListIterableWrapper
*/
- public static <E1, E2 extends E1> SubListIterableWrapper<E1, E2> downCast(ListIterable<E1> iterable) {
+ public static <E1, E2 extends E1> ListIterable<E2> downCast(ListIterable<E1> iterable) {
return new SubListIterableWrapper<E1, E2>(iterable);
}
@@ -524,7 +540,7 @@ public final class IterableTools {
* Return an iterable that converts the specified iterable's element type.
* @see SuperIterableWrapper
*/
- public static <E> SuperIterableWrapper<E> upCast(Iterable<? extends E> iterable) {
+ public static <E> Iterable<E> upCast(Iterable<? extends E> iterable) {
return new SuperIterableWrapper<E>(iterable);
}
@@ -532,7 +548,7 @@ public final class IterableTools {
* Return an iterable that converts the specified iterable's element type.
* @see SuperListIterableWrapper
*/
- public static <E> SuperListIterableWrapper<E> upCast(ListIterable<? extends E> iterable) {
+ public static <E> ListIterable<E> upCast(ListIterable<? extends E> iterable) {
return new SuperListIterableWrapper<E>(iterable);
}
@@ -541,7 +557,10 @@ public final class IterableTools {
* the specified {@link Transformer transformer}.
* @see ChainIterable
*/
- public static <E> ChainIterable<E> chainIterable(E first, Transformer<? super E, ? extends E> transformer) {
+ public static <E> Iterable<E> chainIterable(E first, Transformer<? super E, ? extends E> transformer) {
+ if (first == null) {
+ return emptyIterable();
+ }
return new ChainIterable<E>(first, transformer);
}
@@ -550,7 +569,7 @@ public final class IterableTools {
* elements.
* @see LiveCloneIterable
*/
- public static <E> LiveCloneIterable<E> cloneLive(Collection<? extends E> collection) {
+ public static <E> Iterable<E> cloneLive(Collection<? extends E> collection) {
return cloneLive(collection, DisabledClosure.instance());
}
@@ -559,7 +578,7 @@ public final class IterableTools {
* elements and uses the specified remove closure.
* @see LiveCloneIterable
*/
- public static <E> LiveCloneIterable<E> cloneLive(Collection<? extends E> collection, Closure<? super E> removeClosure) {
+ public static <E> Iterable<E> cloneLive(Collection<? extends E> collection, Closure<? super E> removeClosure) {
return new LiveCloneIterable<E>(collection, removeClosure);
}
@@ -568,7 +587,7 @@ public final class IterableTools {
* elements.
* @see LiveCloneListIterable
*/
- public static <E> LiveCloneListIterable<E> cloneLive(List<? extends E> list) {
+ public static <E> ListIterable<E> cloneLive(List<? extends E> list) {
return cloneLive(list, CloneListIterator.Adapter.ReadOnly.<E>instance());
}
@@ -577,7 +596,7 @@ public final class IterableTools {
* elements and uses the specified {@link org.eclipse.jpt.common.utility.internal.iterator.CloneListIterator.Adapter adapter}.
* @see LiveCloneListIterable
*/
- public static <E> LiveCloneListIterable<E> cloneLive(List<? extends E> list, CloneListIterator.Adapter<E> adapter) {
+ public static <E> ListIterable<E> cloneLive(List<? extends E> list, CloneListIterator.Adapter<E> adapter) {
return new LiveCloneListIterable<E>(list, adapter);
}
@@ -586,16 +605,19 @@ public final class IterableTools {
* elements.
* @see SnapshotCloneIterable
*/
- public static <E> SnapshotCloneIterable<E> cloneSnapshot(Collection<? extends E> collection) {
+ public static <E> Iterable<E> cloneSnapshot(Collection<? extends E> collection) {
return cloneSnapshot(collection, DisabledClosure.instance());
}
/**
* Return an iterable that clones the specified collection before returning
* elements and uses the specified remove closure.
- * @see LiveCloneIterable
+ * @see SnapshotCloneIterable
*/
- public static <E> SnapshotCloneIterable<E> cloneSnapshot(Collection<? extends E> collection, Closure<? super E> removeClosure) {
+ public static <E> Iterable<E> cloneSnapshot(Collection<? extends E> collection, Closure<? super E> removeClosure) {
+ if (collection.isEmpty()) {
+ return emptyIterable();
+ }
return new SnapshotCloneIterable<E>(collection, removeClosure);
}
@@ -604,7 +626,7 @@ public final class IterableTools {
* elements.
* @see SnapshotCloneListIterable
*/
- public static <E> SnapshotCloneListIterable<E> cloneSnapshot(List<? extends E> list) {
+ public static <E> ListIterable<E> cloneSnapshot(List<? extends E> list) {
return cloneSnapshot(list, CloneListIterator.Adapter.ReadOnly.<E>instance());
}
@@ -613,7 +635,10 @@ public final class IterableTools {
* elements and uses the specified {@link org.eclipse.jpt.common.utility.internal.iterator.CloneListIterator.Adapter adapter}.
* @see SnapshotCloneListIterable
*/
- public static <E> SnapshotCloneListIterable<E> cloneSnapshot(List<? extends E> list, CloneListIterator.Adapter<E> adapter) {
+ public static <E> ListIterable<E> cloneSnapshot(List<? extends E> list, CloneListIterator.Adapter<E> adapter) {
+ if (list.isEmpty()) {
+ return emptyListIterable();
+ }
return new SnapshotCloneListIterable<E>(list, adapter);
}
@@ -623,8 +648,8 @@ public final class IterableTools {
* @see CompositeIterable
*/
@SuppressWarnings("unchecked")
- public static <E> CompositeIterable<E> add(Iterable<? extends E> iterable, E object) {
- return concatenate(iterable, singletonIterable(object));
+ public static <E> Iterable<E> add(Iterable<? extends E> iterable, E object) {
+ return concatenate_(iterable, singletonIterable(object));
}
/**
@@ -633,8 +658,8 @@ public final class IterableTools {
* @see CompositeIterable
*/
@SuppressWarnings("unchecked")
- public static <E> CompositeIterable<E> insert(E object, Iterable<? extends E> iterable) {
- return concatenate(singletonIterable(object), iterable);
+ public static <E> Iterable<E> insert(E object, Iterable<? extends E> iterable) {
+ return concatenate_(singletonIterable(object), iterable);
}
/**
@@ -642,7 +667,22 @@ public final class IterableTools {
* elements in the specified iterables.
* @see CompositeIterable
*/
- public static <E> CompositeIterable<E> concatenate(Iterable<? extends E>... iterables) {
+ @SuppressWarnings("unchecked")
+ public static <E> Iterable<E> concatenate(Iterable<? extends E>... iterables) {
+ int len = iterables.length;
+ if (len == 0) {
+ return emptyIterable();
+ }
+ if (len == 1) {
+ return (Iterable<E>) iterables[0];
+ }
+ return concatenate_(iterables);
+ }
+
+ /**
+ * assume the list is not empty
+ */
+ private static <E> Iterable<E> concatenate_(Iterable<? extends E>... iterables) {
return concatenate(Arrays.asList(iterables));
}
@@ -651,7 +691,7 @@ public final class IterableTools {
* elements in the specified iterables.
* @see CompositeIterable
*/
- public static <E> CompositeIterable<E> concatenate(Iterable<? extends Iterable<? extends E>> iterables) {
+ public static <E> Iterable<E> concatenate(Iterable<? extends Iterable<? extends E>> iterables) {
return new CompositeIterable<E>(iterables);
}
@@ -660,7 +700,7 @@ public final class IterableTools {
* Use the specified transformer to transform each parent into its children.
* @see CompositeIterable
*/
- public static <P, E> CompositeIterable<E> children(Iterable<? extends P> parents, Transformer<? super P, ? extends Iterable<? extends E>> childrenTransformer) {
+ public static <P, E> Iterable<E> children(Iterable<? extends P> parents, Transformer<? super P, ? extends Iterable<? extends E>> childrenTransformer) {
return concatenate(transform(parents, childrenTransformer));
}
@@ -670,8 +710,8 @@ public final class IterableTools {
* @see CompositeListIterable
*/
@SuppressWarnings("unchecked")
- public static <E> CompositeListIterable<E> add(ListIterable<E> iterable, E object) {
- return concatenate(iterable, singletonListIterable(object));
+ public static <E> ListIterable<E> add(ListIterable<E> iterable, E object) {
+ return concatenate_(iterable, singletonListIterable(object));
}
/**
@@ -680,8 +720,8 @@ public final class IterableTools {
* @see CompositeListIterable
*/
@SuppressWarnings("unchecked")
- public static <E> CompositeListIterable<E> insert(E object, ListIterable<E> iterable) {
- return concatenate(singletonListIterable(object), iterable);
+ public static <E> ListIterable<E> insert(E object, ListIterable<E> iterable) {
+ return concatenate_(singletonListIterable(object), iterable);
}
/**
@@ -689,7 +729,21 @@ public final class IterableTools {
* elements in the specified iterables.
* @see CompositeListIterable
*/
- public static <E> CompositeListIterable<E> concatenate(ListIterable<E>... iterables) {
+ public static <E> ListIterable<E> concatenate(ListIterable<E>... iterables) {
+ int len = iterables.length;
+ if (len == 0) {
+ return emptyListIterable();
+ }
+ if (len == 1) {
+ return iterables[0];
+ }
+ return concatenate_(iterables);
+ }
+
+ /**
+ * assume the list is not empty
+ */
+ private static <E> ListIterable<E> concatenate_(ListIterable<E>... iterables) {
return concatenate(listIterable(iterables));
}
@@ -698,7 +752,7 @@ public final class IterableTools {
* elements in the specified iterables.
* @see CompositeListIterable
*/
- public static <E> CompositeListIterable<E> concatenate(ListIterable<? extends ListIterable<E>> iterables) {
+ public static <E> ListIterable<E> concatenate(ListIterable<? extends ListIterable<E>> iterables) {
return new CompositeListIterable<E>(iterables);
}
@@ -707,7 +761,7 @@ public final class IterableTools {
* Use the specified transformer to transform each parent into its children.
* @see CompositeListIterable
*/
- public static <P, E> CompositeListIterable<E> children(ListIterable<? extends P> parents, Transformer<? super P, ? extends ListIterable<E>> childrenTransformer) {
+ public static <P, E> ListIterable<E> children(ListIterable<? extends P> parents, Transformer<? super P, ? extends ListIterable<E>> childrenTransformer) {
return concatenate(transform(parents, childrenTransformer));
}
@@ -717,8 +771,8 @@ public final class IterableTools {
* @see ReadOnlyCompositeListIterable
*/
@SuppressWarnings("unchecked")
- public static <E> ReadOnlyCompositeListIterable<E> addReadOnly(ListIterable<? extends E> iterable, E object) {
- return concatenateReadOnly(iterable, singletonListIterable(object));
+ public static <E> ListIterable<E> addReadOnly(ListIterable<? extends E> iterable, E object) {
+ return concatenateReadOnly_(iterable, singletonListIterable(object));
}
/**
@@ -727,8 +781,8 @@ public final class IterableTools {
* @see ReadOnlyCompositeListIterable
*/
@SuppressWarnings("unchecked")
- public static <E> ReadOnlyCompositeListIterable<E> insertReadOnly(E object, ListIterable<? extends E> iterable) {
- return concatenateReadOnly(singletonListIterable(object), iterable);
+ public static <E> ListIterable<E> insertReadOnly(E object, ListIterable<? extends E> iterable) {
+ return concatenateReadOnly_(singletonListIterable(object), iterable);
}
/**
@@ -736,7 +790,22 @@ public final class IterableTools {
* elements in the specified iterables.
* @see ReadOnlyCompositeListIterable
*/
- public static <E> ReadOnlyCompositeListIterable<E> concatenateReadOnly(ListIterable<? extends E>... iterables) {
+ @SuppressWarnings("unchecked")
+ public static <E> ListIterable<E> concatenateReadOnly(ListIterable<? extends E>... iterables) {
+ int len = iterables.length;
+ if (len == 0) {
+ return emptyListIterable();
+ }
+ if (len == 1) {
+ return (ListIterable<E>) iterables[0];
+ }
+ return concatenateReadOnly_(iterables);
+ }
+
+ /**
+ * assume the list is not empty
+ */
+ private static <E> ListIterable<E> concatenateReadOnly_(ListIterable<? extends E>... iterables) {
return concatenateReadOnly(listIterable(iterables));
}
@@ -745,7 +814,7 @@ public final class IterableTools {
* elements in the specified iterables.
* @see ReadOnlyCompositeListIterable
*/
- public static <E> ReadOnlyCompositeListIterable<E> concatenateReadOnly(ListIterable<? extends ListIterable<? extends E>> iterables) {
+ public static <E> ListIterable<E> concatenateReadOnly(ListIterable<? extends ListIterable<? extends E>> iterables) {
return new ReadOnlyCompositeListIterable<E>(iterables);
}
@@ -754,7 +823,7 @@ public final class IterableTools {
* Use the specified transformer to transform each parent into its children.
* @see ReadOnlyCompositeListIterable
*/
- public static <P, E> ReadOnlyCompositeListIterable<E> readOnlyChildren(ListIterable<? extends P> parents, Transformer<? super P, ? extends ListIterable<? extends E>> childrenTransformer) {
+ public static <P, E> ListIterable<E> readOnlyChildren(ListIterable<? extends P> parents, Transformer<? super P, ? extends ListIterable<? extends E>> childrenTransformer) {
return concatenateReadOnly(transform(parents, childrenTransformer));
}
@@ -777,7 +846,7 @@ public final class IterableTools {
* elements in the specified iterable.
* @see FilteringIterable
*/
- public static <E> FilteringIterable<E> filter(Iterable<? extends E> iterable, Predicate<? super E> predicate) {
+ public static <E> Iterable<E> filter(Iterable<? extends E> iterable, Predicate<? super E> predicate) {
return new FilteringIterable<E>(iterable, predicate);
}
@@ -786,7 +855,7 @@ public final class IterableTools {
* elements in the specified iterable.
* @see FilteringIterable
*/
- public static <E> FilteringIterable<E> removeNulls(Iterable<? extends E> iterable) {
+ public static <E> Iterable<E> removeNulls(Iterable<? extends E> iterable) {
return filter(iterable, PredicateTools.isNotNull());
}
@@ -795,7 +864,7 @@ public final class IterableTools {
* by its children etc. as determined by the specified transformer.
* @see GraphIterable
*/
- public static <E> GraphIterable<E> graphIterable(E root, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
+ public static <E> Iterable<E> graphIterable(E root, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
return graphIterable(singletonIterable(root), transformer);
}
@@ -804,7 +873,10 @@ public final class IterableTools {
* by their children etc. as determined by the specified transformer.
* @see GraphIterable
*/
- public static <E> GraphIterable<E> graphIterable(E[] roots, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
+ public static <E> Iterable<E> graphIterable(E[] roots, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
+ if (roots.length == 0) {
+ return emptyIterable();
+ }
return graphIterable(Arrays.asList(roots), transformer);
}
@@ -813,14 +885,14 @@ public final class IterableTools {
* by their children etc. as determined by the specified transformer.
* @see GraphIterable
*/
- public static <E> GraphIterable<E> graphIterable(Iterable<? extends E> roots, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
+ public static <E> Iterable<E> graphIterable(Iterable<? extends E> roots, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
return new GraphIterable<E>(roots, transformer);
}
/**
* Return an iterable on the elements in the specified array.
*/
- public static <E> ArrayIterable<E> iterable(E... array) {
+ public static <E> Iterable<E> iterable(E... array) {
return iterable(array, 0);
}
@@ -828,7 +900,7 @@ public final class IterableTools {
* Return an iterable on the elements in the specified array
* starting at the specified position in the array.
*/
- public static <E> ArrayIterable<E> iterable(E[] array, int start) {
+ public static <E> Iterable<E> iterable(E[] array, int start) {
return iterable(array, start, array.length);
}
@@ -837,7 +909,10 @@ public final class IterableTools {
* starting at the specified start index, inclusive, and continuing to
* the specified end index, exclusive.
*/
- public static <E> ArrayIterable<E> iterable(E[] array, int start, int end) {
+ public static <E> Iterable<E> iterable(E[] array, int start, int end) {
+ if (start == end) {
+ return emptyIterable();
+ }
return new ArrayIterable<E>(array, start, end);
}
@@ -845,7 +920,7 @@ public final class IterableTools {
* Return an iterable on the specified queue.
* @see Queue
*/
- public static <E> QueueIterable<E> iterable(Queue<? extends E> queue) {
+ public static <E> Iterable<E> iterable(Queue<? extends E> queue) {
return new QueueIterable<E>(queue);
}
@@ -853,14 +928,14 @@ public final class IterableTools {
* Return an iterable on the specified stack.
* @see Stack
*/
- public static <E> StackIterable<E> iterable(Stack<? extends E> stack) {
+ public static <E> Iterable<E> iterable(Stack<? extends E> stack) {
return new StackIterable<E>(stack);
}
/**
* Return a list iterable for the specified array.
*/
- public static <E> ArrayListIterable<E> listIterable(E... array) {
+ public static <E> ListIterable<E> listIterable(E... array) {
return listIterable(array, 0);
}
@@ -868,7 +943,7 @@ public final class IterableTools {
* Return a list iterable for the specified array
* starting at the specified position in the array.
*/
- public static <E> ArrayListIterable<E> listIterable(E[] array, int start) {
+ public static <E> ListIterable<E> listIterable(E[] array, int start) {
return listIterable(array, start, array.length);
}
@@ -877,14 +952,17 @@ public final class IterableTools {
* starting at the specified start index, inclusive, and continuing to
* the specified end index, exclusive.
*/
- public static <E> ArrayListIterable<E> listIterable(E[] array, int start, int end) {
+ public static <E> ListIterable<E> listIterable(E[] array, int start, int end) {
+ if (start == end) {
+ return emptyListIterable();
+ }
return new ArrayListIterable<E>(array, start, end);
}
/**
* Return a list iterable for the specified list.
*/
- public static <E> ListListIterable<E> listIterable(List<E> list) {
+ public static <E> ListIterable<E> listIterable(List<E> list) {
return new ListListIterable<E>(list);
}
@@ -901,7 +979,7 @@ public final class IterableTools {
* Convert the specified iterable to read-only.
* @see ReadOnlyIterable
*/
- public static <E> ReadOnlyIterable<E> readOnly(Iterable<? extends E> iterable) {
+ public static <E> Iterable<E> readOnly(Iterable<? extends E> iterable) {
return new ReadOnlyIterable<E>(iterable);
}
@@ -909,7 +987,7 @@ public final class IterableTools {
* Convert the specified iterable to read-only.
* @see ReadOnlyListIterable
*/
- public static <E> ReadOnlyListIterable<E> readOnly(ListIterable<? extends E> iterable) {
+ public static <E> ListIterable<E> readOnly(ListIterable<? extends E> iterable) {
return new ReadOnlyListIterable<E>(iterable);
}
@@ -918,7 +996,7 @@ public final class IterableTools {
* specified object.
* @see SingleElementIterable
*/
- public static <E> SingleElementIterable<E> singletonIterable(E value) {
+ public static <E> Iterable<E> singletonIterable(E value) {
return new SingleElementIterable<E>(value);
}
@@ -927,7 +1005,7 @@ public final class IterableTools {
* specified object.
* @see SingleElementListIterable
*/
- public static <E> SingleElementListIterable<E> singletonListIterable(E value) {
+ public static <E> ListIterable<E> singletonListIterable(E value) {
return new SingleElementListIterable<E>(value);
}
@@ -936,7 +1014,7 @@ public final class IterableTools {
* elements in the specified iterable.
* @see TransformationIterable
*/
- public static <E1, E2> TransformationIterable<E1, E2> transform(Iterable<? extends E1> iterable, Transformer<? super E1, ? extends E2> transformer) {
+ public static <E1, E2> Iterable<E2> transform(Iterable<? extends E1> iterable, Transformer<? super E1, ? extends E2> transformer) {
return new TransformationIterable<E1, E2>(iterable, transformer);
}
@@ -945,7 +1023,7 @@ public final class IterableTools {
* elements in the specified iterable.
* @see TransformationListIterable
*/
- public static <E1, E2, T extends E1> TransformationListIterable<E1, E2> transform(ListIterable<T> iterable, Transformer<? super E1, ? extends E2> transformer) {
+ public static <E1, E2, T extends E1> ListIterable<E2> transform(ListIterable<T> iterable, Transformer<? super E1, ? extends E2> transformer) {
return new TransformationListIterable<E1, E2>(iterable, transformer);
}
@@ -954,7 +1032,7 @@ public final class IterableTools {
* with the specified root and transformer.
* @see TreeIterable
*/
- public static <E> TreeIterable<E> treeIterable(E root, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
+ public static <E> Iterable<E> treeIterable(E root, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
return treeIterable(singletonIterable(root), transformer);
}
@@ -963,7 +1041,10 @@ public final class IterableTools {
* with the specified roots and transformer.
* @see TreeIterable
*/
- public static <E> TreeIterable<E> treeIterable(E[] roots, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
+ public static <E> Iterable<E> treeIterable(E[] roots, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
+ if (roots.length == 0) {
+ return emptyIterable();
+ }
return treeIterable(iterable(roots), transformer);
}
@@ -972,7 +1053,7 @@ public final class IterableTools {
* with the specified roots and transformer.
* @see TreeIterable
*/
- public static <E> TreeIterable<E> treeIterable(Iterable<? extends E> roots, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
+ public static <E> Iterable<E> treeIterable(Iterable<? extends E> roots, Transformer<? super E, ? extends Iterable<? extends E>> transformer) {
return new TreeIterable<E>(roots, transformer);
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterator/IteratorTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterator/IteratorTools.java
index 3c95c00258..2dc251b80d 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterator/IteratorTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/iterator/IteratorTools.java
@@ -21,11 +21,13 @@ import org.eclipse.jpt.common.utility.closure.InterruptibleClosure;
import org.eclipse.jpt.common.utility.collection.Queue;
import org.eclipse.jpt.common.utility.collection.Stack;
import org.eclipse.jpt.common.utility.exception.ExceptionHandler;
+import org.eclipse.jpt.common.utility.internal.ArrayTools;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.closure.DisabledClosure;
import org.eclipse.jpt.common.utility.internal.collection.CollectionTools;
import org.eclipse.jpt.common.utility.internal.collection.HashBag;
import org.eclipse.jpt.common.utility.internal.collection.ListTools;
+import org.eclipse.jpt.common.utility.internal.enumeration.EnumerationTools;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.common.utility.internal.iterator.CloneListIterator.Adapter;
import org.eclipse.jpt.common.utility.internal.predicate.PredicateTools;
@@ -153,7 +155,7 @@ public final class IteratorTools {
* elements in the specified collection.
*/
public static boolean containsAll(Iterator<?> iterator, Collection<?> collection) {
- return CollectionTools.set(iterator).containsAll(collection);
+ return collection.isEmpty() || CollectionTools.set(iterator).containsAll(collection);
}
/**
@@ -162,7 +164,7 @@ public final class IteratorTools {
* The specified iterator size is a performance hint.
*/
public static boolean containsAll(Iterator<?> iterator, int iteratorSize, Collection<?> collection) {
- return CollectionTools.set(iterator, iteratorSize).containsAll(collection);
+ return collection.isEmpty() || CollectionTools.set(iterator, iteratorSize).containsAll(collection);
}
/**
@@ -170,7 +172,7 @@ public final class IteratorTools {
* elements in the specified iterable.
*/
public static boolean containsAll(Iterator<?> iterator, Iterable<?> iterable) {
- return CollectionTools.containsAll(CollectionTools.set(iterator), iterable);
+ return containsAll(iterator, iterable.iterator());
}
/**
@@ -179,7 +181,7 @@ public final class IteratorTools {
* The specified iterator size is a performance hint.
*/
public static boolean containsAll(Iterator<?> iterator, int iteratorSize, Iterable<?> iterable) {
- return CollectionTools.containsAll(CollectionTools.set(iterator, iteratorSize), iterable);
+ return containsAll(iterator, iteratorSize, iterable.iterator());
}
/**
@@ -187,7 +189,7 @@ public final class IteratorTools {
* elements in the specified iterator 2.
*/
public static boolean containsAll(Iterator<?> iterator1, Iterator<?> iterator2) {
- return CollectionTools.containsAll(CollectionTools.set(iterator1), iterator2);
+ return isEmpty(iterator2) || CollectionTools.containsAll(CollectionTools.set(iterator1), iterator2);
}
/**
@@ -196,7 +198,7 @@ public final class IteratorTools {
* The specified iterator 1 size is a performance hint.
*/
public static boolean containsAll(Iterator<?> iterator1, int iterator1Size, Iterator<?> iterator2) {
- return CollectionTools.containsAll(CollectionTools.set(iterator1, iterator1Size), iterator2);
+ return isEmpty(iterator2) || CollectionTools.containsAll(CollectionTools.set(iterator1, iterator1Size), iterator2);
}
/**
@@ -204,7 +206,7 @@ public final class IteratorTools {
* elements in the specified array.
*/
public static boolean containsAll(Iterator<?> iterator, Object... array) {
- return CollectionTools.containsAll(CollectionTools.set(iterator), array);
+ return (array.length == 0) || CollectionTools.containsAll(CollectionTools.set(iterator), array);
}
/**
@@ -213,7 +215,7 @@ public final class IteratorTools {
* The specified iterator size is a performance hint.
*/
public static boolean containsAll(Iterator<?> iterator, int iteratorSize, Object... array) {
- return CollectionTools.containsAll(CollectionTools.set(iterator, iteratorSize), array);
+ return (array.length == 0) || CollectionTools.containsAll(CollectionTools.set(iterator, iteratorSize), array);
}
/**
@@ -318,9 +320,10 @@ public final class IteratorTools {
int i = 0;
while (iterator.hasNext()) {
E next = iterator.next();
- if (i++ == index) {
+ if (i == index) {
return next;
}
+ i++;
}
throw new IndexOutOfBoundsException(String.valueOf(index) + ':' + String.valueOf(i));
}
@@ -527,6 +530,9 @@ public final class IteratorTools {
* Return the iterator after it has been "sorted".
*/
public static <E> ListIterator<E> sort(Iterator<? extends E> iterator, Comparator<? super E> comparator) {
+ if (isEmpty(iterator)) {
+ return emptyListIterator();
+ }
return ListTools.sort(ListTools.list(iterator), comparator).listIterator();
}
@@ -535,6 +541,9 @@ public final class IteratorTools {
* The specified iterator size is a performance hint.
*/
public static <E> ListIterator<E> sort(Iterator<? extends E> iterator, Comparator<? super E> comparator, int iteratorSize) {
+ if (isEmpty(iterator)) {
+ return emptyListIterator();
+ }
return ListTools.sort(ListTools.list(iterator, iteratorSize), comparator).listIterator();
}
@@ -543,7 +552,7 @@ public final class IteratorTools {
* @see Collection#toArray()
*/
public static Object[] toArray(Iterator<?> iterator) {
- return list(iterator).toArray();
+ return isEmpty(iterator) ? ObjectTools.EMPTY_OBJECT_ARRAY : list(iterator).toArray();
}
/**
@@ -552,7 +561,7 @@ public final class IteratorTools {
* @see Collection#toArray()
*/
public static Object[] toArray(Iterator<?> iterator, int iteratorSize) {
- return list(iterator, iteratorSize).toArray();
+ return isEmpty(iterator) ? ObjectTools.EMPTY_OBJECT_ARRAY : list(iterator, iteratorSize).toArray();
}
/**
@@ -560,7 +569,7 @@ public final class IteratorTools {
* @see Collection#toArray(Object[])
*/
public static <E> E[] toArray(Iterator<? extends E> iterator, E[] array) {
- return list(iterator).toArray(array);
+ return isEmpty(iterator) ? ArrayTools.newInstance(array, 0) : list(iterator).toArray(array);
}
/**
@@ -569,7 +578,7 @@ public final class IteratorTools {
* @see Collection#toArray(Object[])
*/
public static <E> E[] toArray(Iterator<? extends E> iterator, int iteratorSize, E[] array) {
- return list(iterator, iteratorSize).toArray(array);
+ return isEmpty(iterator) ? ArrayTools.newInstance(array, 0) : list(iterator, iteratorSize).toArray(array);
}
@@ -580,8 +589,12 @@ public final class IteratorTools {
* in the specified iterators.
* @see SimultaneousIterator
*/
- public static <E, I extends Iterator<? extends E>> SimultaneousIterator<E> align(I... iterators) {
- return align(IterableTools.iterable(iterators), iterators.length);
+ public static <E, I extends Iterator<? extends E>> Iterator<List<E>> align(I... iterators) {
+ int len = iterators.length;
+ if (len == 0) {
+ return emptyIterator();
+ }
+ return align(IterableTools.iterable(iterators), len);
}
/**
@@ -589,8 +602,8 @@ public final class IteratorTools {
* in the specified iterables.
* @see SimultaneousIterator
*/
- public static <E, I extends Iterator<? extends E>> SimultaneousIterator<E> align(Iterable<I> iterables) {
- return new SimultaneousIterator<E>(iterables);
+ public static <E, I extends Iterator<? extends E>> Iterator<List<E>> align(Iterable<I> iterables) {
+ return align(iterables, -1);
}
/**
@@ -598,7 +611,7 @@ public final class IteratorTools {
* in the specified iterables.
* @see SimultaneousIterator
*/
- public static <E, I extends Iterator<? extends E>> SimultaneousIterator<E> align(Iterable<I> iterables, int iterablesSize) {
+ public static <E, I extends Iterator<? extends E>> Iterator<List<E>> align(Iterable<I> iterables, int iterablesSize) {
return new SimultaneousIterator<E>(iterables, iterablesSize);
}
@@ -607,8 +620,12 @@ public final class IteratorTools {
* in the specified iterators.
* @see SimultaneousListIterator
*/
- public static <E, I extends ListIterator<E>> SimultaneousListIterator<E> alignList(I... iterators) {
- return alignList(IterableTools.listIterable(iterators), iterators.length);
+ public static <E, I extends ListIterator<E>> ListIterator<List<E>> alignList(I... iterators) {
+ int len = iterators.length;
+ if (len == 0) {
+ return emptyListIterator();
+ }
+ return alignList(IterableTools.listIterable(iterators), len);
}
/**
@@ -616,8 +633,8 @@ public final class IteratorTools {
* in the specified iterables.
* @see SimultaneousListIterator
*/
- public static <E, I extends ListIterator<E>> SimultaneousListIterator<E> alignList(Iterable<I> iterables) {
- return new SimultaneousListIterator<E>(iterables);
+ public static <E, I extends ListIterator<E>> ListIterator<List<E>> alignList(Iterable<I> iterables) {
+ return alignList(iterables, -1);
}
/**
@@ -625,7 +642,7 @@ public final class IteratorTools {
* in the specified iterators.
* @see SimultaneousListIterator
*/
- public static <E, I extends ListIterator<E>> SimultaneousListIterator<E> alignList(Iterable<I> iterators, int iteratorsSize) {
+ public static <E, I extends ListIterator<E>> ListIterator<List<E>> alignList(Iterable<I> iterators, int iteratorsSize) {
return new SimultaneousListIterator<E>(iterators, iteratorsSize);
}
@@ -633,7 +650,10 @@ public final class IteratorTools {
* Return an iterator that converts the specified iterator's element type.
* @see LateralIteratorWrapper
*/
- public static <E1, E2> LateralIteratorWrapper<E1, E2> cast(Iterator<E1> iterator) {
+ public static <E1, E2> Iterator<E2> cast(Iterator<E1> iterator) {
+ if (isEmpty(iterator)) {
+ return emptyIterator();
+ }
return new LateralIteratorWrapper<E1, E2>(iterator);
}
@@ -641,7 +661,10 @@ public final class IteratorTools {
* Return a list iterator that converts the specified iterator's element type.
* @see LateralListIteratorWrapper
*/
- public static <E1, E2> LateralListIteratorWrapper<E1, E2> cast(ListIterator<E1> iterator) {
+ public static <E1, E2> ListIterator<E2> cast(ListIterator<E1> iterator) {
+ if (isEmpty(iterator)) {
+ return emptyListIterator();
+ }
return new LateralListIteratorWrapper<E1, E2>(iterator);
}
@@ -649,7 +672,10 @@ public final class IteratorTools {
* Return an iterator that converts the specified iterator's element type.
* @see SubIteratorWrapper
*/
- public static <E1, E2 extends E1> SubIteratorWrapper<E1, E2> downCast(Iterator<E1> iterator) {
+ public static <E1, E2 extends E1> Iterator<E2> downCast(Iterator<E1> iterator) {
+ if (isEmpty(iterator)) {
+ return emptyIterator();
+ }
return new SubIteratorWrapper<E1, E2>(iterator);
}
@@ -657,7 +683,10 @@ public final class IteratorTools {
* Return an iterator that converts the specified iterator's element type.
* @see SubListIteratorWrapper
*/
- public static <E1, E2 extends E1> SubListIteratorWrapper<E1, E2> downCast(ListIterator<E1> iterator) {
+ public static <E1, E2 extends E1> ListIterator<E2> downCast(ListIterator<E1> iterator) {
+ if (isEmpty(iterator)) {
+ return emptyListIterator();
+ }
return new SubListIteratorWrapper<E1, E2>(iterator);
}
@@ -666,6 +695,9 @@ public final class IteratorTools {
* @see SuperIteratorWrapper
*/
public static <E> Iterator<E> upCast(Iterator<? extends E> iterator) {
+ if (isEmpty(iterator)) {
+ return emptyIterator();
+ }
return new SuperIteratorWrapper<E>(iterator);
}
@@ -674,6 +706,9 @@ public final class IteratorTools {
* @see SuperListIteratorWrapper
*/
public static <E> ListIterator<E> upCast(ListIterator<? extends E> iterator) {
+ if (isEmpty(iterator)) {
+ return emptyListIterator();
+ }
return new SuperListIteratorWrapper<E>(iterator);
}
@@ -682,7 +717,10 @@ public final class IteratorTools {
* the specified {@link Transformer transformer}.
* @see ChainIterator
*/
- public static <E> ChainIterator<E> chainIterator(E first, Transformer<? super E, ? extends E> transformer) {
+ public static <E> Iterator<E> chainIterator(E first, Transformer<? super E, ? extends E> transformer) {
+ if (first == null) {
+ return emptyIterator();
+ }
return new ChainIterator<E>(first, transformer);
}
@@ -691,7 +729,7 @@ public final class IteratorTools {
* elements.
* @see CloneIterator
*/
- public static <E> CloneIterator<E> clone(Collection<? extends E> collection) {
+ public static <E> Iterator<E> clone(Collection<? extends E> collection) {
return clone(collection, DisabledClosure.instance());
}
@@ -700,7 +738,10 @@ public final class IteratorTools {
* elements and uses the specified {@link Closure remove closure}.
* @see CloneIterator
*/
- public static <E> CloneIterator<E> clone(Collection<? extends E> collection, Closure<? super E> removeClosure) {
+ public static <E> Iterator<E> clone(Collection<? extends E> collection, Closure<? super E> removeClosure) {
+ if (collection.isEmpty()) {
+ return emptyIterator();
+ }
return new CloneIterator<E>(collection, removeClosure);
}
@@ -709,7 +750,7 @@ public final class IteratorTools {
* elements.
* @see CloneIterator
*/
- public static <E> CloneListIterator<E> clone(List<? extends E> list) {
+ public static <E> ListIterator<E> clone(List<? extends E> list) {
return clone(list, Adapter.ReadOnly.<E>instance());
}
@@ -718,7 +759,10 @@ public final class IteratorTools {
* elements and uses the specified {@link CloneListIterator.Adapter adapter}.
* @see CloneIterator
*/
- public static <E> CloneListIterator<E> clone(List<? extends E> list, CloneListIterator.Adapter<E> adapter) {
+ public static <E> ListIterator<E> clone(List<? extends E> list, CloneListIterator.Adapter<E> adapter) {
+ if (list.isEmpty()) {
+ return emptyListIterator();
+ }
return new CloneListIterator<E>(list, adapter);
}
@@ -728,8 +772,11 @@ public final class IteratorTools {
* @see CompositeIterator
*/
@SuppressWarnings("unchecked")
- public static <E> CompositeIterator<E> add(Iterator<? extends E> iterator, E object) {
- return concatenate(iterator, singletonIterator(object));
+ public static <E> Iterator<E> add(Iterator<? extends E> iterator, E object) {
+ if (isEmpty(iterator)) {
+ return singletonIterator(object);
+ }
+ return concatenate_(iterator, singletonIterator(object));
}
/**
@@ -738,8 +785,11 @@ public final class IteratorTools {
* @see CompositeIterator
*/
@SuppressWarnings("unchecked")
- public static <E> CompositeIterator<E> insert(E object, Iterator<? extends E> iterator) {
- return concatenate(singletonIterator(object), iterator);
+ public static <E> Iterator<E> insert(E object, Iterator<? extends E> iterator) {
+ if (isEmpty(iterator)) {
+ return singletonIterator(object);
+ }
+ return concatenate_(singletonIterator(object), iterator);
}
/**
@@ -747,8 +797,23 @@ public final class IteratorTools {
* elements in the specified iterators.
* @see CompositeIterator
*/
- public static <E> CompositeIterator<E> concatenate(Iterator<? extends E>... iterators) {
- return concatenate(iterator(iterators));
+ @SuppressWarnings("unchecked")
+ public static <E> Iterator<E> concatenate(Iterator<? extends E>... iterators) {
+ int len = iterators.length;
+ if (len == 0) {
+ return emptyIterator();
+ }
+ if (len == 1) {
+ return (Iterator<E>) iterators[0];
+ }
+ return concatenate_(iterators);
+ }
+
+ /**
+ * assume the list is not empty
+ */
+ private static <E> Iterator<E> concatenate_(Iterator<? extends E>... iterators) {
+ return concatenate_(iterator(iterators));
}
/**
@@ -756,7 +821,17 @@ public final class IteratorTools {
* elements in the specified iterators.
* @see CompositeIterator
*/
- public static <E> CompositeIterator<E> concatenate(Iterator<? extends Iterator<? extends E>> iterators) {
+ public static <E> Iterator<E> concatenate(Iterator<? extends Iterator<? extends E>> iterators) {
+ if (isEmpty(iterators)) {
+ return emptyListIterator();
+ }
+ return concatenate_(iterators);
+ }
+
+ /**
+ * assume the list is not empty
+ */
+ private static <E> Iterator<E> concatenate_(Iterator<? extends Iterator<? extends E>> iterators) {
return new CompositeIterator<E>(iterators);
}
@@ -765,7 +840,7 @@ public final class IteratorTools {
* Use the specified transformer to transform each parent into its children.
* @see CompositeIterator
*/
- public static <P, E> CompositeIterator<E> children(Iterator<? extends P> parents, Transformer<? super P, ? extends Iterator<? extends E>> childrenTransformer) {
+ public static <P, E> Iterator<E> children(Iterator<? extends P> parents, Transformer<? super P, ? extends Iterator<? extends E>> childrenTransformer) {
return concatenate(transform(parents, childrenTransformer));
}
@@ -775,8 +850,11 @@ public final class IteratorTools {
* @see CompositeListIterator
*/
@SuppressWarnings("unchecked")
- public static <E> CompositeListIterator<E> add(ListIterator<E> iterator, E object) {
- return concatenate(iterator, singletonListIterator(object));
+ public static <E> ListIterator<E> add(ListIterator<E> iterator, E object) {
+ if (isEmpty(iterator)) {
+ return singletonListIterator(object);
+ }
+ return concatenate_(iterator, singletonListIterator(object));
}
/**
@@ -785,8 +863,11 @@ public final class IteratorTools {
* @see CompositeListIterator
*/
@SuppressWarnings("unchecked")
- public static <E> CompositeListIterator<E> insert(E object, ListIterator<E> iterator) {
- return concatenate(singletonListIterator(object), iterator);
+ public static <E> ListIterator<E> insert(E object, ListIterator<E> iterator) {
+ if (isEmpty(iterator)) {
+ return singletonListIterator(object);
+ }
+ return concatenate_(singletonListIterator(object), iterator);
}
/**
@@ -794,8 +875,22 @@ public final class IteratorTools {
* elements in the specified iterators.
* @see CompositeListIterator
*/
- public static <E> CompositeListIterator<E> concatenate(ListIterator<E>... iterators) {
- return concatenate(listIterator(iterators));
+ public static <E> ListIterator<E> concatenate(ListIterator<E>... iterators) {
+ int len = iterators.length;
+ if (len == 0) {
+ return emptyListIterator();
+ }
+ if (len == 1) {
+ return iterators[0];
+ }
+ return concatenate_(iterators);
+ }
+
+ /**
+ * assume the list is not empty
+ */
+ private static <E> ListIterator<E> concatenate_(ListIterator<E>... iterators) {
+ return concatenate_(listIterator(iterators));
}
/**
@@ -803,7 +898,17 @@ public final class IteratorTools {
* elements in the specified iterators.
* @see CompositeListIterator
*/
- public static <E> CompositeListIterator<E> concatenate(ListIterator<? extends ListIterator<E>> iterators) {
+ public static <E> ListIterator<E> concatenate(ListIterator<? extends ListIterator<E>> iterators) {
+ if (isEmpty(iterators)) {
+ return emptyListIterator();
+ }
+ return concatenate_(iterators);
+ }
+
+ /**
+ * assume the list is not empty
+ */
+ private static <E> ListIterator<E> concatenate_(ListIterator<? extends ListIterator<E>> iterators) {
return new CompositeListIterator<E>(iterators);
}
@@ -812,7 +917,7 @@ public final class IteratorTools {
* Use the specified transformer to transform each parent into its children.
* @see CompositeListIterator
*/
- public static <P, E> CompositeListIterator<E> children(ListIterator<? extends P> parents, Transformer<? super P, ? extends ListIterator<E>> childrenTransformer) {
+ public static <P, E> ListIterator<E> children(ListIterator<? extends P> parents, Transformer<? super P, ? extends ListIterator<E>> childrenTransformer) {
return concatenate(transform(parents, childrenTransformer));
}
@@ -822,8 +927,11 @@ public final class IteratorTools {
* @see ReadOnlyCompositeListIterator
*/
@SuppressWarnings("unchecked")
- public static <E> ReadOnlyCompositeListIterator<E> addReadOnly(ListIterator<? extends E> iterator, E object) {
- return concatenateReadOnly(iterator, singletonListIterator(object));
+ public static <E> ListIterator<E> addReadOnly(ListIterator<? extends E> iterator, E object) {
+ if (isEmpty(iterator)) {
+ return singletonListIterator(object);
+ }
+ return concatenateReadOnly_(iterator, singletonListIterator(object));
}
/**
@@ -832,8 +940,11 @@ public final class IteratorTools {
* @see ReadOnlyCompositeListIterator
*/
@SuppressWarnings("unchecked")
- public static <E> ReadOnlyCompositeListIterator<E> insertReadOnly(E object, ListIterator<? extends E> iterator) {
- return concatenateReadOnly(singletonListIterator(object), iterator);
+ public static <E> ListIterator<E> insertReadOnly(E object, ListIterator<? extends E> iterator) {
+ if (isEmpty(iterator)) {
+ return singletonListIterator(object);
+ }
+ return concatenateReadOnly_(singletonListIterator(object), iterator);
}
/**
@@ -841,8 +952,23 @@ public final class IteratorTools {
* elements in the specified iterators.
* @see ReadOnlyCompositeListIterator
*/
- public static <E> ReadOnlyCompositeListIterator<E> concatenateReadOnly(ListIterator<? extends E>... iterators) {
- return concatenateReadOnly(listIterator(iterators));
+ @SuppressWarnings("unchecked")
+ public static <E> ListIterator<E> concatenateReadOnly(ListIterator<? extends E>... iterators) {
+ int len = iterators.length;
+ if (len == 0) {
+ return emptyListIterator();
+ }
+ if (len == 1) {
+ return (ListIterator<E>) iterators[0];
+ }
+ return concatenateReadOnly_(iterators);
+ }
+
+ /**
+ * assume the list is not empty
+ */
+ private static <E> ListIterator<E> concatenateReadOnly_(ListIterator<? extends E>... iterators) {
+ return concatenateReadOnly_(listIterator(iterators));
}
/**
@@ -850,7 +976,17 @@ public final class IteratorTools {
* elements in the specified iterators.
* @see ReadOnlyCompositeListIterator
*/
- public static <E> ReadOnlyCompositeListIterator<E> concatenateReadOnly(ListIterator<? extends ListIterator<? extends E>> iterators) {
+ public static <E> ListIterator<E> concatenateReadOnly(ListIterator<? extends ListIterator<? extends E>> iterators) {
+ if (isEmpty(iterators)) {
+ return emptyListIterator();
+ }
+ return concatenateReadOnly_(iterators);
+ }
+
+ /**
+ * assume the list is not empty
+ */
+ private static <E> ListIterator<E> concatenateReadOnly_(ListIterator<? extends ListIterator<? extends E>> iterators) {
return new ReadOnlyCompositeListIterator<E>(iterators);
}
@@ -859,7 +995,7 @@ public final class IteratorTools {
* Use the specified transformer to transform each parent into its children.
* @see ReadOnlyCompositeListIterator
*/
- public static <P, E> ReadOnlyCompositeListIterator<E> readOnlyChildren(ListIterator<? extends P> parents, Transformer<? super P, ? extends ListIterator<? extends E>> childrenTransformer) {
+ public static <P, E> ListIterator<E> readOnlyChildren(ListIterator<? extends P> parents, Transformer<? super P, ? extends ListIterator<? extends E>> childrenTransformer) {
return concatenateReadOnly(transform(parents, childrenTransformer));
}
@@ -882,7 +1018,10 @@ public final class IteratorTools {
* elements in the specified iterator.
* @see FilteringIterator
*/
- public static <E> FilteringIterator<E> filter(Iterator<? extends E> iterator, Predicate<? super E> predicate) {
+ public static <E> Iterator<E> filter(Iterator<? extends E> iterator, Predicate<? super E> predicate) {
+ if (isEmpty(iterator)) {
+ return emptyIterator();
+ }
return new FilteringIterator<E>(iterator, predicate);
}
@@ -891,7 +1030,7 @@ public final class IteratorTools {
* elements in the specified iterator.
* @see FilteringIterator
*/
- public static <E> FilteringIterator<E> removeNulls(Iterator<? extends E> iterator) {
+ public static <E> Iterator<E> removeNulls(Iterator<? extends E> iterator) {
return filter(iterator, PredicateTools.isNotNull());
}
@@ -900,8 +1039,8 @@ public final class IteratorTools {
* by its children etc. as determined by the specified transformer.
* @see GraphIterator
*/
- public static <E> GraphIterator<E> graphIterator(E root, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
- return graphIterator(singletonIterator(root), transformer);
+ public static <E> Iterator<E> graphIterator(E root, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
+ return graphIterator_(singletonIterator(root), transformer);
}
/**
@@ -909,8 +1048,11 @@ public final class IteratorTools {
* by their children etc. as determined by the specified transformer.
* @see GraphIterator
*/
- public static <E> GraphIterator<E> graphIterator(E[] roots, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
- return graphIterator(iterator(roots), transformer);
+ public static <E> Iterator<E> graphIterator(E[] roots, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
+ if (roots.length == 0) {
+ return emptyIterator();
+ }
+ return graphIterator_(iterator(roots), transformer);
}
/**
@@ -918,21 +1060,34 @@ public final class IteratorTools {
* by their children etc. as determined by the specified transformer.
* @see GraphIterator
*/
- public static <E> GraphIterator<E> graphIterator(Iterator<? extends E> roots, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
+ public static <E> Iterator<E> graphIterator(Iterator<? extends E> roots, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
+ if (isEmpty(roots)) {
+ return emptyIterator();
+ }
+ return graphIterator_(roots, transformer);
+ }
+
+ /**
+ * assume roots are present
+ */
+ private static <E> Iterator<E> graphIterator_(Iterator<? extends E> roots, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
return new GraphIterator<E>(roots, transformer);
}
/**
* Return an iterator the corresponds to the specified enumeration.
*/
- public static <E> EnumerationIterator<E> iterator(Enumeration<E> enumeration) {
+ public static <E> Iterator<E> iterator(Enumeration<E> enumeration) {
+ if (EnumerationTools.isEmpty(enumeration)) {
+ return emptyIterator();
+ }
return new EnumerationIterator<E>(enumeration);
}
/**
* Return an iterator on the elements in the specified array.
*/
- public static <E> ArrayIterator<E> iterator(E... array) {
+ public static <E> Iterator<E> iterator(E... array) {
return iterator(array, 0);
}
@@ -940,7 +1095,7 @@ public final class IteratorTools {
* Return an iterator on the elements in the specified array
* starting at the specified position in the array.
*/
- public static <E> ArrayIterator<E> iterator(E[] array, int start) {
+ public static <E> Iterator<E> iterator(E[] array, int start) {
return iterator(array, start, array.length);
}
@@ -949,7 +1104,10 @@ public final class IteratorTools {
* starting at the specified start index, inclusive, and continuing to
* the specified end index, exclusive.
*/
- public static <E> ArrayIterator<E> iterator(E[] array, int start, int end) {
+ public static <E> Iterator<E> iterator(E[] array, int start, int end) {
+ if (start == end) {
+ return emptyIterator();
+ }
return new ArrayIterator<E>(array, start, end);
}
@@ -957,7 +1115,7 @@ public final class IteratorTools {
* Return an iterator on the specified queue.
* @see Queue
*/
- public static <E> QueueIterator<E> iterator(Queue<? extends E> queue) {
+ public static <E> Iterator<E> iterator(Queue<? extends E> queue) {
return new QueueIterator<E>(queue);
}
@@ -965,14 +1123,14 @@ public final class IteratorTools {
* Return an iterator on the specified stack.
* @see Stack
*/
- public static <E> StackIterator<E> iterator(Stack<? extends E> stack) {
+ public static <E> Iterator<E> iterator(Stack<? extends E> stack) {
return new StackIterator<E>(stack);
}
/**
* Return a list iterator for the specified array.
*/
- public static <E> ArrayListIterator<E> listIterator(E... array) {
+ public static <E> ListIterator<E> listIterator(E... array) {
return listIterator(array, 0);
}
@@ -980,7 +1138,7 @@ public final class IteratorTools {
* Return a list iterator for the specified array
* starting at the specified position in the array.
*/
- public static <E> ArrayListIterator<E> listIterator(E[] array, int start) {
+ public static <E> ListIterator<E> listIterator(E[] array, int start) {
return listIterator(array, start, array.length);
}
@@ -989,7 +1147,10 @@ public final class IteratorTools {
* starting at the specified start index, inclusive, and continuing to
* the specified end index, exclusive.
*/
- public static <E> ArrayListIterator<E> listIterator(E[] array, int start, int end) {
+ public static <E> ListIterator<E> listIterator(E[] array, int start, int end) {
+ if (start == end) {
+ return emptyListIterator();
+ }
return new ArrayListIterator<E>(array, start, end);
}
@@ -997,7 +1158,10 @@ public final class IteratorTools {
* Return an iterator the returns <code>null</code> the specified number of times.
* @see NullElementIterator
*/
- public static <E> NullElementIterator<E> nullElementIterator(int size) {
+ public static <E> Iterator<E> nullElementIterator(int size) {
+ if (size == 0) {
+ return emptyIterator();
+ }
return new NullElementIterator<E>(size);
}
@@ -1005,7 +1169,10 @@ public final class IteratorTools {
* Return a list iterator the returns <code>null</code> the specified number of times.
* @see NullElementListIterator
*/
- public static <E> NullElementListIterator<E> nullElementListIterator(int size) {
+ public static <E> ListIterator<E> nullElementListIterator(int size) {
+ if (size == 0) {
+ return emptyListIterator();
+ }
return new NullElementListIterator<E>(size);
}
@@ -1020,7 +1187,10 @@ public final class IteratorTools {
* Convert the specified iterator to read-only.
* @see ReadOnlyIterator
*/
- public static <E> ReadOnlyIterator<E> readOnly(Iterator<? extends E> iterator) {
+ public static <E> Iterator<E> readOnly(Iterator<? extends E> iterator) {
+ if (isEmpty(iterator)) {
+ return emptyIterator();
+ }
return new ReadOnlyIterator<E>(iterator);
}
@@ -1028,7 +1198,10 @@ public final class IteratorTools {
* Convert the specified iterator to read-only.
* @see ReadOnlyListIterator
*/
- public static <E> ReadOnlyListIterator<E> readOnly(ListIterator<? extends E> iterator) {
+ public static <E> ListIterator<E> readOnly(ListIterator<? extends E> iterator) {
+ if (isEmpty(iterator)) {
+ return emptyListIterator();
+ }
return new ReadOnlyListIterator<E>(iterator);
}
@@ -1037,7 +1210,10 @@ public final class IteratorTools {
* of times.
* @see RepeatingElementIterator
*/
- public static <E> RepeatingElementIterator<E> repeatingElementIterator(E element, int size) {
+ public static <E> Iterator<E> repeatingElementIterator(E element, int size) {
+ if (size == 0) {
+ return emptyIterator();
+ }
return new RepeatingElementIterator<E>(element, size);
}
@@ -1046,7 +1222,10 @@ public final class IteratorTools {
* of times.
* @see RepeatingElementIterator
*/
- public static <E> RepeatingElementListIterator<E> repeatingElementListIterator(E element, int size) {
+ public static <E> ListIterator<E> repeatingElementListIterator(E element, int size) {
+ if (size == 0) {
+ return emptyListIterator();
+ }
return new RepeatingElementListIterator<E>(element, size);
}
@@ -1054,7 +1233,10 @@ public final class IteratorTools {
* Return an iterator that returns the objects in the specified iterator
* in reverse order.
*/
- public static <E> ReverseIterator<E> reverse(Iterator<? extends E> iterator) {
+ public static <E> Iterator<E> reverse(Iterator<? extends E> iterator) {
+ if (isEmpty(iterator)) {
+ return emptyIterator();
+ }
return new ReverseIterator<E>(iterator);
}
@@ -1063,7 +1245,10 @@ public final class IteratorTools {
* in reverse order.
* The specified iterator size is a performance hint.
*/
- public static <E> ReverseIterator<E> reverse(Iterator<? extends E> iterator, int iteratorSize) {
+ public static <E> Iterator<E> reverse(Iterator<? extends E> iterator, int iteratorSize) {
+ if (isEmpty(iterator)) {
+ return emptyIterator();
+ }
return new ReverseIterator<E>(iterator, iteratorSize);
}
@@ -1072,7 +1257,7 @@ public final class IteratorTools {
* specified object.
* @see SingleElementIterator
*/
- public static <E> SingleElementIterator<E> singletonIterator(E value) {
+ public static <E> Iterator<E> singletonIterator(E value) {
return new SingleElementIterator<E>(value);
}
@@ -1081,7 +1266,7 @@ public final class IteratorTools {
* specified object.
* @see SingleElementListIterator
*/
- public static <E> SingleElementListIterator<E> singletonListIterator(E value) {
+ public static <E> ListIterator<E> singletonListIterator(E value) {
return new SingleElementListIterator<E>(value);
}
@@ -1089,7 +1274,10 @@ public final class IteratorTools {
* Return an iterator that synchronizes the specified iterator on itself.
* @see SynchronizedIterator
*/
- public static <E> SynchronizedIterator<E> synchronize(Iterator<? extends E> iterator) {
+ public static <E> Iterator<E> synchronize(Iterator<? extends E> iterator) {
+ if (isEmpty(iterator)) {
+ return emptyIterator();
+ }
return new SynchronizedIterator<E>(iterator);
}
@@ -1098,7 +1286,10 @@ public final class IteratorTools {
* specified mutex.
* @see SynchronizedIterator
*/
- public static <E> SynchronizedIterator<E> synchronize(Iterator<? extends E> iterator, Object mutex) {
+ public static <E> Iterator<E> synchronize(Iterator<? extends E> iterator, Object mutex) {
+ if (isEmpty(iterator)) {
+ return emptyIterator();
+ }
return new SynchronizedIterator<E>(iterator, mutex);
}
@@ -1106,7 +1297,10 @@ public final class IteratorTools {
* Return an iterator that synchronizes the specified iterator on itself.
* @see SynchronizedListIterator
*/
- public static <E> SynchronizedListIterator<E> synchronize(ListIterator<E> iterator) {
+ public static <E> ListIterator<E> synchronize(ListIterator<E> iterator) {
+ if (isEmpty(iterator)) {
+ return emptyListIterator();
+ }
return new SynchronizedListIterator<E>(iterator);
}
@@ -1115,7 +1309,10 @@ public final class IteratorTools {
* specified mutex.
* @see SynchronizedListIterator
*/
- public static <E> SynchronizedListIterator<E> synchronize(ListIterator<E> iterator, Object mutex) {
+ public static <E> ListIterator<E> synchronize(ListIterator<E> iterator, Object mutex) {
+ if (isEmpty(iterator)) {
+ return emptyListIterator();
+ }
return new SynchronizedListIterator<E>(iterator, mutex);
}
@@ -1124,7 +1321,10 @@ public final class IteratorTools {
* elements in the specified iterator.
* @see TransformationIterator
*/
- public static <E1, E2> TransformationIterator<E1, E2> transform(Iterator<? extends E1> iterator, Transformer<? super E1, ? extends E2> transformer) {
+ public static <E1, E2> Iterator<E2> transform(Iterator<? extends E1> iterator, Transformer<? super E1, ? extends E2> transformer) {
+ if (isEmpty(iterator)) {
+ return emptyIterator();
+ }
return new TransformationIterator<E1, E2>(iterator, transformer);
}
@@ -1133,7 +1333,10 @@ public final class IteratorTools {
* elements in the specified iterator.
* @see TransformationListIterator
*/
- public static <E1, E2> TransformationListIterator<E1, E2> transform(ListIterator<? extends E1> iterator, Transformer<? super E1, ? extends E2> transformer) {
+ public static <E1, E2> ListIterator<E2> transform(ListIterator<? extends E1> iterator, Transformer<? super E1, ? extends E2> transformer) {
+ if (isEmpty(iterator)) {
+ return emptyListIterator();
+ }
return new TransformationListIterator<E1, E2>(iterator, transformer);
}
@@ -1142,8 +1345,8 @@ public final class IteratorTools {
* with the specified root and transformer.
* @see TreeIterator
*/
- public static <E> TreeIterator<E> treeIterator(E root, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
- return treeIterator(singletonIterator(root), transformer);
+ public static <E> Iterator<E> treeIterator(E root, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
+ return treeIterator_(singletonIterator(root), transformer);
}
/**
@@ -1151,8 +1354,11 @@ public final class IteratorTools {
* with the specified roots and transformer.
* @see TreeIterator
*/
- public static <E> TreeIterator<E> treeIterator(E[] roots, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
- return treeIterator(iterator(roots), transformer);
+ public static <E> Iterator<E> treeIterator(E[] roots, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
+ if (roots.length == 0) {
+ return emptyIterator();
+ }
+ return treeIterator_(iterator(roots), transformer);
}
/**
@@ -1160,10 +1366,33 @@ public final class IteratorTools {
* with the specified roots and transformer.
* @see TreeIterator
*/
- public static <E> TreeIterator<E> treeIterator(Iterator<? extends E> roots, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
+ public static <E> Iterator<E> treeIterator(Iterator<? extends E> roots, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
+ if (isEmpty(roots)) {
+ return emptyIterator();
+ }
+ return treeIterator_(roots, transformer);
+ }
+
+ /**
+ * assume roots are present
+ */
+ private static <E> Iterator<E> treeIterator_(Iterator<? extends E> roots, Transformer<? super E, ? extends Iterator<? extends E>> transformer) {
return new TreeIterator<E>(roots, transformer);
}
+ /**
+ * Return a string representation of the specified iterator.
+ */
+ public static String toString(Iterator<?> iterator) {
+ StringBuilder sb = new StringBuilder();
+ sb.append('[');
+ while (iterator.hasNext()) {
+ sb.append(iterator.next());
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
// ********** constructor **********

Back to the top