org.grouplens.common.cursors.Cursors Maven / Gradle / Ivy
/*
* GroupLens Common Utilities
* Copyright © 2011 Regents of the University of Minnesota
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of the University of Minnesota nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.grouplens.common.cursors;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import com.google.common.base.Predicate;
/**
* Utility methods for cursors.
* @author Michael Ekstrand
*
*/
public class Cursors {
/**
* Wrap an iterator in a cursor.
* @param The type of data to return.
* @param iterator An iterator to wrap
* @return A cursor returning the elements of the iterator.
*/
public static Cursor wrap(Iterator iterator) {
return new IteratorCursor(iterator);
}
/**
* Wrap a collection in a cursor.
* @param The type of data to return.
* @param collection A collection to wrap
* @return A cursor returning the elements of the collection.
*/
public static Cursor wrap(Collection collection) {
return new CollectionCursor(collection);
}
/**
* Filter a cursor.
* @param The type of cursor rows.
* @param cursor The source cursor.
* @param predicate A predicate indicating which rows to return.
* @return A cursor returning all rows for which predicate returns
* true.
*/
public static Cursor filter(Cursor cursor, Predicate predicate) {
return new FilteredCursor(cursor, predicate);
}
/**
* Create an empty cursor.
*/
public static Cursor empty() {
return new AbstractCursor() {
@Override
public int getRowCount() {
return 0;
}
@Override
public boolean hasNext() {
return false;
}
@Override
public T next() {
throw new NoSuchElementException();
}
};
}
/**
* Read a cursor into a list, closing when it is finished.
* @param The type of item in the cursor.
* @param cursor The cursor.
* @return A list containing the elements of the cursor.
*/
public static ArrayList makeList(Cursor cursor) {
ArrayList list;
try {
int n = cursor.getRowCount();
if (n < 0) n = 10;
list = new ArrayList(n);
for (T item: cursor) {
list.add(item);
}
} finally {
cursor.close();
}
list.trimToSize();
return list;
}
}