aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/jdisc/state/StateMetricContext.java
blob: 4cb1965384860e1cd3a258999fc64fc9de206897 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.state;

import com.yahoo.jdisc.Metric;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * A context implementation whose identity is the key and values such that this can be used as
 * a key in metrics lookups.
 *
 * @author Simon Thoresen Hult
 */
public final class StateMetricContext implements MetricDimensions, Metric.Context {

    private final Map<String, String> data; // effectively immutable
    private final int hashCode;

    private StateMetricContext(Map<String, String> data) {
        this.data = data;
        this.hashCode = data.hashCode();
    }

    @Override
    public Iterator<Map.Entry<String, String>> iterator() {
        return data.entrySet().iterator();
    }

    @Override
    public int hashCode() {
        return hashCode;
    }

    @Override
    public boolean equals(Object obj) {
        return (obj == this) ||
               (obj instanceof StateMetricContext && ((StateMetricContext)obj).data.equals(data));
    }

    public static StateMetricContext newInstance(Map<String, ?> properties) {
        Map<String, String> data;
        if (properties != null) {
            data = new HashMap<>(properties.size());
            for (Map.Entry<String, ?> entry : properties.entrySet()) {
                data.put(entry.getKey(), entry.getValue() != null ? entry.getValue().toString() : null);
            }
            data = Collections.unmodifiableMap(data);
        } else {
            data = Collections.emptyMap();
        }
        return new StateMetricContext(data);
    }

}