1 package net
.kezvh
.collections
;
3 import java
.util
.Collection
;
4 import java
.util
.Comparator
;
5 import java
.util
.Iterator
;
6 import java
.util
.LinkedList
;
8 import java
.util
.ListIterator
;
9 import java
.util
.NavigableSet
;
10 import java
.util
.NoSuchElementException
;
11 import java
.util
.SortedSet
;
13 import net
.kezvh
.collections
.unmodifiable
.AbstractUnmodifiableList
;
14 import net
.kezvh
.development
.UnimplementedException
;
18 * @param <E> FIXME commment
20 public class Range
<E
> extends AbstractUnmodifiableList
<E
> implements NavigableSet
<E
> {
22 private final class RangeIterator
implements ListIterator
<E
> {
26 public RangeIterator() {
27 this(0, Range
.this.firstOpen ? Range
.this.navigator
.next(Range
.this.first
) : Range
.this.first
);
30 public RangeIterator(final int index
, final E current
) {
33 this.current
= current
;
37 public void add(final E e
) {
38 throw new UnsupportedOperationException();
42 public boolean hasNext() {
43 return Range
.this.contains(this.current
);
47 public boolean hasPrevious() {
48 return Range
.this.contains(Range
.this.navigator
.previous(this.current
));
58 this.current
= Range
.this.navigator
.next(this.current
);
60 throw new NoSuchElementException();
64 public int nextIndex() {
70 if (this.hasPrevious())
72 return Range
.this.navigator
.previous(this.current
);
75 this.current
= Range
.this.navigator
.previous(this.current
);
77 throw new NoSuchElementException();
81 public int previousIndex() {
82 return this.index
- 1;
86 public void remove() {
87 throw new UnsupportedOperationException();
91 public void set(final E e
) {
92 throw new UnsupportedOperationException();
96 private final Navigator
<E
> navigator
;
97 private final boolean directlyNavigable
;
99 private final E first
;
100 private final E last
;
102 private final boolean firstOpen
;
103 private final boolean lastOpen
;
106 * @param first first element
107 * @param last last element
109 public Range(final E first
, final E last
) {
110 this(first
, last
, true, false, new Navigator
<E
>() {
111 @SuppressWarnings("unchecked")
112 public int compare(final E o1
, final E o2
) {
113 return ((Comparable
<E
>) o1
).compareTo(o2
);
116 @SuppressWarnings("unchecked")
117 public E
next(final E current
) {
118 return ((Navigable
<E
>) current
).next();
121 @SuppressWarnings("unchecked")
122 public E
previous(final E current
) {
123 return ((Navigable
<E
>) current
).previous();
129 * @param first first element
130 * @param last last element
131 * @param firstOpen exclude the first element. ignored if the first element is null.
132 * @param lastOpen exclude the last element. ignored if the first element is null.
133 * @param navigator navigator
135 public Range(final E first
, final E last
, final boolean firstOpen
, final boolean lastOpen
, final Navigator
<E
> navigator
) {
136 throw new RuntimeException("too buggy to use");
137 // this.first = first;
140 // if (navigator == null && first instanceof Number)
141 // this.navigator = Navigator.Utilities.getNumberNavigator(first.getClass());
143 // this.navigator = navigator;
144 // this.firstOpen = firstOpen;
145 // this.lastOpen = lastOpen;
147 // this.directlyNavigable = navigator != null;
149 // if (first == null || (this.directlyNavigable && !(first instanceof Navigable) && !(first instanceof Number)))
150 // throw new IllegalArgumentException(first + " is not comparable");
152 // if (last == null || (this.directlyNavigable && !(last instanceof Navigable) && !(first instanceof Number)))
153 // throw new IllegalArgumentException(last + " is not comparable");
157 * @see java.util.SortedSet#comparator()
160 public Comparator
<?
super E
> comparator() {
161 return this.navigator
;
165 * @see java.util.Collection#contains(java.lang.Object)
169 @SuppressWarnings("unchecked")
171 public boolean contains(final Object o
) {
172 final int f
= this.navigator
.compare(this.first
, (E
) o
);
173 final int l
= this.navigator
.compare((E
) o
, this.last
);
175 if ((f
> 0 && this.firstOpen
) || f
== 0) {
184 * @see java.util.Collection#containsAll(java.util.Collection)
185 * @param c other collection
186 * @return containsAll
189 public boolean containsAll(final Collection
<?
> c
) {
190 for (final Object o
: c
)
191 if (!this.contains(o
))
197 * @see java.util.SortedSet#first()
205 * @see java.util.SortedSet#headSet(java.lang.Object)
206 * @param toElement COMMENT
209 public SortedSet
<E
> headSet(final E toElement
) {
210 return new Range
<E
>(this.first
, toElement
, this.firstOpen
, this.lastOpen
, this.navigator
);
214 * @see java.util.Collection#isEmpty()
218 public boolean isEmpty() {
219 if (this.firstOpen
&& this.lastOpen
)
220 return this.navigator
.compare(this.navigator
.next(this.first
), this.last
) > 0;
221 if (this.firstOpen
|| this.lastOpen
)
222 return this.navigator
.compare(this.first
, this.last
) > 0;
223 return this.navigator
.compare(this.first
, this.last
) >= 0;
227 * @see java.util.Collection#iterator()
231 public Iterator
<E
> iterator() {
232 return new RangeIterator();
236 * @see java.util.SortedSet#last()
244 * @see java.util.Collection#size()
249 return CollectionsUtilities
.count(this.iterator());
253 * @see java.util.SortedSet#subSet(java.lang.Object, java.lang.Object)
254 * @param fromElement COMMENT
255 * @param toElement COMMENT
258 public SortedSet
<E
> subSet(final E fromElement
, final E toElement
) {
259 return new Range
<E
>(fromElement
, toElement
, this.firstOpen
, this.lastOpen
, this.navigator
);
263 * @see java.util.SortedSet#tailSet(java.lang.Object)
264 * @param fromElement COMMENT
265 * @return fromelement
267 public SortedSet
<E
> tailSet(final E fromElement
) {
268 return new Range
<E
>(fromElement
, this.last
, this.firstOpen
, this.lastOpen
, this.navigator
);
272 * @see java.util.Collection#toArray()
276 public Object
[] toArray() {
277 return new LinkedList
<E
>(this).toArray();
281 * @see java.util.Collection#toArray(T[])
287 public <T
> T
[] toArray(final T
[] a
) {
288 return new LinkedList
<E
>(this).toArray(a
);
292 public E
ceiling(final E e
) {
293 final E ceiling
= this.higher(e
);
294 if (ceiling
== null && this.contains(e
))
300 public Iterator
<E
> descendingIterator() {
301 throw new UnimplementedException(); // FIXME
305 public NavigableSet
<E
> descendingSet() {
306 throw new UnimplementedException(); // FIXME
310 public E
floor(final E e
) {
311 final E floor
= this.lower(e
);
312 if (floor
== null && this.contains(e
))
318 public NavigableSet
<E
> headSet(final E toElement
, final boolean inclusive
) {
319 throw new UnimplementedException(); // FIXME
323 public E
higher(final E e
) {
324 final E higher
= this.navigator
.next(e
);
325 if (this.contains(higher
))
331 public E
lower(final E e
) {
332 final E lower
= this.navigator
.previous(e
);
333 if (this.contains(lower
))
339 public E
pollFirst() {
340 throw new UnimplementedException(); // FIXME
344 public E
pollLast() {
345 throw new UnimplementedException(); // FIXME
349 public NavigableSet
<E
> subSet(final E fromElement
, final boolean fromInclusive
, final E toElement
, final boolean toInclusive
) {
350 throw new UnimplementedException(); // FIXME
354 public NavigableSet
<E
> tailSet(final E fromElement
, final boolean inclusive
) {
355 throw new UnimplementedException(); // FIXME
359 public E
get(final int index
) {
360 throw new UnimplementedException(); // FIXME
364 public int indexOf(final Object o
) {
365 throw new UnimplementedException(); // FIXME
369 public int lastIndexOf(final Object o
) {
370 throw new UnimplementedException(); // FIXME
374 public ListIterator
<E
> listIterator() {
375 return new RangeIterator();
379 public ListIterator
<E
> listIterator(final int index
) {
380 return new RangeIterator(index
, this.get(index
));
384 public List
<E
> subList(final int fromIndex
, final int toIndex
) {
385 return new Range
<E
>(this.get(fromIndex
), this.get(toIndex
), false, true, this.navigator
);