Javolution 6.0.0 java
SubSortedSetImpl.java
Go to the documentation of this file.
1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2012 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */
9 package javolution.util.internal.set.sorted;
10 
11 import java.util.Iterator;
12 import java.util.NoSuchElementException;
13 
16 
20 public class SubSortedSetImpl<E> extends SortedSetView<E> {
21 
23  private class IteratorImpl implements Iterator<E> {
24 
25  private boolean ahead;
26  private final Equality<? super E> cmp = comparator();
27  private E next;
28  private final Iterator<E> targetIterator = target().iterator();
29 
30  @Override
31  public boolean hasNext() {
32  if (ahead) return true;
33  while (targetIterator.hasNext()) {
34  next = targetIterator.next();
35  if ((from != null) && (cmp.compare(next, from) < 0)) continue;
36  if ((to != null) && (cmp.compare(next, to) >= 0)) break;
37  ahead = true;
38  return true;
39  }
40  return false;
41  }
42 
43  @Override
44  public E next() {
45  hasNext(); // Moves ahead.
46  ahead = false;
47  return next;
48  }
49 
50  @Override
51  public void remove() {
52  targetIterator.remove();
53  }
54  }
55 
56  private static final long serialVersionUID = 0x600L; // Version.
57  private final E from; // Can be null.
58  private final E to; // Can be null.
59 
61  super(target);
62  if ((from != null) && (to != null)
63  && (comparator().compare(from, to) > 0)) throw new IllegalArgumentException(
64  "from: " + from + ", to: " + to); // As per SortedSet contract.
65  this.from = from;
66  this.to = to;
67  }
68 
69  @Override
70  public boolean add(E e) {
72  if ((from != null) && (cmp.compare(e, from) < 0)) throw new IllegalArgumentException(
73  "Element: " + e + " outside of this sub-set bounds");
74  if ((to != null) && (cmp.compare(e, to) >= 0)) throw new IllegalArgumentException(
75  "Element: " + e + " outside of this sub-set bounds");
76  return target().add(e);
77  }
78 
79  @Override
81  return target().comparator();
82  }
83 
84  @SuppressWarnings("unchecked")
85  @Override
86  public boolean contains(Object obj) {
88  if ((from != null) && (cmp.compare((E) obj, from) < 0)) return false;
89  if ((to != null) && (cmp.compare((E) obj, to) >= 0)) return false;
90  return target().contains(obj);
91  }
92 
93  @Override
94  public E first() {
95  if (from == null) return target().first();
96  Iterator<E> it = iterator();
97  if (!it.hasNext()) throw new NoSuchElementException();
98  return it.next();
99  }
100 
101  @Override
102  public boolean isEmpty() {
103  return iterator().hasNext();
104  }
105 
106  @Override
107  public Iterator<E> iterator() {
108  return new IteratorImpl();
109  }
110 
111  @Override
112  public E last() {
113  if (to == null) return target().last();
114  Iterator<E> it = iterator();
115  if (!it.hasNext()) throw new NoSuchElementException();
116  E last = it.next();
117  while (it.hasNext()) {
118  last = it.next();
119  }
120  return last;
121  }
122 
123  @SuppressWarnings("unchecked")
124  @Override
125  public boolean remove(Object obj) {
127  if ((from != null) && (cmp.compare((E) obj, from) < 0)) return false;
128  if ((to != null) && (cmp.compare((E) obj, to) >= 0)) return false;
129  return target().remove(obj);
130  }
131 
132  @Override
133  public int size() { // Unfortunately, no choice other than counting.
134  int count = 0;
135  Iterator<E> it = iterator();
136  while (it.hasNext()) {
137  count++;
138  it.next();
139  }
140  return count;
141  }
142 
143 }
javolution.util.internal.set.sorted.SubSortedSetImpl.to
final E to
Definition: SubSortedSetImpl.java:58
javolution
javolution.util.service
Definition: BitSetService.java:9
javolution.util.service.SortedSetService
Definition: SortedSetService.java:19
javolution.util.internal.set.sorted.SubSortedSetImpl
Definition: SubSortedSetImpl.java:20
javolution.util.internal.set.sorted.SubSortedSetImpl.size
int size()
Definition: SubSortedSetImpl.java:133
javolution.util.internal.set.sorted.SubSortedSetImpl.comparator
Equality<? super E > comparator()
Definition: SubSortedSetImpl.java:80
javolution.util.internal.set.sorted.SubSortedSetImpl.iterator
Iterator< E > iterator()
Definition: SubSortedSetImpl.java:107
javolution.util.internal.set.sorted.SubSortedSetImpl.IteratorImpl.cmp
final Equality<? super E > cmp
Definition: SubSortedSetImpl.java:26
javolution.util.internal.set.sorted.SubSortedSetImpl.IteratorImpl.targetIterator
final Iterator< E > targetIterator
Definition: SubSortedSetImpl.java:28
javolution.util.internal.set.sorted.SortedSetView
Definition: SortedSetView.java:18
javolution.util.internal.set.sorted.SubSortedSetImpl.IteratorImpl
Definition: SubSortedSetImpl.java:23
javolution.util.internal.set.sorted.SubSortedSetImpl.serialVersionUID
static final long serialVersionUID
Definition: SubSortedSetImpl.java:56
javolution.util.function.Equality
Definition: Equality.java:39
javolution.util.internal.set.sorted.SubSortedSetImpl.isEmpty
boolean isEmpty()
Definition: SubSortedSetImpl.java:102
javolution.util.function
Definition: Consumer.java:9
javolution.util.internal.set.sorted.SubSortedSetImpl.add
boolean add(E e)
Definition: SubSortedSetImpl.java:70
javolution.util.internal.set.sorted.SortedSetView.target
SortedSetService< E > target()
Definition: SortedSetView.java:56
javolution.util.internal.set.sorted.SubSortedSetImpl.first
E first()
Definition: SubSortedSetImpl.java:94
javolution.util.internal.set.sorted.SubSortedSetImpl.IteratorImpl.hasNext
boolean hasNext()
Definition: SubSortedSetImpl.java:31
javolution.util.internal.set.sorted.SubSortedSetImpl.from
final E from
Definition: SubSortedSetImpl.java:57
javolution.util.internal.set.sorted.SubSortedSetImpl.IteratorImpl.next
E next()
Definition: SubSortedSetImpl.java:44
javolution.util.function.Equality.compare
int compare(T left, T right)
javolution.util.internal.set.sorted.SubSortedSetImpl.last
E last()
Definition: SubSortedSetImpl.java:112
javolution.util.internal.set.sorted.SubSortedSetImpl.contains
boolean contains(Object obj)
Definition: SubSortedSetImpl.java:86
javolution.util.internal.set.sorted.SubSortedSetImpl.SubSortedSetImpl
SubSortedSetImpl(SortedSetService< E > target, E from, E to)
Definition: SubSortedSetImpl.java:60
javolution.util.internal.set.sorted.SubSortedSetImpl.IteratorImpl.ahead
boolean ahead
Definition: SubSortedSetImpl.java:25
javolution.util.internal.set.sorted.SubSortedSetImpl.IteratorImpl.next
E next
Definition: SubSortedSetImpl.java:27
javolution.util
Definition: FastBitSet.java:9