blob: a05008cc9a09093e5c1cf803f9bdeff63584407e (
plain) (
blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.utils;
import java.util.*;
/**
* Delegates to a map that can be froozen.
* Not thread safe.
* @author tonytv
*/
public class FreezableMap<K, V> implements Map<K, V> {
private boolean frozen = false;
private Map<K, V> map;
//TODO: review the use of unchecked.
@SuppressWarnings("unchecked")
public FreezableMap(Class<LinkedHashMap> mapClass) {
try {
map = mapClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public int size() {
return map.size();
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean containsKey(Object o) {
return map.containsKey(o);
}
public boolean containsValue(Object o) {
return map.containsValue(o);
}
public V get(Object o) {
return map.get(o);
}
public V put(K key, V value) {
return map.put(key, value);
}
public V remove(Object o) {
return map.remove(o);
}
public void putAll(Map<? extends K, ? extends V> map) {
this.map.putAll(map);
}
public void clear() {
map.clear();
}
public Set<K> keySet() {
return map.keySet();
}
public Collection<V> values() {
return map.values();
}
public Set<Entry<K, V>> entrySet() {
return map.entrySet();
}
public boolean equals(Object o) {
return map.equals(o);
}
public int hashCode() {
return map.hashCode();
}
public void freeze() {
if (frozen)
throw new RuntimeException("The map has already been frozen.");
frozen = true;
map = Collections.unmodifiableMap(map);
}
public boolean isFrozen() {
return frozen;
}
}
|