001: /*
002: * $Header: /home/cvs/jakarta-commons/primitives/src/java/org/apache/commons/collections/primitives/AbstractLongCollection.java,v 1.3 2003/10/16 20:49:36 scolebourne Exp $
003: * ====================================================================
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 2003 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowledgement:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowledgement may appear in the software itself,
026: * if and wherever such third-party acknowledgements normally appear.
027: *
028: * 4. The names "The Jakarta Project", "Commons", and "Apache Software
029: * Foundation" must not be used to endorse or promote products derived
030: * from this software without prior written permission. For written
031: * permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache"
034: * nor may "Apache" appear in their names without prior written
035: * permission of the Apache Software Foundation.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: *
056: */
057:
058: package org.apache.commons.collections.primitives;
059:
060: /**
061: * Abstract base class for {@link LongCollection}s.
062: * <p />
063: * Read-only subclasses must override {@link #iterator}
064: * and {@link #size}. Mutable subclasses
065: * should also override {@link #add} and
066: * {@link LongIterator#remove LongIterator.remove}.
067: * All other methods have at least some base implementation
068: * derived from these. Subclasses may choose to override
069: * these methods to provide a more efficient implementation.
070: *
071: * @since Commons Primitives 1.0
072: * @version $Revision: 1.3 $ $Date: 2003/10/16 20:49:36 $
073: *
074: * @author Rodney Waldhoff
075: */
076: public abstract class AbstractLongCollection implements LongCollection {
077: public abstract LongIterator iterator();
078:
079: public abstract int size();
080:
081: protected AbstractLongCollection() {
082: }
083:
084: /** Unsupported in this base implementation. */
085: public boolean add(long element) {
086: throw new UnsupportedOperationException(
087: "add(long) is not supported.");
088: }
089:
090: public boolean addAll(LongCollection c) {
091: boolean modified = false;
092: for (LongIterator iter = c.iterator(); iter.hasNext();) {
093: modified |= add(iter.next());
094: }
095: return modified;
096: }
097:
098: public void clear() {
099: for (LongIterator iter = iterator(); iter.hasNext();) {
100: iter.next();
101: iter.remove();
102: }
103: }
104:
105: public boolean contains(long element) {
106: for (LongIterator iter = iterator(); iter.hasNext();) {
107: if (iter.next() == element) {
108: return true;
109: }
110: }
111: return false;
112: }
113:
114: public boolean containsAll(LongCollection c) {
115: for (LongIterator iter = c.iterator(); iter.hasNext();) {
116: if (!contains(iter.next())) {
117: return false;
118: }
119: }
120: return true;
121: }
122:
123: public boolean isEmpty() {
124: return (0 == size());
125: }
126:
127: public boolean removeElement(long element) {
128: for (LongIterator iter = iterator(); iter.hasNext();) {
129: if (iter.next() == element) {
130: iter.remove();
131: return true;
132: }
133: }
134: return false;
135: }
136:
137: public boolean removeAll(LongCollection c) {
138: boolean modified = false;
139: for (LongIterator iter = c.iterator(); iter.hasNext();) {
140: modified |= removeElement(iter.next());
141: }
142: return modified;
143: }
144:
145: public boolean retainAll(LongCollection c) {
146: boolean modified = false;
147: for (LongIterator iter = iterator(); iter.hasNext();) {
148: if (!c.contains(iter.next())) {
149: iter.remove();
150: modified = true;
151: }
152: }
153: return modified;
154: }
155:
156: public long[] toArray() {
157: long[] array = new long[size()];
158: int i = 0;
159: for (LongIterator iter = iterator(); iter.hasNext();) {
160: array[i] = iter.next();
161: i++;
162: }
163: return array;
164: }
165:
166: public long[] toArray(long[] a) {
167: if (a.length < size()) {
168: return toArray();
169: } else {
170: int i = 0;
171: for (LongIterator iter = iterator(); iter.hasNext();) {
172: a[i] = iter.next();
173: i++;
174: }
175: return a;
176: }
177: }
178: }
|