KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > adapter > CollectionToBooleanCollectionAdapter


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2002, 2003 Søren Bak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj.adapter;
20
21 import bak.pcj.Adapter;
22 import bak.pcj.AbstractBooleanCollection;
23 import bak.pcj.BooleanIterator;
24 import bak.pcj.util.Exceptions;
25 import java.util.Collection JavaDoc;
26 import java.util.AbstractCollection JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  * This class represents adaptions of Java Collections Framework
31  * collections to primitive collections of boolean values.
32  * The adapter is implemented as a wrapper around the collection.
33  * Thus, changes to the underlying collection are reflected by this
34  * collection and vice versa.
35  *
36  * <p>
37  * Adapters from JCF collections to primitive collections will
38  * fail if the JCF collection contains <tt>null</tt> values or
39  * values of the wrong class. However, adapters are not fast
40  * failing in the case that the underlying collection should
41  * contain illegal values. To implement fast failure would require
42  * every operation to check every element of the underlying
43  * collection before doing anything. Instead validation methods
44  * are provided. They can be called using the assertion facility
45  * in the client code:
46  * <pre>
47  * CollectionToBooleanCollectionAdapter s;
48  * ...
49  * <b>assert</b> s.validate();
50  * </pre>
51  * or by letting the adapter throw an exception on illegal values:
52  * <pre>
53  * CollectionToBooleanCollectionAdapter s;
54  * ...
55  * s.evalidate(); // Throws an exception on illegal values
56  * </pre>
57  * Either way, validation must be invoked directly by the client
58  * code.
59  *
60  * @author S&oslash;ren Bak
61  * @version 1.2 20-08-2003 23:18
62  * @since 1.0
63  */

64 public class CollectionToBooleanCollectionAdapter extends AbstractBooleanCollection {
65
66     /** The underlying collection. */
67     protected Collection collection;
68
69     /**
70      * Creates a new adaption of a collection to a collection of boolean
71      * values.
72      *
73      * @param collection
74      * the underlying collection. This collection must
75      * consist of values of class
76      * {@link Boolean Boolean}. Otherwise a
77      * {@link ClassCastException ClassCastException}
78      * will be thrown by some methods.
79      *
80      * @throws NullPointerException
81      * if <tt>collection</tt> is <tt>null</tt>.
82      */

83     public CollectionToBooleanCollectionAdapter(Collection collection) {
84         this(collection, false);
85     }
86
87     /**
88      * Creates a new adaption of a collection to a collection of boolean
89      * values. The collection to adapt is optionally validated.
90      *
91      * @param collection
92      * the underlying collection. This collection must
93      * consist of values of class
94      * {@link Boolean Boolean}. Otherwise a
95      * {@link ClassCastException ClassCastException}
96      * will be thrown by some methods.
97      *
98      * @param validate
99      * indicates whether <tt>collection</tt> should
100      * be checked for illegal values.
101      *
102      * @throws NullPointerException
103      * if <tt>collection</tt> is <tt>null</tt>.
104      *
105      * @throws IllegalStateException
106      * if <tt>validate</tt> is <tt>true</tt> and
107      * <tt>collection</tt> contains a <tt>null</tt> value
108      * or a value that is not of class
109      * {@link Boolean Boolean}.
110      */

111     public CollectionToBooleanCollectionAdapter(Collection collection, boolean validate) {
112         super();
113         if (collection == null)
114             Exceptions.nullArgument("collection");
115         this.collection = collection;
116         if (validate)
117             evalidate();
118     }
119
120     public boolean add(boolean v)
121     { return collection.add(new Boolean JavaDoc(v)); }
122
123     public void clear()
124     { collection.clear(); }
125
126     public boolean contains(boolean v)
127     { return collection.contains(new Boolean JavaDoc(v)); }
128
129     public int hashCode()
130     { return collection.hashCode(); }
131
132     public BooleanIterator iterator()
133     { return new IteratorToBooleanIteratorAdapter(collection.iterator()); }
134
135     public boolean remove(boolean v)
136     { return collection.remove(new Boolean JavaDoc(v)); }
137
138     public int size()
139     { return collection.size(); }
140
141     /**
142      * Indicates whether the underlying collection is valid for
143      * this adapter. For the underlying collection to be valid, it
144      * can only contain {@link Boolean Boolean} values and no <tt>null</tt>
145      * values.
146      *
147      * @return <tt>true</tt> if the underlying collection is
148      * valid; returns <tt>false</tt> otherwise.
149      */

150     public boolean validate()
151     { return Adapter.isBooleanAdaptable(collection); }
152
153     /**
154      * Validates the collection underlying this adapter and throws
155      * an exception if it is invalid. For the underlying collection
156      * to be valid, it can only contain {@link Boolean Boolean}
157      * values and no <tt>null</tt> values.
158      *
159      * @throws IllegalStateException
160      * if the underlying collection is invalid.
161      */

162     public void evalidate() {
163         if (!validate())
164             Exceptions.cannotAdapt("collection");
165     }
166
167 }
Popular Tags