001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2003 Søren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package bak.pcj.adapter;
020:
021: import bak.pcj.set.DoubleSortedSet;
022:
023: import java.util.Comparator;
024: import java.util.AbstractSet;
025: import java.util.SortedSet;
026:
027: /**
028: * This class represents adapters of double sets to Java Collections
029: * Framework sets. The adapter
030: * is implemented as a wrapper around a primitive set. Thus,
031: * changes to the underlying set are reflected by this
032: * set and vice versa.
033: *
034: * @see DoubleSortedSet
035: * @see java.util.SortedSet
036: *
037: * @author Søren Bak
038: * @version 1.0 2002/12/04
039: * @since 1.2
040: */
041: public class DoubleSortedSetToSortedSetAdapter extends
042: DoubleSetToSetAdapter implements SortedSet {
043:
044: /**
045: * Creates a new adaption of a set of double
046: * values to a Java Collections Framework set.
047: *
048: * @param set
049: * the underlying primitive set.
050: *
051: * @throws NullPointerException
052: * if <tt>set</tt> is <tt>null</tt>.
053: */
054: public DoubleSortedSetToSortedSetAdapter(DoubleSortedSet set) {
055: super (set);
056: }
057:
058: /**
059: * Returns the comparator used by this set. This method
060: * always returns <tt>null</tt>, since primitive sets are
061: * sorted by their natural ordering.
062: *
063: * @return <tt>null</tt>.
064: */
065: public Comparator comparator() {
066: return null;
067: }
068:
069: /**
070: * Returns the lowest element of this set.
071: *
072: * @return the lowest element of this set.
073: *
074: * @throws NoSuchElementException
075: * if this set is empty.
076: */
077: public Object first() {
078: return new Double(((DoubleSortedSet) set).first());
079: }
080:
081: /**
082: * Returns the subset of values lower than a specified value.
083: * The returned subset is a view of this set, so changes to the
084: * subset are reflected by this set and vice versa.
085: *
086: * @param to
087: * the upper bound of the returned set (not included).
088: *
089: * @throws IllegalArgumentException
090: * if <tt>to</tt> is not permitted
091: * in this set (which can be the case with returned
092: * subsets).
093: *
094: * @throws ClassCastException
095: * if <tt>to</tt> is not of class {@link Double Double}.
096: *
097: * @throws NullPointerException
098: * if <tt>to</tt> is <tt>null</tt>.
099: */
100: public SortedSet headSet(Object to) {
101: return new DoubleSortedSetToSortedSetAdapter(
102: ((DoubleSortedSet) set).headSet(((Double) to)
103: .doubleValue()));
104: }
105:
106: /**
107: * Returns the highest element of this set.
108: *
109: * @return the highest element of this set.
110: *
111: * @throws NoSuchElementException
112: * if this set is empty.
113: */
114: public Object last() {
115: return new Double(((DoubleSortedSet) set).last());
116: }
117:
118: /**
119: * Returns the subset of values lower that a specified value and
120: * higher than or equal to another specified value.
121: * The returned subset is a view of this set, so changes to the
122: * subset are reflected by this set and vice versa.
123: *
124: * @param from
125: * the lower bound of the returned set (included).
126: *
127: * @param to
128: * the upper bound of the returned set (not included).
129: *
130: * @throws IllegalArgumentException
131: * if <tt>from</tt> is greater than <tt>to</tt>;
132: * if <tt>from</tt> or <tt>to</tt> is not permitted
133: * in this set (which can be the case with returned
134: * subsets).
135: *
136: * @throws ClassCastException
137: * if <tt>from</tt> is not of class {@link Double Double};
138: * if <tt>to</tt> is not of class {@link Double Double}.
139: *
140: * @throws NullPointerException
141: * if <tt>from</tt> is <tt>null</tt>;
142: * if <tt>to</tt> is <tt>null</tt>.
143: */
144: public SortedSet subSet(Object from, Object to) {
145: double tfrom = ((Double) from).doubleValue();
146: double tto = ((Double) to).doubleValue();
147: return new DoubleSortedSetToSortedSetAdapter(
148: ((DoubleSortedSet) set).subSet(tfrom, tto));
149: }
150:
151: /**
152: * Returns the subset of values higher than or equal to a
153: * specified value.
154: * The returned subset is a view of this set, so changes to the
155: * subset are reflected by this set and vice versa.
156: *
157: * @param from
158: * the lower bound of the returned set (included).
159: *
160: * @throws IllegalArgumentException
161: * if <tt>from</tt> is not permitted
162: * in this set (which can be the case with returned
163: * subsets).
164: *
165: * @throws ClassCastException
166: * if <tt>from</tt> is not of class {@link Double Double}.
167: *
168: * @throws NullPointerException
169: * if <tt>from</tt> is <tt>null</tt>.
170: */
171: public SortedSet tailSet(Object from) {
172: return new DoubleSortedSetToSortedSetAdapter(
173: ((DoubleSortedSet) set).tailSet(((Double) from)
174: .doubleValue()));
175: }
176:
177: }
|