aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/searchers/test/MockMetric.java
blob: 7835a9934c7a6885596cb5d7029ced176ec152c6 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.searchers.test;

import com.yahoo.jdisc.Metric;

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

/**
* @author bratseth
*/
class MockMetric implements Metric {

    private Map<Context, Map<String, Number>> metrics = new HashMap<>();

    public Map<String, Number> values(Context context) {
        return metricsForContext(context);
    }

    @Override
    public void set(String key, Number val, Context context) {
        metricsForContext(context).put(key, val);
    }

    @Override
    public void add(String key, Number value, Context context) {
        Number previousValue = metricsForContext(context).get(key);
        if (previousValue == null)
            previousValue = 0;
        metricsForContext(context).put(key, value.doubleValue() + previousValue.doubleValue());
    }

    /** Returns the metrics for a given context, never null */
    private Map<String, Number> metricsForContext(Context context) {
        Map<String, Number> metricsForContext = metrics.get(context);
        if (metricsForContext == null) {
            metricsForContext = new HashMap<>();
            metrics.put(context, metricsForContext);
        }
        return metricsForContext;
    }

    @Override
    public Context createContext(Map<String, ?> dimensions) {
        return new MapContext(dimensions);
    }

    /** Creates a context containing a single dimension */
    public Metric.Context createContext(String dimensionName, String dimensionValue) {
        if (dimensionName.isEmpty())
            return createContext(Collections.emptyMap());
        return createContext(Collections.singletonMap(dimensionName, dimensionValue));
    }

    private class MapContext implements Metric.Context {

        private final Map<String, ?> dimensions;

        public MapContext(Map<String, ?> dimensions) {
            this.dimensions = dimensions;
        }

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if ( ! (o instanceof MapContext)) return false;
            return dimensions.equals(((MapContext)o).dimensions);
        }

    }

}