Javolution 6.0.0 java
FastMapImpl.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;
10 
11 import java.util.Iterator;
12 import java.util.NoSuchElementException;
13 
16 
23 public class FastMapImpl<K, V> extends MapView<K, V> {
24 
25  private static final long serialVersionUID = 0x600L; // Version.
26  transient MapEntryImpl<K, V> firstEntry = null;
30  transient MapEntryImpl<K, V> lastEntry = null;
31  transient int size;
33 
36  super(null); // Root.
37  this.keyComparator = keyComparator;
38  this.valueComparator = valueComparator;
39  }
40 
41  @Override
42  public void clear() {
43  firstEntry = null;
44  lastEntry = null;
45  fractal = new FractalMapImpl();
46  size = 0;
47  }
48 
49  @Override
50  public FastMapImpl<K, V> clone() { // Makes a copy.
52  valueComparator());
53  copy.putAll(this);
54  return copy;
55  }
56 
57  @SuppressWarnings("unchecked")
58  @Override
59  public boolean containsKey(Object key) {
60  return fractal.getEntry(key, keyComparator.hashCodeOf((K) key)) != null;
61  }
62 
63  @SuppressWarnings("unchecked")
64  @Override
65  public V get(Object key) {
67  keyComparator.hashCodeOf((K) key));
68  if (entry == null) return null;
69  return entry.value;
70  }
71 
72  @Override
73  public Iterator<Entry<K, V>> iterator() {
74  return new Iterator<Entry<K, V>>() {
75  MapEntryImpl<K, V> current;
77 
78  @Override
79  public boolean hasNext() {
80  return (next != null);
81  }
82 
83  @Override
84  public java.util.Map.Entry<K, V> next() {
85  if (next == null) throw new NoSuchElementException();
86  current = next;
87  next = next.next;
88  return current;
89  }
90 
91  @Override
92  public void remove() {
93  if (current == null) throw new IllegalStateException();
94  fractal.removeEntry(current.key, current.hash);
95  detachEntry(current); // Entry is not referenced anymore and will be gc.
96  size--;
97  }
98  };
99 
100  }
101 
102  @Override
104  return keyComparator;
105  }
106 
107  @SuppressWarnings("unchecked")
108  @Override
109  public V put(K key, V value) {
110  int hash = keyComparator.hashCodeOf(key);
111  MapEntryImpl<K, V> tmp = fractal.addEntry(freeEntry, key, hash);
112  if (tmp == freeEntry) { // New entry.
114  attachEntry(tmp);
115  size++;
116  tmp.value = value;
117  return null;
118  } else { // Existing entry.
119  V oldValue = (V) tmp.value;
120  tmp.value = value;
121  return oldValue;
122  }
123  }
124 
125  @SuppressWarnings("unchecked")
126  @Override
127  public V putIfAbsent(K key, V value) {
128  int hash = keyComparator.hashCodeOf(key);
129  MapEntryImpl<K, V> tmp = fractal.addEntry(freeEntry, key, hash);
130  if (tmp == freeEntry) { // New entry.
132  attachEntry(tmp);
133  size++;
134  tmp.value = value;
135  return null;
136  } else { // Existing entry.
137  return (V) tmp.value;
138  }
139  }
140 
141  @SuppressWarnings("unchecked")
142  @Override
143  public V remove(Object key) {
145  keyComparator.hashCodeOf((K) key));
146  if (entry == null) return null;
147  detachEntry(entry); // Entry is not referenced anymore and will be gc.
148  size--;
149  return entry.value;
150  }
151 
152  @SuppressWarnings("unchecked")
153  @Override
154  public boolean remove(Object key, Object value) {
155  int hash = keyComparator.hashCodeOf((K) key);
156  MapEntryImpl<K, V> entry = fractal.getEntry(key, hash);
157  if (entry == null) return false;
158  if (!valueComparator.areEqual((V) entry.value, (V) value)) return false;
159  fractal.removeEntry(key, hash);
160  detachEntry(entry); // Entry is not referenced anymore and will be gc.
161  size--;
162  return true;
163  }
164 
165  @SuppressWarnings("unchecked")
166  @Override
167  public V replace(K key, V value) {
168  MapEntryImpl<K, V> entry = fractal.getEntry(key,
170  if (entry == null) return null;
171  V oldValue = entry.value;
172  entry.value = value;
173  return oldValue;
174  }
175 
176  @SuppressWarnings("unchecked")
177  @Override
178  public boolean replace(K key, V oldValue, V newValue) {
179  MapEntryImpl<K, V> entry = fractal.getEntry(key,
181  if (entry == null) return false;
182  if (!valueComparator.areEqual(entry.value, oldValue)) return false;
183  entry.value = newValue;
184  return true;
185  }
186 
187  @Override
188  public int size() {
189  return size;
190  }
191 
192  @SuppressWarnings("unchecked")
193  @Override
194  public MapService<K, V>[] split(int n) {
195  return new MapService[] { this }; // No splitting supported yet.
196  }
197 
198  @Override
200  return valueComparator;
201  }
202 
203  private void attachEntry(MapEntryImpl<K, V> entry) {
204  if (lastEntry != null) {
205  lastEntry.next = entry;
206  entry.previous = lastEntry;
207  }
208  lastEntry = entry;
209  if (firstEntry == null) {
210  firstEntry = entry;
211  }
212  }
213 
214  private void detachEntry(MapEntryImpl<K, V> entry) {
215  if (entry == firstEntry) {
216  firstEntry = entry.next;
217  }
218  if (entry == lastEntry) {
219  lastEntry = entry.previous;
220  }
221  MapEntryImpl<K, V> previous = entry.previous;
222  MapEntryImpl<K, V> next = entry.next;
223  if (previous != null) {
224  previous.next = next;
225  }
226  if (next != null) {
227  next.previous = previous;
228  }
229  }
230 
232  @SuppressWarnings("unchecked")
233  private void readObject(java.io.ObjectInputStream s)
234  throws java.io.IOException, ClassNotFoundException {
235  s.defaultReadObject(); // Deserialize comparator.
236  fractal = new FractalMapImpl();
238  int n = s.readInt();
239  for (int i = 0; i < n; i++) {
240  put((K) s.readObject(), (V) s.readObject());
241  }
242  }
243 
245  private void writeObject(java.io.ObjectOutputStream s)
246  throws java.io.IOException {
247  s.defaultWriteObject(); // Serialize comparators.
248  s.writeInt(size);
249  Iterator<Entry<K, V>> it = iterator();
250  while (it.hasNext()) {
251  Entry<K, V> e = it.next();
252  s.writeObject(e.getKey());
253  s.writeObject(e.getValue());
254  }
255  }
256 
257 }
javolution.util.function.Equality.areEqual
boolean areEqual(T left, T right)
javolution.util.internal.map.MapEntryImpl
Definition: MapEntryImpl.java:16
javolution
javolution.util.internal.map.FractalMapImpl.removeEntry
MapEntryImpl removeEntry(Object key, int hash)
Definition: FractalMapImpl.java:64
javolution.util.internal.map.FastMapImpl.firstEntry
transient MapEntryImpl< K, V > firstEntry
Definition: FastMapImpl.java:26
javolution.util.service
Definition: BitSetService.java:9
javolution.util.internal.map.FastMapImpl.clear
void clear()
Definition: FastMapImpl.java:42
javolution.util.internal.map.FastMapImpl.lastEntry
transient MapEntryImpl< K, V > lastEntry
Definition: FastMapImpl.java:30
javolution.util.internal.map.FastMapImpl.fractal
transient FractalMapImpl fractal
Definition: FastMapImpl.java:27
javolution.util.internal.map.FastMapImpl.serialVersionUID
static final long serialVersionUID
Definition: FastMapImpl.java:25
javolution.util.internal.map.MapView
Definition: MapView.java:29
javolution.util.internal.map.FastMapImpl.detachEntry
void detachEntry(MapEntryImpl< K, V > entry)
Definition: FastMapImpl.java:214
javolution.util.internal.map.FastMapImpl.attachEntry
void attachEntry(MapEntryImpl< K, V > entry)
Definition: FastMapImpl.java:203
javolution.util.service.MapService
Definition: MapService.java:27
javolution.util.internal.map.FastMapImpl.valueComparator
Equality<? super V > valueComparator()
Definition: FastMapImpl.java:199
javolution.util.internal.map.FastMapImpl.freeEntry
transient MapEntryImpl< K, V > freeEntry
Definition: FastMapImpl.java:28
javolution.util.internal.map.FastMapImpl.replace
boolean replace(K key, V oldValue, V newValue)
Definition: FastMapImpl.java:178
javolution.util.function.Equality
Definition: Equality.java:39
javolution.util.internal.map.FractalMapImpl.addEntry
MapEntryImpl addEntry(MapEntryImpl newEntry, Object key, int hash)
Definition: FractalMapImpl.java:39
javolution.util.internal.map.FastMapImpl.putIfAbsent
V putIfAbsent(K key, V value)
Definition: FastMapImpl.java:127
javolution.util.internal.map.MapEntryImpl.key
K key
Definition: MapEntryImpl.java:19
javolution.util.function
Definition: Consumer.java:9
javolution.util.internal.map.FastMapImpl.clone
FastMapImpl< K, V > clone()
Definition: FastMapImpl.java:50
javolution.util.internal.map.FractalMapImpl
Definition: FractalMapImpl.java:18
javolution.util.internal.map.FastMapImpl.valueComparator
final Equality<? super V > valueComparator
Definition: FastMapImpl.java:32
javolution.util.internal.map.FastMapImpl.FastMapImpl
FastMapImpl(Equality<? super K > keyComparator, final Equality<? super V > valueComparator)
Definition: FastMapImpl.java:34
javolution.util.internal.map.FastMapImpl.put
V put(K key, V value)
Definition: FastMapImpl.java:109
javolution.util.internal.map.MapEntryImpl.hash
int hash
Definition: MapEntryImpl.java:18
javolution.util.internal.map.FastMapImpl.size
int size()
Definition: FastMapImpl.java:188
javolution.util.internal.map.FastMapImpl.readObject
void readObject(java.io.ObjectInputStream s)
Definition: FastMapImpl.java:233
javolution.util.internal.map.MapEntryImpl.next
MapEntryImpl< K, V > next
Definition: MapEntryImpl.java:20
javolution.util.internal.map.FastMapImpl.keyComparator
final Equality<? super K > keyComparator
Definition: FastMapImpl.java:29
javolution.util.internal.map.FastMapImpl.split
MapService< K, V >[] split(int n)
Definition: FastMapImpl.java:194
javolution.util.internal.map.MapView.putAll
void putAll(Map<? extends K, ? extends V > m)
Definition: MapView.java:290
javolution.util.function.Equality.hashCodeOf
int hashCodeOf(T object)
javolution.util.internal.map.MapEntryImpl.value
V value
Definition: MapEntryImpl.java:22
javolution.util.internal.map.FractalMapImpl.getEntry
MapEntryImpl getEntry(Object key, int hash)
Definition: FractalMapImpl.java:59
javolution.util.internal.map.FastMapImpl
Definition: FastMapImpl.java:23
javolution.util.internal.map.MapEntryImpl.previous
MapEntryImpl< K, V > previous
Definition: MapEntryImpl.java:21
javolution.util.internal.map.FastMapImpl.size
transient int size
Definition: FastMapImpl.java:31
javolution.util.internal.map.FastMapImpl.keyComparator
Equality<? super K > keyComparator()
Definition: FastMapImpl.java:103
javolution.util
Definition: FastBitSet.java:9
javolution.util.internal.map.FastMapImpl.writeObject
void writeObject(java.io.ObjectOutputStream s)
Definition: FastMapImpl.java:245
javolution.util.internal.map.FastMapImpl.iterator
Iterator< Entry< K, V > > iterator()
Definition: FastMapImpl.java:73
javolution.util.internal.map.FastMapImpl.containsKey
boolean containsKey(Object key)
Definition: FastMapImpl.java:59
javolution.util.internal.map.FastMapImpl.replace
V replace(K key, V value)
Definition: FastMapImpl.java:167