Javolution 6.0.0 java
SubSortedMapImpl.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.map.sorted;
10 
11 import java.util.Iterator;
12 import java.util.NoSuchElementException;
13 
16 
20 public class SubSortedMapImpl<K, V> extends SortedMapView<K, V> {
21 
23  private class IteratorImpl implements Iterator<Entry<K, V>> {
24 
25  private boolean ahead;
27  private Entry<K, V> next;
28  private final Iterator<Entry<K, V>> targetIterator = target()
29  .iterator();
30 
31  @Override
32  public boolean hasNext() {
33  if (ahead) return true;
34  while (targetIterator.hasNext()) {
35  next = targetIterator.next();
36  if ((from != null) && (cmp.compare(next.getKey(), from) < 0)) continue;
37  if ((to != null) && (cmp.compare(next.getKey(), to) >= 0)) break;
38  ahead = true;
39  return true;
40  }
41  return false;
42  }
43 
44  @Override
45  public Entry<K, V> next() {
46  hasNext(); // Moves ahead.
47  ahead = false;
48  return next;
49  }
50 
51  @Override
52  public void remove() {
53  targetIterator.remove();
54  }
55  }
56 
57  private static final long serialVersionUID = 0x600L; // Version.
58 
59  private final K from; // Can be null.
60  private final K to; // Can be null.
61 
63  super(target);
64  if ((from != null) && (to != null)
65  && (keyComparator().compare(from, to) > 0)) throw new IllegalArgumentException(
66  "from: " + from + ", to: " + to); // As per SortedSet contract.
67  this.from = from;
68  this.to = to;
69  }
70 
71  @SuppressWarnings("unchecked")
72  @Override
73  public boolean containsKey(Object key) {
75  if ((from != null) && (cmp.compare((K) key, from) < 0)) return false;
76  if ((to != null) && (cmp.compare((K) key, to) >= 0)) return false;
77  return target().containsKey(key);
78  }
79 
80  @Override
81  public K firstKey() {
82  if (from == null) return target().firstKey();
83  Iterator<Entry<K, V>> it = iterator();
84  if (!it.hasNext()) throw new NoSuchElementException();
85  return it.next().getKey();
86  }
87 
88  @SuppressWarnings("unchecked")
89  @Override
90  public V get(Object key) {
92  if ((from != null) && (cmp.compare((K) key, from) < 0)) return null;
93  if ((to != null) && (cmp.compare((K) key, to) >= 0)) return null;
94  return target().get(key);
95  }
96 
97  @Override
98  public Iterator<Entry<K, V>> iterator() {
99  return new IteratorImpl();
100  }
101 
102  @Override
104  return target().keyComparator();
105  }
106 
107  @Override
108  public K lastKey() {
109  if (to == null) return target().lastKey();
110  Iterator<Entry<K, V>> it = iterator();
111  if (!it.hasNext()) throw new NoSuchElementException();
112  Entry<K, V> last = it.next();
113  while (it.hasNext()) {
114  last = it.next();
115  }
116  return last.getKey();
117  }
118 
119  @Override
120  public V put(K key, V value) {
122  if ((from != null) && (cmp.compare((K) key, from) < 0)) throw new IllegalArgumentException(
123  "Key: " + key + " outside of this sub-map bounds");
124  if ((to != null) && (cmp.compare((K) key, to) >= 0)) throw new IllegalArgumentException(
125  "Key: " + key + " outside of this sub-map bounds");
126  return target().put(key, value);
127  }
128 
129  @SuppressWarnings("unchecked")
130  @Override
131  public V remove(Object key) {
133  if ((from != null) && (cmp.compare((K) key, from) < 0)) return null;
134  if ((to != null) && (cmp.compare((K) key, to) >= 0)) return null;
135  return target().remove(key);
136  }
137 
138  @Override
140  return target().valueComparator();
141  }
142 
143 }
javolution.util.internal.map.sorted.SubSortedMapImpl.containsKey
boolean containsKey(Object key)
Definition: SubSortedMapImpl.java:73
javolution.util.internal.map.sorted.SubSortedMapImpl.from
final K from
Definition: SubSortedMapImpl.java:59
javolution.util.internal.map.sorted.SubSortedMapImpl.keyComparator
Equality<? super K > keyComparator()
Definition: SubSortedMapImpl.java:103
javolution
javolution.util.service
Definition: BitSetService.java:9
javolution.util.internal.map.sorted.SubSortedMapImpl.iterator
Iterator< Entry< K, V > > iterator()
Definition: SubSortedMapImpl.java:98
javolution.util.internal.map.sorted.SubSortedMapImpl.IteratorImpl
Definition: SubSortedMapImpl.java:23
javolution.util.internal.map.sorted.SubSortedMapImpl.put
V put(K key, V value)
Definition: SubSortedMapImpl.java:120
javolution.util.internal.map.sorted.SubSortedMapImpl.valueComparator
Equality<? super V > valueComparator()
Definition: SubSortedMapImpl.java:139
javolution.util.service.SortedMapService
Definition: SortedMapService.java:21
javolution.util.internal.map.sorted.SubSortedMapImpl.IteratorImpl.ahead
boolean ahead
Definition: SubSortedMapImpl.java:25
javolution.util.internal.map.sorted.SubSortedMapImpl.IteratorImpl.targetIterator
final Iterator< Entry< K, V > > targetIterator
Definition: SubSortedMapImpl.java:28
javolution.util.internal.map.sorted.SubSortedMapImpl.IteratorImpl.next
Entry< K, V > next
Definition: SubSortedMapImpl.java:27
javolution.util.internal.map.sorted.SubSortedMapImpl
Definition: SubSortedMapImpl.java:20
javolution.util.function.Equality
Definition: Equality.java:39
javolution.util.internal.map.sorted.SubSortedMapImpl.IteratorImpl.cmp
final Equality<? super K > cmp
Definition: SubSortedMapImpl.java:26
javolution.util.internal.map.sorted.SubSortedMapImpl.SubSortedMapImpl
SubSortedMapImpl(SortedMapService< K, V > target, K from, K to)
Definition: SubSortedMapImpl.java:62
javolution.util.internal.map.sorted.SubSortedMapImpl.IteratorImpl.next
Entry< K, V > next()
Definition: SubSortedMapImpl.java:45
javolution.util.internal.map.sorted.SubSortedMapImpl.IteratorImpl.hasNext
boolean hasNext()
Definition: SubSortedMapImpl.java:32
javolution.util.internal.map.sorted.SubSortedMapImpl.firstKey
K firstKey()
Definition: SubSortedMapImpl.java:81
javolution.util.function
Definition: Consumer.java:9
javolution.util.internal.map.sorted.SubSortedMapImpl.to
final K to
Definition: SubSortedMapImpl.java:60
javolution.util.function.Equality.compare
int compare(T left, T right)
javolution.util.internal.map.sorted.SubSortedMapImpl.lastKey
K lastKey()
Definition: SubSortedMapImpl.java:108
javolution.util.internal.map.sorted.SubSortedMapImpl.serialVersionUID
static final long serialVersionUID
Definition: SubSortedMapImpl.java:57
javolution.util.internal.map.sorted.SortedMapView.target
SortedMapService< K, V > target()
Definition: SortedMapView.java:154
javolution.util.internal.map.sorted.SortedMapView
Definition: SortedMapView.java:24
javolution.util
Definition: FastBitSet.java:9