📄 iteratorutils.java
字号:
* @param transform the transform to use, not null
* @return a new transforming iterator
* @throws NullPointerException if either parameter is null
*/
public static Iterator transformedIterator(Iterator iterator, Transformer transform) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
if (transform == null) {
throw new NullPointerException("Transformer must not be null");
}
return new TransformIterator(iterator, transform);
}
// Filtered
//-----------------------------------------------------------------------
/**
* Gets an iterator that filters another iterator.
* <p>
* The returned iterator will only return objects that match the specified
* filtering predicate.
*
* @param iterator the iterator to use, not null
* @param predicate the predicate to use as a filter, not null
* @return a new filtered iterator
* @throws NullPointerException if either parameter is null
*/
public static Iterator filteredIterator(Iterator iterator, Predicate predicate) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
if (predicate == null) {
throw new NullPointerException("Predicate must not be null");
}
return new FilterIterator(iterator, predicate);
}
/**
* Gets a list iterator that filters another list iterator.
* <p>
* The returned iterator will only return objects that match the specified
* filtering predicate.
*
* @param listIterator the list iterator to use, not null
* @param predicate the predicate to use as a filter, not null
* @return a new filtered iterator
* @throws NullPointerException if either parameter is null
*/
public static ListIterator filteredListIterator(ListIterator listIterator, Predicate predicate) {
if (listIterator == null) {
throw new NullPointerException("ListIterator must not be null");
}
if (predicate == null) {
throw new NullPointerException("Predicate must not be null");
}
return new FilterListIterator(listIterator, predicate);
}
// Looping
//-----------------------------------------------------------------------
/**
* Gets an iterator that loops continuously over the supplied collection.
* <p>
* The iterator will only stop looping if the remove method is called
* enough times to empty the collection, or if the collection is empty
* to start with.
*
* @param coll the collection to iterate over, not null
* @return a new looping iterator
* @throws NullPointerException if the collection is null
*/
public static ResettableIterator loopingIterator(Collection coll) {
if (coll == null) {
throw new NullPointerException("Collection must not be null");
}
return new LoopingIterator(coll);
}
/**
* Gets an iterator that loops continuously over the supplied list.
* <p>
* The iterator will only stop looping if the remove method is called
* enough times to empty the list, or if the list is empty to start with.
*
* @param list the list to iterate over, not null
* @return a new looping iterator
* @throws NullPointerException if the list is null
* @since Commons Collections 3.2
*/
public static ResettableListIterator loopingListIterator(List list) {
if (list == null) {
throw new NullPointerException("List must not be null");
}
return new LoopingListIterator(list);
}
// Views
//-----------------------------------------------------------------------
/**
* Gets an iterator that provides an iterator view of the given enumeration.
*
* @param enumeration the enumeration to use
* @return a new iterator
*/
public static Iterator asIterator(Enumeration enumeration) {
if (enumeration == null) {
throw new NullPointerException("Enumeration must not be null");
}
return new EnumerationIterator(enumeration);
}
/**
* Gets an iterator that provides an iterator view of the given enumeration
* that will remove elements from the specified collection.
*
* @param enumeration the enumeration to use
* @param removeCollection the collection to remove elements from
* @return a new iterator
*/
public static Iterator asIterator(Enumeration enumeration, Collection removeCollection) {
if (enumeration == null) {
throw new NullPointerException("Enumeration must not be null");
}
if (removeCollection == null) {
throw new NullPointerException("Collection must not be null");
}
return new EnumerationIterator(enumeration, removeCollection);
}
/**
* Gets an enumeration that wraps an iterator.
*
* @param iterator the iterator to use, not null
* @return a new enumeration
* @throws NullPointerException if iterator is null
*/
public static Enumeration asEnumeration(Iterator iterator) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
return new IteratorEnumeration(iterator);
}
/**
* Gets a list iterator based on a simple iterator.
* <p>
* As the wrapped Iterator is traversed, a LinkedList of its values is
* cached, permitting all required operations of ListIterator.
*
* @param iterator the iterator to use, not null
* @return a new iterator
* @throws NullPointerException if iterator parameter is null
*/
public static ListIterator toListIterator(Iterator iterator) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
return new ListIteratorWrapper(iterator);
}
/**
* Gets an array based on an iterator.
* <p>
* As the wrapped Iterator is traversed, an ArrayList of its values is
* created. At the end, this is converted to an array.
*
* @param iterator the iterator to use, not null
* @return an array of the iterator contents
* @throws NullPointerException if iterator parameter is null
*/
public static Object[] toArray(Iterator iterator) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
List list = toList(iterator, 100);
return list.toArray();
}
/**
* Gets an array based on an iterator.
* <p>
* As the wrapped Iterator is traversed, an ArrayList of its values is
* created. At the end, this is converted to an array.
*
* @param iterator the iterator to use, not null
* @param arrayClass the class of array to create
* @return an array of the iterator contents
* @throws NullPointerException if iterator parameter is null
* @throws NullPointerException if arrayClass is null
* @throws ClassCastException if the arrayClass is invalid
*/
public static Object[] toArray(Iterator iterator, Class arrayClass) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
if (arrayClass == null) {
throw new NullPointerException("Array class must not be null");
}
List list = toList(iterator, 100);
return list.toArray((Object[]) Array.newInstance(arrayClass, list.size()));
}
/**
* Gets a list based on an iterator.
* <p>
* As the wrapped Iterator is traversed, an ArrayList of its values is
* created. At the end, the list is returned.
*
* @param iterator the iterator to use, not null
* @return a list of the iterator contents
* @throws NullPointerException if iterator parameter is null
*/
public static List toList(Iterator iterator) {
return toList(iterator, 10);
}
/**
* Gets a list based on an iterator.
* <p>
* As the wrapped Iterator is traversed, an ArrayList of its values is
* created. At the end, the list is returned.
*
* @param iterator the iterator to use, not null
* @param estimatedSize the initial size of the ArrayList
* @return a list of the iterator contents
* @throws NullPointerException if iterator parameter is null
* @throws IllegalArgumentException if the size is less than 1
*/
public static List toList(Iterator iterator, int estimatedSize) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
if (estimatedSize < 1) {
throw new IllegalArgumentException("Estimated size must be greater than 0");
}
List list = new ArrayList(estimatedSize);
while (iterator.hasNext()) {
list.add(iterator.next());
}
return list;
}
/**
* Gets a suitable Iterator for the given object.
* <p>
* This method can handles objects as follows
* <ul>
* <li>null - empty iterator
* <li>Iterator - returned directly
* <li>Enumeration - wrapped
* <li>Collection - iterator from collection returned
* <li>Map - values iterator returned
* <li>Dictionary - values (elements) enumeration returned as iterator
* <li>array - iterator over array returned
* <li>object with iterator() public method accessed by reflection
* <li>object - singleton iterator
* </ul>
*
* @param obj the object to convert to an iterator
* @return a suitable iterator, never null
*/
public static Iterator getIterator(Object obj) {
if (obj == null) {
return emptyIterator();
} else if (obj instanceof Iterator) {
return (Iterator) obj;
} else if (obj instanceof Collection) {
return ((Collection) obj).iterator();
} else if (obj instanceof Object[]) {
return new ObjectArrayIterator((Object[]) obj);
} else if (obj instanceof Enumeration) {
return new EnumerationIterator((Enumeration) obj);
} else if (obj instanceof Map) {
return ((Map) obj).values().iterator();
} else if (obj instanceof Dictionary) {
return new EnumerationIterator(((Dictionary) obj).elements());
} else if (obj != null && obj.getClass().isArray()) {
return new ArrayIterator(obj);
} else {
try {
Method method = obj.getClass().getMethod("iterator", (Class[]) null);
if (Iterator.class.isAssignableFrom(method.getReturnType())) {
Iterator it = (Iterator) method.invoke(obj, (Object[]) null);
if (it != null) {
return it;
}
}
} catch (Exception ex) {
// ignore
}
return singletonIterator(obj);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -