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.adapters;
18  
19  import java.lang.reflect.Array;
20  import java.util.Collection;
21  import java.util.Iterator;
22  
23  import org.apache.commons.collections.primitives.BooleanCollection;
24  
25  /**
26   * @since Commons Primitives 1.1
27   * @version $Revision: 480462 $ $Date: 2006-11-29 03:15:00 -0500 (Wed, 29 Nov 2006) $
28   */
29  abstract class AbstractBooleanCollectionCollection implements Collection {
30      
31      public boolean add(Object element) {
32          return getBooleanCollection().add(((Boolean)element).booleanValue());
33      }
34  
35      public boolean addAll(Collection c) {
36          return getBooleanCollection()
37                  .addAll(CollectionBooleanCollection.wrap(c));
38      }
39          
40      public void clear() {
41          getBooleanCollection().clear();
42      }
43  
44      public boolean contains(Object element) {
45          return getBooleanCollection()
46                  .contains(((Boolean)element).booleanValue());
47      }
48     
49      
50      public boolean containsAll(Collection c) {
51          return getBooleanCollection().containsAll(CollectionBooleanCollection.wrap(c));
52      }        
53          
54      public String toString() {
55          return getBooleanCollection().toString();
56      }
57      
58      public boolean isEmpty() {
59          return getBooleanCollection().isEmpty();
60      }
61      
62      /**
63       * {@link BooleanIteratorIterator#wrap wraps} the
64       * {@link org.apache.commons.collections.primitives.BooleanIterator
65       * BooleanIterator} returned by my underlying
66       * {@link org.apache.commons.collections.primitives.BooleanCollection
67       * BooleanCollection}, if any.
68       */
69      public Iterator iterator() {
70          return BooleanIteratorIterator.wrap(getBooleanCollection().iterator());
71      }
72       
73      public boolean remove(Object element) {
74          return getBooleanCollection().removeElement(
75                  ((Boolean)element).booleanValue());
76      }
77      
78      public boolean removeAll(Collection c) {
79          return getBooleanCollection()
80                  .removeAll(CollectionBooleanCollection.wrap(c));
81      }
82      
83      public boolean retainAll(Collection c) {
84          return getBooleanCollection().
85                  retainAll(CollectionBooleanCollection.wrap(c));
86      }
87      
88      public int size() {
89          return getBooleanCollection().size();
90      }
91      
92      public Object[] toArray() {
93          boolean[] a = getBooleanCollection().toArray();
94          Object[] A = new Object[a.length];
95          for(int i=0;i<a.length;i++) {
96              A[i] = new Boolean(a[i]);
97          }
98          return A;
99      }
100     
101     public Object[] toArray(Object[] A) {
102         boolean[] a = getBooleanCollection().toArray();
103         if(A.length < a.length) {
104             A = (Object[])(Array.newInstance(A.getClass().getComponentType(), a.length));
105         }
106         for(int i=0;i<a.length;i++) {
107             A[i] = new Boolean(a[i]);
108         }
109         if(A.length > a.length) {
110             A[a.length] = null;
111         }
112 
113         return A;
114     }
115 
116     protected abstract BooleanCollection getBooleanCollection();
117 }