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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
package bjc.esodata;
import bjc.funcdata.FunctionalMap;
import bjc.funcdata.IMap;
/**
* Simple implementation of {@link Directory}.
*
* Has a unified namespace for data and children.
*
* @author EVE
*
* @param <K>
* The key type of the directory.
*
* @param <V>
* The value type of the directory.
*/
public class UnifiedDirectory<K, V> implements Directory<K, V> {
/* Our directory children. */
private final IMap<K, Directory<K, V>> children;
/* Our data children. */
private final IMap<K, V> data;
/** Create a new directory. */
public UnifiedDirectory() {
children = new FunctionalMap<>();
data = new FunctionalMap<>();
}
@Override
public Directory<K, V> getSubdirectory(final K key) {
return children.get(key);
}
@Override
public boolean hasSubdirectory(final K key) {
return children.containsKey(key);
}
@Override
public Directory<K, V> putSubdirectory(final K key, final Directory<K, V> val) {
if (data.containsKey(key)) {
final String msg = String.format("Key %s is already used for data", key);
throw new IllegalArgumentException(msg);
}
return children.put(key, val);
}
@Override
public boolean containsKey(final K key) {
return data.containsKey(key);
}
@Override
public V getKey(final K key) {
return data.get(key);
}
@Override
public V putKey(final K key, final V val) {
if (children.containsKey(key)) {
final String msg
= String.format("Key %s is already used for sub-directories.", key);
throw new IllegalArgumentException(msg);
}
return data.put(key, val);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (children == null ? 0 : children.hashCode());
result = prime * result + (data == null ? 0 : data.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof UnifiedDirectory<?, ?>))
return false;
final UnifiedDirectory<?, ?> other = (UnifiedDirectory<?, ?>) obj;
if (children == null) {
if (other.children != null)
return false;
} else if (!children.equals(other.children))
return false;
if (data == null) {
if (other.data != null)
return false;
} else if (!data.equals(other.data))
return false;
return true;
}
@Override
public String toString() {
return String.format("UnifiedDirectory [children=%s, data=%s]", children, data);
}
}
|