/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.uwyn.rife.datastructures.collections.primitives; /** * Abstract base class for {@link IntCollection}s. * <p /> * Read-only subclasses must override {@link #iterator} * and {@link #size}. Mutable subclasses * should also override {@link #add} and * {@link IntIterator#remove IntIterator.remove}. * All other methods have at least some base implementation * derived from these. Subclasses may choose to override * these methods to provide a more efficient implementation. * * @since Commons Primitives 1.0 * @version $Revision: 3913 $ $Date: 2008-04-13 09:38:28 +0200 (Sun, 13 Apr 2008) $ * * @author Rodney Waldhoff */ public abstract class AbstractIntCollection implements IntCollection { public abstract IntIterator iterator(); public abstract int size(); protected AbstractIntCollection() { } /** Unsupported in this base implementation. */ public boolean add(int element) { throw new UnsupportedOperationException("add(int) is not supported."); } public boolean addAll(IntCollection c) { boolean modified = false; for(IntIterator iter = c.iterator(); iter.hasNext(); ) { modified |= add(iter.next()); } return modified; } public void clear() { for(IntIterator iter = iterator(); iter.hasNext();) { iter.next(); iter.remove(); } } public boolean contains(int element) { for(IntIterator iter = iterator(); iter.hasNext();) { if(iter.next() == element) { return true; } } return false; } public boolean containsAll(IntCollection c) { for(IntIterator iter = c.iterator(); iter.hasNext();) { if(!contains(iter.next())) { return false; } } return true; } public boolean isEmpty() { return (0 == size()); } public boolean removeElement(int element) { for(IntIterator iter = iterator(); iter.hasNext();) { if(iter.next() == element) { iter.remove(); return true; } } return false; } public boolean removeAll(IntCollection c) { boolean modified = false; for(IntIterator iter = c.iterator(); iter.hasNext(); ) { modified |= removeElement(iter.next()); } return modified; } public boolean retainAll(IntCollection c) { boolean modified = false; for(IntIterator iter = iterator(); iter.hasNext();) { if(!c.contains(iter.next())) { iter.remove(); modified = true; } } return modified; } public int[] toArray() { int[] array = new int[size()]; int i = 0; for(IntIterator iter = iterator(); iter.hasNext();) { array[i] = iter.next(); i++; } return array; } public int[] toArray(int[] a) { if(a.length < size()) { return toArray(); } else { int i = 0; for(IntIterator iter = iterator(); iter.hasNext();) { a[i] = iter.next(); i++; } return a; } } }