-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImMap.java
55 lines (45 loc) · 1.19 KB
/
ImMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Collection;
import java.util.Iterator;
import java.util.Optional;
/**
* From
* @author cs2030
* An immutable implementation of {@code LinkedHashMap}.
*/
public class ImMap<K, V> implements Iterable<Map.Entry<K, V>> {
private final Map<K, V> map;
public ImMap() {
this.map = new LinkedHashMap<K, V>();
}
public ImMap<K, V> put(K key, V value) {
ImMap<K, V> newMap = new ImMap<K, V>();
newMap.map.putAll(this.map);
newMap.map.put(key, value);
return newMap;
}
public Set<K> keySet() {
return this.map.keySet();
}
public Collection<V> values() {
return this.map.values();
}
public Set<Map.Entry<K, V>> entrySet() {
return this.map.entrySet();
}
public Iterator<Map.Entry<K, V>> iterator() {
return this.entrySet().iterator();
}
public Optional<V> get(Object key) {
return Optional.<V>ofNullable(this.map.get(key));
}
boolean isEmpty() {
return this.map.isEmpty();
}
@Override
public String toString() {
return this.map.toString();
}
}