KickJava   Java API By Example, From Geeks To Geeks.

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


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

65 public class SetToIntSetAdapter extends AbstractIntSet implements IntSet {
66
67     /** The underlying set. */
68     protected Set set;
69
70     /**
71      * Creates a new adaption to a set of int
72      * values.
73      *
74      * @param set
75      * the underlying set. This set must
76      * consist of values of class
77      * {@link Integer Integer}. Otherwise a
78      * {@link ClassCastException ClassCastException}
79      * will be thrown by some methods.
80      *
81      * @throws NullPointerException
82      * if <tt>set</tt> is <tt>null</tt>.
83      */

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

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

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

164     public void evalidate() {
165         if (!validate())
166             Exceptions.cannotAdapt("set");
167     }
168
169 }
Popular Tags