summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java
blob: 378a4a0b784c0c694227d042b81102a4cfe6bb2a (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
package bjc.utils.funcdata;

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

import bjc.utils.funcutils.ListUtils;

class ExtendedMap<KeyType, ValueType>
		implements IMap<KeyType, ValueType> {
	private IMap<KeyType, ValueType>	delegate;

	private IMap<KeyType, ValueType>	store;

	public ExtendedMap(IMap<KeyType, ValueType> delegate,
			IMap<KeyType, ValueType> store) {
		this.delegate = delegate;
		this.store = store;
	}

	@Override
	public boolean containsKey(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(BiConsumer<KeyType, ValueType> action) {
		store.forEach(action);

		delegate.forEach(action);
	}

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

		delegate.forEachKey(action);
	}

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

		delegate.forEachValue(action);
	}

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

		return delegate.get(key);
	}

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

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

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

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

	@Override
	public ValueType remove(KeyType key) {
		return store.remove(key);
	}

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