Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractBooleanCollection |
|
| 2.357142857142857;2.357 |
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 | 524 | protected AbstractBooleanCollection() { } |
36 | ||
37 | /** Unsupported in this base implementation. */ | |
38 | public boolean add(boolean element) { | |
39 | 1 | throw new UnsupportedOperationException( |
40 | "add(boolean) is not supported."); | |
41 | } | |
42 | ||
43 | public boolean addAll(BooleanCollection c) { | |
44 | 4 | boolean modified = false; |
45 | 4 | for(BooleanIterator iter = c.iterator(); iter.hasNext(); ) { |
46 | 46 | modified |= add(iter.next()); |
47 | } | |
48 | 3 | return modified; |
49 | } | |
50 | ||
51 | public void clear() { | |
52 | 2 | for(BooleanIterator iter = iterator(); iter.hasNext();) { |
53 | 13 | iter.next(); |
54 | 13 | iter.remove(); |
55 | } | |
56 | 2 | } |
57 | ||
58 | public boolean contains(boolean element) { | |
59 | 427 | for(BooleanIterator iter = iterator(); iter.hasNext();) { |
60 | 1563 | if(iter.next() == element) { |
61 | 310 | return true; |
62 | } | |
63 | } | |
64 | 117 | return false; |
65 | } | |
66 | ||
67 | public boolean containsAll(BooleanCollection c) { | |
68 | 14 | for(BooleanIterator iter = c.iterator(); iter.hasNext();) { |
69 | 108 | if(!contains(iter.next())) { |
70 | 4 | return false; |
71 | } | |
72 | } | |
73 | 10 | return true; |
74 | } | |
75 | ||
76 | public boolean isEmpty() { | |
77 | 932 | return (0 == size()); |
78 | } | |
79 | ||
80 | public boolean removeElement(boolean element) { | |
81 | 142 | for(BooleanIterator iter = iterator(); iter.hasNext();) { |
82 | 729 | if(iter.next() == element) { |
83 | 52 | iter.remove(); |
84 | 52 | return true; |
85 | } | |
86 | } | |
87 | 90 | return false; |
88 | } | |
89 | ||
90 | public boolean removeAll(BooleanCollection c) { | |
91 | 5 | boolean modified = false; |
92 | 5 | for(BooleanIterator iter = c.iterator(); iter.hasNext(); ) { |
93 | 25 | modified |= removeElement(iter.next()); |
94 | } | |
95 | 5 | return modified; |
96 | } | |
97 | ||
98 | public boolean retainAll(BooleanCollection c) { | |
99 | 5 | boolean modified = false; |
100 | 5 | for(BooleanIterator iter = iterator(); iter.hasNext();) { |
101 | 13 | if(!c.contains(iter.next())) { |
102 | 5 | iter.remove(); |
103 | 5 | modified = true; |
104 | } | |
105 | } | |
106 | 5 | return modified; |
107 | } | |
108 | ||
109 | public boolean[] toArray() { | |
110 | 896 | boolean[] array = new boolean[size()]; |
111 | 896 | int i = 0; |
112 | 896 | for(BooleanIterator iter = iterator(); iter.hasNext();) { |
113 | 13690 | array[i] = iter.next(); |
114 | 13690 | i++; |
115 | } | |
116 | 896 | return array; |
117 | } | |
118 | ||
119 | public boolean[] toArray(boolean[] a) { | |
120 | 3 | if(a.length < size()) { |
121 | 1 | return toArray(); |
122 | } else { | |
123 | 2 | int i = 0; |
124 | 2 | for(BooleanIterator iter = iterator(); iter.hasNext();) { |
125 | 38 | a[i] = iter.next(); |
126 | 38 | i++; |
127 | } | |
128 | 2 | return a; |
129 | } | |
130 | } | |
131 | } |