aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/profile/ChainedMap.java
blob: 2f36db6b0f2dd04f70787cbb4bd57103affbd459 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * A read-only map which forwards lookups to a primary map, and then a secondary for
 * keys not existing in the primary.
 *
 * @author bratseth
 */
class ChainedMap<K, V> implements Map<K, V> {

    private final Map<K, V> primary, secondary;

    ChainedMap(Map<K, V> primary, Map<K, V> secondary) {
        this.primary = primary;
        this.secondary = secondary;
    }

    @Override
    public int size() {
        return (primary.size() >= secondary.size())
                ? countUnique(primary, secondary)
                : countUnique(secondary, primary);
    }

    private int countUnique(Map<K, V> large, Map<K,V> small) {
        int size = large.size();
        for (K key : small.keySet()) {
            if ( ! large.containsKey(key)) size++;
        }
        return size;
    }

    @Override
    public boolean isEmpty() {
        return primary.isEmpty() && secondary.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return primary.containsKey(key) || secondary.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return primary.containsValue(value) || secondary.containsValue(value);
    }

    @Override
    public V get(Object key) {
        V value = primary.get(key);
        return value != null ? value : secondary.get(key);
    }

    @Override
    public V put(K key, V value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public V remove(Object key) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> m) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<K> keySet() {
        var keys = new HashSet<>(secondary.keySet());
        keys.addAll(primary.keySet());
        return keys;
    }

    @Override
    public Collection<V> values() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<Entry<K, V>> entrySet() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean equals(Object o) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int hashCode() {
        throw new UnsupportedOperationException();
    }

}