summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/jdisc/state/StateMetricContext.java
blob: 6d6635ecc94bd978af80351f6a55c9c341d17779 (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
// 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.Iterator;
import java.util.Map;
import java.util.Objects;

import static java.util.stream.Collectors.toUnmodifiableMap;

/**
 * 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 = (properties != null)
                ? properties.entrySet().stream().collect(
                        toUnmodifiableMap(Map.Entry::getKey, e -> Objects.requireNonNullElse(e.getValue(), "").toString()))
                : Map.of();
        return new StateMetricContext(data);
    }

}