blob: d4568c0dea6e6addb8a3e9f1140abd63cc01ef8f (
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
|
// Copyright 2016 Yahoo Inc. 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 <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
*/
public final class StateMetricContext implements MetricDimensions, Metric.Context {
private final Map<String, String> data;
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);
}
}
|