001: // ============================================================================
002: // $Id: CollectionUtils.java,v 1.1 2006/05/22 03:27:22 davidahall Exp $
003: // Copyright (c) 2006 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032: package net.sf.jga.util;
033:
034: import java.util.Collection;
035: import java.util.Iterator;
036:
037: /**
038: * General utilites for working with collections. These are generally extensions
039: * to what's already defined in the standard java.util.Collection class
040: * <p>
041: * Copyright © 2006 David A. Hall
042: *
043: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
044: */
045:
046: public class CollectionUtils {
047: /**
048: * Adds all of the elements of the iterator to the collection. If necessary and possible,
049: * the collection will be enlarged: if enlarging the collection is not possible, then a
050: * runtime exception will be thrown. This is an augmentation of the
051: * Collection.addAll(Collection) API method.
052: * @returns true if the state of the collection was changed.
053: */
054: static public <T> boolean addAll(Collection<? super T> cout,
055: Iterator<T> iter) {
056: boolean b = false;
057: while (iter.hasNext()) {
058: b |= cout.add(iter.next());
059: }
060:
061: return b;
062: }
063:
064: /**
065: * Adds all of the arguments to the collection. If necessary and possible, the collection
066: * will be enlarged: if enlarging the collection is not possible, then a runtime exception
067: * will be thrown.
068: * Augmentation of the Collection.addAll(Collection) API method.
069: * @returns true if the state of the collection was changed.
070: */
071: static public <T> boolean addAll(Collection<? super T> cout,
072: T... values) {
073: boolean b = false;
074: for (int i = 0; i < values.length; ++i) {
075: b |= cout.add(values[i]);
076: }
077:
078: return b;
079: }
080:
081: /**
082: * Adds all of the elements of the iterator to the collection and returns the collection.
083: * If necessary and possible, the collection will be enlarged: if enlarging the collection
084: * is not possible, then a runtime exception is thrown.
085: */
086: static public <T, TCollection extends Collection<? super T>> TCollection append(
087: TCollection cout, Iterable<T> iterable) {
088: addAll(cout, iterable.iterator());
089: return cout;
090: }
091:
092: /**
093: * Adds all of the elements of the iterator to the collection and returns the collection.
094: * If necessary and possible, the collection will be enlarged: if enlarging the collection
095: * is not possible, then a runtime exception is thrown.
096: */
097: static public <T, TCollection extends Collection<? super T>> TCollection append(
098: TCollection cout, Iterator<T> iter) {
099: addAll(cout, iter);
100: return cout;
101: }
102:
103: /**
104: * Adds all of the arguments to the collection and returns the collection. If necessary
105: * and possible, the collection will be enlarged: if enlarging the collection is not
106: * possible, then the runtime exception thrown. A similar method exists in Java 1.5,
107: * although it returns boolean rather than the collection.
108: */
109: static public <T, TCollection extends Collection<? super T>> TCollection append(
110: TCollection cout, T... values) {
111: addAll(cout, values);
112: return cout;
113: }
114: }
|