001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.collections.primitives;
018    
019    /**
020     * Abstract base class for {@link LongCollection}s.
021     * <p />
022     * Read-only subclasses must override {@link #iterator}
023     * and {@link #size}.  Mutable subclasses
024     * should also override {@link #add} and 
025     * {@link LongIterator#remove LongIterator.remove}.
026     * All other methods have at least some base implementation 
027     * derived from these.  Subclasses may choose to override 
028     * these methods to provide a more efficient implementation. 
029     * 
030     * @since Commons Primitives 1.0
031     * @version $Revision: 480460 $ $Date: 2006-11-29 09:14:21 +0100 (Wed, 29 Nov 2006) $
032     * 
033     * @author Rodney Waldhoff 
034     */
035    public abstract class AbstractLongCollection implements LongCollection {
036        public abstract LongIterator iterator();
037        public abstract int size();
038              
039        protected AbstractLongCollection() { }
040                  
041        /** Unsupported in this base implementation. */
042        public boolean add(long element) {
043            throw new UnsupportedOperationException("add(long) is not supported.");
044        }
045    
046        public boolean addAll(LongCollection c) {
047            boolean modified = false;
048            for(LongIterator iter = c.iterator(); iter.hasNext(); ) {
049                modified  |= add(iter.next());
050            }
051            return modified;
052        }
053        
054        public void clear() {
055            for(LongIterator iter = iterator(); iter.hasNext();) {
056                iter.next();
057                iter.remove();
058            }
059        }        
060    
061        public boolean contains(long element) {
062            for(LongIterator iter = iterator(); iter.hasNext();) {
063                if(iter.next() == element) {
064                    return true;
065                }
066            }
067            return false;
068        }
069            
070        public boolean containsAll(LongCollection c) {
071            for(LongIterator iter = c.iterator(); iter.hasNext();) {
072                if(!contains(iter.next())) {
073                    return false;
074                }
075            }
076            return true;
077        }
078        
079        public boolean isEmpty() {
080            return (0 == size());
081        }
082           
083        public boolean removeElement(long element) {
084            for(LongIterator iter = iterator(); iter.hasNext();) {
085                if(iter.next() == element) {
086                    iter.remove();
087                    return true;
088                }
089            }
090            return false;
091        }        
092        
093        public boolean removeAll(LongCollection c) {
094            boolean modified = false;
095            for(LongIterator iter = c.iterator(); iter.hasNext(); ) {
096                modified  |= removeElement(iter.next());
097            }
098            return modified;
099        }       
100        
101        public boolean retainAll(LongCollection c) {
102            boolean modified = false;
103            for(LongIterator iter = iterator(); iter.hasNext();) {
104                if(!c.contains(iter.next())) {
105                    iter.remove();
106                    modified = true;
107                }
108            }
109            return modified;
110        }
111        
112        public long[] toArray() {
113            long[] array = new long[size()];
114            int i = 0;
115            for(LongIterator iter = iterator(); iter.hasNext();) {
116                array[i] = iter.next();
117                i++;
118            }
119            return array;
120        }
121            
122        public long[] toArray(long[] a) {
123            if(a.length < size()) {
124                return toArray();
125            } else {
126                int i = 0;
127                for(LongIterator iter = iterator(); iter.hasNext();) {
128                    a[i] = iter.next();
129                    i++;
130                }
131                return a;
132            }            
133        }
134    }