001: /*
002: * Distributed as part of debuggen v.0.1.0
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v1.util;
024:
025: import java.util.*;
026: import java.lang.reflect.Array;
027:
028: public final class IteratorUtils {
029: public final static Iterator EMPTY_ITERATOR = new Iterator() {
030: public boolean hasNext() {
031: return false;
032: }
033:
034: public Object next() {
035: throw new NoSuchElementException();
036: }
037:
038: public void remove() {
039: throw new IllegalStateException();
040: }
041: };
042:
043: public static Iterator oneElementUnmodifiableIterator(
044: final Object elem) {
045: return new Iterator() {
046: boolean shot = false;
047:
048: public boolean hasNext() {
049: return (!shot);
050: }
051:
052: public Object next() {
053: if (shot)
054: throw new NoSuchElementException();
055: else {
056: shot = true;
057: return elem;
058: }
059: }
060:
061: public void remove() {
062: throw new UnsupportedOperationException(
063: "remove() not supported.");
064: }
065: };
066: }
067:
068: public static boolean equivalent(Iterator ii, Iterator jj) {
069: while (true) {
070: boolean ii_hasnext = ii.hasNext();
071: boolean jj_hasnext = jj.hasNext();
072: if (ii_hasnext ^ jj_hasnext)
073: return false;
074: else if (ii_hasnext) {
075: Object iiNext = ii.next();
076: Object jjNext = jj.next();
077: if (iiNext == jjNext)
078: continue;
079: else if (iiNext == null)
080: return false;
081: else if (!iiNext.equals(jjNext))
082: return false;
083: } else
084: return true;
085: }
086: }
087:
088: public static ArrayList toArrayList(Iterator ii,
089: int initial_capacity) {
090: ArrayList out = new ArrayList(initial_capacity);
091: while (ii.hasNext())
092: out.add(ii.next());
093: return out;
094: }
095:
096: /**
097: * Fills an array with the contents of an iterator. If the array is too small,
098: * it will contain the first portion of the iterator. If the array can contain
099: * more elements than the iterator, extra elements are left untouched, unless
100: * null_terminate is set to true, in which case the element immediately following
101: * the last from the iterator is set to null. (This method is intended to make
102: * it easy to implement Collection.toArray(Object[] oo) methods...
103: *
104: * @param null_terminate iff there is extra space in the array, set the element
105: * immediately after the last from the iterator to null.
106: */
107: public static void fillArray(Iterator ii, Object[] fillMe,
108: boolean null_terminate) {
109: int i = 0;
110: int len = fillMe.length;
111: while (i < len && ii.hasNext())
112: fillMe[i++] = ii.next();
113: if (null_terminate && i < len)
114: fillMe[i] = null;
115: }
116:
117: public static void fillArray(Iterator ii, Object[] fillMe) {
118: fillArray(ii, fillMe, false);
119: }
120:
121: /**
122: * @param null_terminate iff there is extra space in the array, set the element
123: * immediately after the last from the iterator to null.
124: */
125: public static Object[] toArray(Iterator ii, int array_size,
126: Class componentClass, boolean null_terminate) {
127: Object[] out = (Object[]) Array.newInstance(componentClass,
128: array_size);
129: fillArray(ii, out, null_terminate);
130: return out;
131: }
132:
133: public static Object[] toArray(Iterator ii, int array_size,
134: Class componentClass) {
135: return toArray(ii, array_size, componentClass, false);
136: }
137:
138: /**
139: * Designed to help implement Collection.toArray(Object[] )methods... does
140: * the right thing if you can express an iterator and know the size of your
141: * Collection.
142: */
143: public static Object[] toArray(Iterator ii, int ii_size,
144: Object[] maybeFillMe) {
145: if (maybeFillMe.length >= ii_size) {
146: fillArray(ii, maybeFillMe, true);
147: return maybeFillMe;
148: } else {
149: Class componentType = maybeFillMe.getClass()
150: .getComponentType();
151: return toArray(ii, ii_size, componentType);
152: }
153: }
154:
155: private IteratorUtils() {
156: }
157: }
|