001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.ontoware.rdf2go.util;
007:
008: import java.util.Collection;
009: import java.util.Iterator;
010:
011: import org.ontoware.aifbcommons.collection.ClosableIterator;
012:
013: /**
014: * This class consists exclusively of static methods that operate on or return
015: * iterators. It is the Iterator-equivalent of <tt>java.util.Collections</tt>.
016: *
017: * copied into RDF2Go by Leo Sauemann on 11.1.2007,
018: * it helps to get working quicker.
019: */
020: public class Iterators {
021:
022: /**
023: * Adds all elements from the supplied iterator to the specified collection. If the
024: * supplied iterator is an instance of {@link CloseableIterator} it is automatically
025: * closed after consumption.
026: *
027: * @param iter
028: * An iterator containing elements to add to the container. If the iterator
029: * is an instance of {@link CloseableIterator} it is automatically closed
030: * after consumption.
031: * @param collection
032: * The collection to add the elements to.
033: * @return The <tt>collection</tt> object that was supplied to this method.
034: */
035: public static <E, C extends Collection<E>> C addAll(
036: Iterator<? extends E> iter, C collection) {
037: try {
038: while (iter.hasNext()) {
039: collection.add(iter.next());
040: }
041: } finally {
042: closeCloseable(iter);
043: }
044:
045: return collection;
046: }
047:
048: /**
049: * Closes the supplied iterator if it is an instance of
050: * {@link CloseableIterator}, otherwise the request is ignored.
051: *
052: * @param iter
053: * The iterator that should be closed.
054: */
055: public static void closeCloseable(Iterator<?> iter) {
056: if (iter instanceof ClosableIterator<?>) {
057: ((ClosableIterator<?>) iter).close();
058: }
059: }
060:
061: /**
062: * Converts an iterator to a string by concatenating all of the string
063: * representations of objects in the iterator, divided by a separator.
064: *
065: * @param iter
066: * An iterator over arbitrary objects that are expected to implement
067: * {@link Object#toString()}.
068: * @param separator
069: * The separator to insert between the object strings.
070: * @return A String representation of the objects provided by the supplied
071: * iterator.
072: */
073: public static String toString(Iterator<?> iter, String separator) {
074: StringBuilder sb = new StringBuilder();
075: toString(iter, separator, sb);
076: return sb.toString();
077: }
078:
079: /**
080: * Converts an iterator to a string by concatenating all of the string
081: * representations of objects in the iterator, divided by a separator.
082: *
083: * @param iter
084: * An iterator over arbitrary objects that are expected to implement
085: * {@link Object#toString()}.
086: * @param separator
087: * The separator to insert between the object strings.
088: * @param sb
089: * A StringBuilder to append the iterator string to.
090: */
091: public static void toString(Iterator<?> iter, String separator,
092: StringBuilder sb) {
093: while (iter.hasNext()) {
094: sb.append(iter.next());
095:
096: if (iter.hasNext()) {
097: sb.append(separator);
098: }
099: }
100: }
101:
102: /**
103: * Runs through the iterator and returns number of items. It doesn't close it.
104: *
105: * @author clemente
106: *
107: * @param it Any type of iterator, not null
108: * @returns Number of items
109: */
110:
111: public static int count(Iterator<?> it) {
112: assert it != null;
113:
114: int count = 0;
115: while (it.hasNext()) {
116: count++;
117: it.next();
118: }
119:
120: return count;
121:
122: }
123:
124: }
|