001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.util.ArrayList;
030: import java.util.Collection;
031: import java.util.Enumeration;
032: import java.util.Iterator;
033:
034: /** An Enumeration which is backed by an Iterator over a copy of an
035: * original collection. Optimized for zero-element cases, and
036: * non-zero length ArrayList arguments.
037: *
038: * Useful for exposing a pre-collections interface to a
039: * collections-based implementation.
040: **/
041:
042: public final class BackedEnumerator implements Enumeration {
043: private Object[] a;
044: private int size;
045: private int index = 0;
046:
047: public BackedEnumerator(Collection c) {
048: size = c.size();
049: if (size != 0) {
050: Iterator ci = c.iterator();
051: a = new Object[size];
052: for (int i = 0; i < size; i++) {
053: a[i] = ci.next();
054: }
055: } else {
056: a = null;
057: }
058: }
059:
060: // more efficient version for ArrayList
061: public BackedEnumerator(ArrayList c) {
062: if (c.size() > 0) {
063: a = c.toArray();
064: size = a.length;
065: } else {
066: a = null;
067: size = 0;
068: }
069: }
070:
071: public BackedEnumerator(Enumeration e) {
072: if (e.hasMoreElements()) {
073: ArrayList tmp = new ArrayList();
074: while (e.hasMoreElements()) {
075: tmp.add(e.nextElement());
076: }
077: a = tmp.toArray();
078: size = a.length;
079: } else {
080: a = null;
081: size = 0;
082: }
083: }
084:
085: public BackedEnumerator(Iterator i) {
086: if (i.hasNext()) {
087: ArrayList tmp = new ArrayList();
088: while (i.hasNext()) {
089: tmp.add(i.next());
090: }
091: a = tmp.toArray();
092: size = a.length;
093: } else {
094: a = null;
095: size = 0;
096: }
097: }
098:
099: public final boolean hasMoreElements() {
100: return (index < size);
101: }
102:
103: public final Object nextElement() {
104: return a[index++];
105: }
106: }
|