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.IntIterator;
022: import bak.pcj.set.IntSortedSet;
023: import bak.pcj.set.AbstractIntSet;
024: import bak.pcj.adapter.IteratorToIntIteratorAdapter;
025:
026: import java.util.SortedSet;
027:
028: /**
029: * This class represents adaptions of Java Collections Framework
030: * sets to primitive sets of int values.
031: * The adapter is implemented as a wrapper around the set.
032: * Thus, changes to the underlying set are reflected by this
033: * set and vice versa.
034: *
035: * <p>
036: * Adapters from JCF collections to primitive collections will
037: * fail if the JCF collection contains <tt>null</tt> values or
038: * values of the wrong class. However, adapters are not fast
039: * failing in the case that the underlying collection should
040: * contain illegal values. To implement fast failure would require
041: * every operation to check every element of the underlying
042: * collection before doing anything. Instead validation methods
043: * are provided. They can be called using the assertion facility
044: * in the client code:
045: * <pre>
046: * SortedSetToIntSortedSetAdapter s;
047: * ...
048: * <b>assert</b> s.validate();
049: * </pre>
050: * or by letting the adapter throw an exception on illegal values:
051: * <pre>
052: * SortedSetToIntSortedSetAdapter s;
053: * ...
054: * s.evalidate(); // Throws an exception on illegal values
055: * </pre>
056: * Either way, validation must be invoked directly by the client
057: * code.
058: *
059: * @author Søren Bak
060: * @version 1.0 20-08-2003 23:16
061: * @since 1.2
062: */
063: public class SortedSetToIntSortedSetAdapter extends SetToIntSetAdapter
064: implements IntSortedSet {
065:
066: /**
067: * Creates a new adaption to a set of int
068: * values.
069: *
070: * @param set
071: * the underlying set. This set must
072: * consist of values of class
073: * {@link Integer Integer}. Otherwise a
074: * {@link ClassCastException ClassCastException}
075: * will be thrown by some methods.
076: *
077: * @throws NullPointerException
078: * if <tt>set</tt> is <tt>null</tt>.
079: */
080: public SortedSetToIntSortedSetAdapter(SortedSet set) {
081: super (set);
082: }
083:
084: /**
085: * Creates a new adaption to a set of int
086: * values. The set to adapt is optionally validated.
087: *
088: * @param set
089: * the underlying set. This set must
090: * consist of values of class
091: * {@link Integer Integer}. Otherwise a
092: * {@link ClassCastException ClassCastException}
093: * will be thrown by some methods.
094: *
095: * @param validate
096: * indicates whether <tt>set</tt> should
097: * be checked for illegal values.
098: *
099: * @throws NullPointerException
100: * if <tt>set</tt> is <tt>null</tt>.
101: *
102: * @throws IllegalStateException
103: * if <tt>validate</tt> is <tt>true</tt> and
104: * <tt>set</tt> contains a <tt>null</tt> value
105: * or a value that is not of class
106: * {@link Integer Integer}.
107: */
108: public SortedSetToIntSortedSetAdapter(SortedSet set,
109: boolean validate) {
110: super (set, validate);
111: }
112:
113: public int first() {
114: return ((Integer) (((SortedSet) set).first())).intValue();
115: }
116:
117: public IntSortedSet headSet(int to) {
118: return new SortedSetToIntSortedSetAdapter(((SortedSet) set)
119: .headSet(new Integer(to)));
120: }
121:
122: public int last() {
123: return ((Integer) (((SortedSet) set).last())).intValue();
124: }
125:
126: public IntSortedSet subSet(int from, int to) {
127: return new SortedSetToIntSortedSetAdapter(((SortedSet) set)
128: .subSet(new Integer(from), new Integer(to)));
129: }
130:
131: public IntSortedSet tailSet(int from) {
132: return new SortedSetToIntSortedSetAdapter(((SortedSet) set)
133: .tailSet(new Integer(from)));
134: }
135:
136: }
|