View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections.primitives;
18  
19  /**
20   * Abstract base class for {@link BooleanCollection}s.
21   * <p/>
22   * Read-only subclasses must override {@link #iterator} and {@link #size}.
23   * Mutable subclasses should also override {@link #add} and {@link
24   * BooleanIterator#remove BooleanIterator.remove}.  All other methods have
25   * at least some base implementation derived from these.  Subclasses may
26   * choose to override these methods to provide a more efficient implementation.
27   * 
28   * @since Commons Primitives 1.1
29   * @version $Revision: 480460 $ $Date: 2006-11-29 03:14:21 -0500 (Wed, 29 Nov 2006) $
30   */
31  public abstract class AbstractBooleanCollection implements BooleanCollection {
32      public abstract BooleanIterator iterator();
33      public abstract int size();
34            
35      protected AbstractBooleanCollection() { }
36                
37      /** Unsupported in this base implementation. */
38      public boolean add(boolean element) {
39          throw new UnsupportedOperationException(
40                  "add(boolean) is not supported.");
41      }
42  
43      public boolean addAll(BooleanCollection c) {
44          boolean modified = false;
45          for(BooleanIterator iter = c.iterator(); iter.hasNext(); ) {
46              modified  |= add(iter.next());
47          }
48          return modified;
49      }
50      
51      public void clear() {
52          for(BooleanIterator iter = iterator(); iter.hasNext();) {
53              iter.next();
54              iter.remove();
55          }
56      }        
57  
58      public boolean contains(boolean element) {
59          for(BooleanIterator iter = iterator(); iter.hasNext();) {
60              if(iter.next() == element) {
61                  return true;
62              }
63          }
64          return false;
65      }
66          
67      public boolean containsAll(BooleanCollection c) {
68          for(BooleanIterator iter = c.iterator(); iter.hasNext();) {
69              if(!contains(iter.next())) {
70                  return false;
71              }
72          }
73          return true;
74      }
75      
76      public boolean isEmpty() {
77          return (0 == size());
78      }
79         
80      public boolean removeElement(boolean element) {
81          for(BooleanIterator iter = iterator(); iter.hasNext();) {
82              if(iter.next() == element) {
83                  iter.remove();
84                  return true;
85              }
86          }
87          return false;
88      }        
89      
90      public boolean removeAll(BooleanCollection c) {
91          boolean modified = false;
92          for(BooleanIterator iter = c.iterator(); iter.hasNext(); ) {
93              modified  |= removeElement(iter.next());
94          }
95          return modified;
96      }       
97      
98      public boolean retainAll(BooleanCollection c) {
99          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 }