KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2002 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.set.ShortSet;
22 import bak.pcj.adapter.ShortIteratorToIteratorAdapter;
23 import bak.pcj.util.Exceptions;
24
25 import java.util.AbstractSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Collection JavaDoc;
28
29 /**
30  * This class represents adapters of short sets to Java Collections
31  * Framework sets. The adapter
32  * is implemented as a wrapper around a primitive set. Thus,
33  * changes to the underlying set are reflected by this
34  * set and vice versa.
35  *
36  * @see ShortSet
37  * @see java.util.Set
38  *
39  * @author Søren Bak
40  * @version 1.2 20-08-2003 22:56
41  * @since 1.0
42  */

43 public class ShortSetToSetAdapter extends AbstractSet JavaDoc {
44
45     /** The underlying primitive set. */
46     protected ShortSet set;
47
48     /**
49      * Creates a new adaption of a set of short
50      * values to a Java Collections Framework set.
51      *
52      * @param set
53      * the underlying primitive set.
54      *
55      * @throws NullPointerException
56      * if <tt>set</tt> is <tt>null</tt>.
57      */

58     public ShortSetToSetAdapter(ShortSet set) {
59         if (set == null)
60             Exceptions.nullArgument("set");
61         this.set = set;
62     }
63
64     /**
65      * Adds an element to this set. The unwrapped element is added
66      * to the underlying set.
67      *
68      * @param o
69      * the element to add to this set.
70      *
71      * @return <tt>true</tt> if this set was modified
72      * as a result of adding <tt>o</tt>; returns
73      * <tt>false</tt> otherwise.
74      *
75      * @throws IllegalArgumentException
76      * if <tt>o</tt> is <tt>null</tt>.
77      *
78      * @throws ClassCastException
79      * if <tt>o</tt> is not of class {@link Short Short}.
80      *
81      * @throws UnsupportedOperationException
82      * if the operation is not supported by the
83      * underlying set.
84      */

85     public boolean add(Object JavaDoc o) {
86         if (o == null)
87             Exceptions.nullElementNotAllowed();
88         return set.add(((Short JavaDoc)o).shortValue());
89     }
90
91     /**
92      * Clears this collection. The underlying set is
93      * cleared.
94      *
95      * @throws UnsupportedOperationException
96      * if the operation is not supported by the
97      * underlying set.
98      */

99     public void clear()
100     { set.clear(); }
101
102     /**
103      * Indicates whether this set contains a specified
104      * element. For this set to contain an object, the
105      * underlying set must contain its unwrapped value.
106      * <p>Note that this set can never contain <tt>null</tt>
107      * values or values of other classes than {@link Short Short}.
108      * In those cases, this method will return <tt>false</tt>.
109      *
110      * @param o
111      * the element to test for containment.
112      *
113      * @return <tt>true</tt> if <tt>o</tt> is contained in this
114      * set; returns <tt>false</tt> otherwise.
115      */

116     public boolean contains(Object JavaDoc o) {
117         try {
118             return set.contains( ((Short JavaDoc)o).shortValue() );
119         } catch (ClassCastException JavaDoc cce) {
120         } catch (NullPointerException JavaDoc npe) {
121         }
122         return false;
123     }
124
125     /**
126      * Returns a hash code value for this set. The hash code
127      * returned is that of the underlying set.
128      *
129      * @return a hash code value for this set.
130      */

131     public int hashCode()
132     { return set.hashCode(); }
133
134     /**
135      * Returns an iterator over this set.
136      *
137      * @return an iterator over this set.
138      */

139     public Iterator JavaDoc iterator()
140     { return new ShortIteratorToIteratorAdapter(set.iterator()); }
141
142     /**
143      * Removes a specified element from this set.
144      * The unwrapped element is removed from the underlying set.
145      * <p>Note that this set can never contain <tt>null</tt>
146      * values or values of other classes than {@link Short Short}.
147      * In those cases, this method will return <tt>false</tt>.
148      *
149      * @param o
150      * the Short value to remove from this set.
151      *
152      * @return <tt>true</tt> if this set was modified
153      * as a result of removing <tt>o</tt>; returns
154      * <tt>false</tt> otherwise.
155      *
156      * @throws UnsupportedOperationException
157      * if the operation is not supported by the
158      * underlying set.
159      */

160     public boolean remove(Object JavaDoc o) {
161         try {
162             return set.remove( ((Short JavaDoc)o).shortValue() );
163         } catch (ClassCastException JavaDoc cce) {
164         } catch (NullPointerException JavaDoc npe) {
165         }
166         return false;
167     }
168
169     /**
170      * Retains only the elements of a specified collection in
171      * this set. The unwrapped elements are removed from
172      * the underlying set.
173      * <p>This method is only overridden to work
174      * around a bug in {@link AbstractSet AbstractSet},
175      * which does not throw a
176      * {@link NullPointerException NullPointerException} when the
177      * argument is <tt>null</tt> and the set is empty. The
178      * bug is inherited from {@link java.util.AbstractCollection java.util.AbstractCollection}.
179      *
180      * @param c
181      * the collection whose elements to retain in this
182      * collection.
183      *
184      * @return <tt>true</tt> if this set was modified
185      * as a result of removing the elements not contained
186      * in <tt>c</tt>;
187      * returns <tt>false</tt> otherwise.
188      *
189      * @throws UnsupportedOperationException
190      * if the operation is not supported by the underlying
191      * set.
192      *
193      * @throws NullPointerException
194      * if <tt>c</tt> is <tt>null</tt>.
195      */

196     public boolean retainAll(Collection JavaDoc c) {
197         if (c == null)
198             Exceptions.nullArgument("collection");
199         return super.retainAll(c);
200     }
201
202     /**
203      * Returns the number of elements in this set. The
204      * number of elements is the same as that of the underlying
205      * set.
206      *
207      * @return the number of elements in this set.
208      */

209     public int size()
210     { return set.size(); }
211
212 }
Popular Tags