summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcdata/ExtendedMap.java
blob: 0c6389e1415c1b2df382dc13e77b30f73e3cbd01 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package bjc.utils.funcdata;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import bjc.utils.funcutils.ListUtils;

/**
 * An extended version of a map, that stores values into a map, but can look
 * into a different one for others.
 *
 * @author Ben Culkin
 *
 * @param <KeyType>
 * 	The type of the keys of the map.
 *
 * @param <ValueType>
 * 	The type of the values of the map.
 */
class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> {
	/* The map we delegate lookups to. */
	private final IMap<KeyType, ValueType> delegate;
	/* The map we store things in. */
	private final IMap<KeyType, ValueType> store;

	/**
	 * Create a new extended map.
	 *
	 * @param delegate
	 * 	The map to lookup things in.
	 * 
	 * @param store
	 * 	The map to store things in.
	 */
	public ExtendedMap(final IMap<KeyType, ValueType> delegate, final IMap<KeyType, ValueType> store) {
		this.delegate = delegate;
		this.store = store;
	}

	@Override
	public void clear() {
		store.clear();
	}

	@Override
	public boolean containsKey(final KeyType key) {
		if (store.containsKey(key)) return true;

		return delegate.containsKey(key);
	}

	@Override
	public IMap<KeyType, ValueType> extend() {
		return new ExtendedMap<>(this, new FunctionalMap<>());
	}

	@Override
	public void forEach(final BiConsumer<KeyType, ValueType> action) {
		store.forEach(action);

		delegate.forEach(action);
	}

	@Override
	public void forEachKey(final Consumer<KeyType> action) {
		store.forEachKey(action);

		delegate.forEachKey(action);
	}

	@Override
	public void forEachValue(final Consumer<ValueType> action) {
		store.forEachValue(action);

		delegate.forEachValue(action);
	}

	@Override
	public ValueType get(final KeyType key) {
		if (store.containsKey(key)) return store.get(key);

		return delegate.get(key);
	}

	@Override
	public int size() {
		return store.size() + delegate.size();
	}

	@Override
	public IList<KeyType> keyList() {
		return ListUtils.mergeLists(store.keyList(), delegate.keyList());
	}

	@Override
	public <MappedValue> IMap<KeyType, MappedValue> transform(final Function<ValueType, MappedValue> transformer) {
		return new TransformedValueMap<>(this, transformer);
	}

	@Override
	public ValueType put(final KeyType key, final ValueType val) {
		return store.put(key, val);
	}

	@Override
	public ValueType remove(final KeyType key) {
		if (!store.containsKey(key)) return delegate.remove(key);

		return store.remove(key);
	}

	@Override
	public IList<ValueType> valueList() {
		return ListUtils.mergeLists(store.valueList(), delegate.valueList());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (delegate == null ? 0 : delegate.hashCode());
		result = prime * result + (store == null ? 0 : store.hashCode());
		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (!(obj instanceof ExtendedMap)) return false;

		final ExtendedMap<?, ?> other = (ExtendedMap<?, ?>) obj;

		if (delegate == null) {
			if (other.delegate != null) return false;
		} else if (!delegate.equals(other.delegate)) return false;
		if (store == null) {
			if (other.store != null) return false;
		} else if (!store.equals(other.store)) return false;

		return true;
	}

	@Override
	public String toString() {
		return String.format("ExtendedMap [delegate=%s, store=%s]", delegate, store);
	}
}