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 */ 017package org.apache.commons.collections.primitives; 018 019/** 020 * Abstract base class for {@link BooleanCollection}s. 021 * <p/> 022 * Read-only subclasses must override {@link #iterator} and {@link #size}. 023 * Mutable subclasses should also override {@link #add} and {@link 024 * BooleanIterator#remove BooleanIterator.remove}. All other methods have 025 * at least some base implementation derived from these. Subclasses may 026 * choose to override these methods to provide a more efficient implementation. 027 * 028 * @since Commons Primitives 1.1 029 * @version $Revision: 480460 $ $Date: 2006-11-29 03:14:21 -0500 (Wed, 29 Nov 2006) $ 030 */ 031public abstract class AbstractBooleanCollection implements BooleanCollection { 032 public abstract BooleanIterator iterator(); 033 public abstract int size(); 034 035 protected AbstractBooleanCollection() { } 036 037 /** Unsupported in this base implementation. */ 038 public boolean add(boolean element) { 039 throw new UnsupportedOperationException( 040 "add(boolean) is not supported."); 041 } 042 043 public boolean addAll(BooleanCollection c) { 044 boolean modified = false; 045 for(BooleanIterator iter = c.iterator(); iter.hasNext(); ) { 046 modified |= add(iter.next()); 047 } 048 return modified; 049 } 050 051 public void clear() { 052 for(BooleanIterator iter = iterator(); iter.hasNext();) { 053 iter.next(); 054 iter.remove(); 055 } 056 } 057 058 public boolean contains(boolean element) { 059 for(BooleanIterator iter = iterator(); iter.hasNext();) { 060 if(iter.next() == element) { 061 return true; 062 } 063 } 064 return false; 065 } 066 067 public boolean containsAll(BooleanCollection c) { 068 for(BooleanIterator iter = c.iterator(); iter.hasNext();) { 069 if(!contains(iter.next())) { 070 return false; 071 } 072 } 073 return true; 074 } 075 076 public boolean isEmpty() { 077 return (0 == size()); 078 } 079 080 public boolean removeElement(boolean element) { 081 for(BooleanIterator iter = iterator(); iter.hasNext();) { 082 if(iter.next() == element) { 083 iter.remove(); 084 return true; 085 } 086 } 087 return false; 088 } 089 090 public boolean removeAll(BooleanCollection c) { 091 boolean modified = false; 092 for(BooleanIterator iter = c.iterator(); iter.hasNext(); ) { 093 modified |= removeElement(iter.next()); 094 } 095 return modified; 096 } 097 098 public boolean retainAll(BooleanCollection c) { 099 boolean modified = false; 100 for(BooleanIterator iter = iterator(); iter.hasNext();) { 101 if(!c.contains(iter.next())) { 102 iter.remove(); 103 modified = true; 104 } 105 } 106 return modified; 107 } 108 109 public boolean[] toArray() { 110 boolean[] array = new boolean[size()]; 111 int i = 0; 112 for(BooleanIterator iter = iterator(); iter.hasNext();) { 113 array[i] = iter.next(); 114 i++; 115 } 116 return array; 117 } 118 119 public boolean[] toArray(boolean[] a) { 120 if(a.length < size()) { 121 return toArray(); 122 } else { 123 int i = 0; 124 for(BooleanIterator iter = iterator(); iter.hasNext();) { 125 a[i] = iter.next(); 126 i++; 127 } 128 return a; 129 } 130 } 131}