001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.foundation;
022:
023: /**
024: * Iterator primitives (concat, map, reduce, filter, etc...).
025: *
026: * @exclude
027: */
028: public class Iterators {
029:
030: public static final Iterator4 EMPTY_ITERATOR = new Iterator4Impl(
031: null);
032:
033: public static final Iterable4 EMPTY_ITERABLE = new Iterable4() {
034: public Iterator4 iterator() {
035: return EMPTY_ITERATOR;
036: }
037: };
038:
039: static final Object NO_ELEMENT = new Object();
040:
041: public static Iterator4 concat(Iterator4 iterators) {
042: return new CompositeIterator4(iterators);
043: }
044:
045: public static Iterator4 map(Iterator4 iterator, Function4 function) {
046: return new FunctionApplicationIterator(iterator, function);
047: }
048:
049: public static Iterator4 map(Object[] array, Function4 function) {
050: return map(new ArrayIterator4(array), function);
051: }
052:
053: public static Iterator4 filter(Object[] array, Predicate4 predicate) {
054: return filter(new ArrayIterator4(array), predicate);
055: }
056:
057: public static Iterator4 filter(Iterator4 iterator,
058: Predicate4 predicate) {
059: return new FilteredIterator(iterator, predicate);
060: }
061:
062: public static Iterator4 iterate(Object[] array) {
063: return new ArrayIterator4(array);
064: }
065:
066: public static int size(Iterable4 iterable) {
067: return size(iterable.iterator());
068: }
069:
070: public static Object next(Iterator4 iterator) {
071: if (!iterator.moveNext()) {
072: throw new IllegalStateException();
073: }
074: return iterator.current();
075: }
076:
077: private static int size(Iterator4 iterator) {
078: int count = 0;
079: while (iterator.moveNext()) {
080: ++count;
081: }
082: return count;
083: }
084:
085: public static String toString(Iterator4 i) {
086: if (!i.moveNext()) {
087: return "[]";
088: }
089: StringBuffer sb = new StringBuffer();
090: sb.append("[");
091: sb.append(i.current());
092:
093: while (i.moveNext()) {
094: sb.append(", ");
095: sb.append(i.current());
096: }
097: sb.append("]");
098: return sb.toString();
099: }
100: }
|