KickJava   Java API By Example, From Geeks To Geeks.

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


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

41 public class IntSortedSetToSortedSetAdapter extends IntSetToSetAdapter implements SortedSet {
42
43     /**
44      * Creates a new adaption of a set of int
45      * values to a Java Collections Framework set.
46      *
47      * @param set
48      * the underlying primitive set.
49      *
50      * @throws NullPointerException
51      * if <tt>set</tt> is <tt>null</tt>.
52      */

53     public IntSortedSetToSortedSetAdapter(IntSortedSet set) {
54         super(set);
55     }
56
57     /**
58      * Returns the comparator used by this set. This method
59      * always returns <tt>null</tt>, since primitive sets are
60      * sorted by their natural ordering.
61      *
62      * @return <tt>null</tt>.
63      */

64     public Comparator JavaDoc comparator()
65     { return null; }
66
67     /**
68      * Returns the lowest element of this set.
69      *
70      * @return the lowest element of this set.
71      *
72      * @throws NoSuchElementException
73      * if this set is empty.
74      */

75     public Object JavaDoc first()
76     { return new Integer JavaDoc(((IntSortedSet)set).first()); }
77
78     /**
79      * Returns the subset of values lower than a specified value.
80      * The returned subset is a view of this set, so changes to the
81      * subset are reflected by this set and vice versa.
82      *
83      * @param to
84      * the upper bound of the returned set (not included).
85      *
86      * @throws IllegalArgumentException
87      * if <tt>to</tt> is not permitted
88      * in this set (which can be the case with returned
89      * subsets).
90      *
91      * @throws ClassCastException
92      * if <tt>to</tt> is not of class {@link Integer Integer}.
93      *
94      * @throws NullPointerException
95      * if <tt>to</tt> is <tt>null</tt>.
96      */

97     public SortedSet headSet(Object JavaDoc to)
98     { return new IntSortedSetToSortedSetAdapter(((IntSortedSet)set).headSet( ((Integer JavaDoc)to).intValue() )); }
99
100     /**
101      * Returns the highest element of this set.
102      *
103      * @return the highest element of this set.
104      *
105      * @throws NoSuchElementException
106      * if this set is empty.
107      */

108     public Object JavaDoc last()
109     { return new Integer JavaDoc(((IntSortedSet)set).last()); }
110
111     /**
112      * Returns the subset of values lower that a specified value and
113      * higher than or equal to another specified value.
114      * The returned subset is a view of this set, so changes to the
115      * subset are reflected by this set and vice versa.
116      *
117      * @param from
118      * the lower bound of the returned set (included).
119      *
120      * @param to
121      * the upper bound of the returned set (not included).
122      *
123      * @throws IllegalArgumentException
124      * if <tt>from</tt> is greater than <tt>to</tt>;
125      * if <tt>from</tt> or <tt>to</tt> is not permitted
126      * in this set (which can be the case with returned
127      * subsets).
128      *
129      * @throws ClassCastException
130      * if <tt>from</tt> is not of class {@link Integer Integer};
131      * if <tt>to</tt> is not of class {@link Integer Integer}.
132      *
133      * @throws NullPointerException
134      * if <tt>from</tt> is <tt>null</tt>;
135      * if <tt>to</tt> is <tt>null</tt>.
136      */

137     public SortedSet subSet(Object JavaDoc from, Object JavaDoc to) {
138         int tfrom = ((Integer JavaDoc)from).intValue();
139         int tto = ((Integer JavaDoc)to).intValue();
140         return new IntSortedSetToSortedSetAdapter(((IntSortedSet)set).subSet(tfrom, tto));
141     }
142
143     /**
144      * Returns the subset of values higher than or equal to a
145      * specified value.
146      * The returned subset is a view of this set, so changes to the
147      * subset are reflected by this set and vice versa.
148      *
149      * @param from
150      * the lower bound of the returned set (included).
151      *
152      * @throws IllegalArgumentException
153      * if <tt>from</tt> is not permitted
154      * in this set (which can be the case with returned
155      * subsets).
156      *
157      * @throws ClassCastException
158      * if <tt>from</tt> is not of class {@link Integer Integer}.
159      *
160      * @throws NullPointerException
161      * if <tt>from</tt> is <tt>null</tt>.
162      */

163     public SortedSet tailSet(Object JavaDoc from)
164     { return new IntSortedSetToSortedSetAdapter(((IntSortedSet)set).tailSet( ((Integer JavaDoc)from).intValue() )); }
165
166 }
Popular Tags