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.ByteSortedSet;
022:
023: import java.util.Comparator;
024: import java.util.AbstractSet;
025: import java.util.SortedSet;
026:
027: /**
028: * This class represents adapters of byte 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 ByteSortedSet
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 ByteSortedSetToSortedSetAdapter extends
042: ByteSetToSetAdapter implements SortedSet {
043:
044: /**
045: * Creates a new adaption of a set of byte
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 ByteSortedSetToSortedSetAdapter(ByteSortedSet 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 Byte(((ByteSortedSet) 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 Byte Byte}.
096: *
097: * @throws NullPointerException
098: * if <tt>to</tt> is <tt>null</tt>.
099: */
100: public SortedSet headSet(Object to) {
101: return new ByteSortedSetToSortedSetAdapter(
102: ((ByteSortedSet) set).headSet(((Byte) to).byteValue()));
103: }
104:
105: /**
106: * Returns the highest element of this set.
107: *
108: * @return the highest element of this set.
109: *
110: * @throws NoSuchElementException
111: * if this set is empty.
112: */
113: public Object last() {
114: return new Byte(((ByteSortedSet) set).last());
115: }
116:
117: /**
118: * Returns the subset of values lower that a specified value and
119: * higher than or equal to another specified value.
120: * The returned subset is a view of this set, so changes to the
121: * subset are reflected by this set and vice versa.
122: *
123: * @param from
124: * the lower bound of the returned set (included).
125: *
126: * @param to
127: * the upper bound of the returned set (not included).
128: *
129: * @throws IllegalArgumentException
130: * if <tt>from</tt> is greater than <tt>to</tt>;
131: * if <tt>from</tt> or <tt>to</tt> is not permitted
132: * in this set (which can be the case with returned
133: * subsets).
134: *
135: * @throws ClassCastException
136: * if <tt>from</tt> is not of class {@link Byte Byte};
137: * if <tt>to</tt> is not of class {@link Byte Byte}.
138: *
139: * @throws NullPointerException
140: * if <tt>from</tt> is <tt>null</tt>;
141: * if <tt>to</tt> is <tt>null</tt>.
142: */
143: public SortedSet subSet(Object from, Object to) {
144: byte tfrom = ((Byte) from).byteValue();
145: byte tto = ((Byte) to).byteValue();
146: return new ByteSortedSetToSortedSetAdapter(
147: ((ByteSortedSet) set).subSet(tfrom, tto));
148: }
149:
150: /**
151: * Returns the subset of values higher than or equal to a
152: * specified value.
153: * The returned subset is a view of this set, so changes to the
154: * subset are reflected by this set and vice versa.
155: *
156: * @param from
157: * the lower bound of the returned set (included).
158: *
159: * @throws IllegalArgumentException
160: * if <tt>from</tt> is not permitted
161: * in this set (which can be the case with returned
162: * subsets).
163: *
164: * @throws ClassCastException
165: * if <tt>from</tt> is not of class {@link Byte Byte}.
166: *
167: * @throws NullPointerException
168: * if <tt>from</tt> is <tt>null</tt>.
169: */
170: public SortedSet tailSet(Object from) {
171: return new ByteSortedSetToSortedSetAdapter(
172: ((ByteSortedSet) set)
173: .tailSet(((Byte) from).byteValue()));
174: }
175:
176: }
|