All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.container.jdisc.state.StateMetricContext Maven / Gradle / Ivy

There is a newer version: 8.458.13
Show newest version
// Copyright 2017 Yahoo Holdings. 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 data; // effectively immutable
    private final int hashCode;

    private StateMetricContext(Map data) {
        this.data = data;
        this.hashCode = data.hashCode();
    }

    @Override
    public Iterator> 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 properties) {
        Map data;
        if (properties != null) {
            data = new HashMap<>(properties.size());
            for (Map.Entry 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);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy