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

com.opentext.ia.sdk.sip.Counters Maven / Gradle / Ivy

There is a newer version: 12.8.4
Show newest version
/*
 * Copyright (c) 2016-2017 by OpenText Corporation. All Rights Reserved.
 */
package com.opentext.ia.sdk.sip;

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

/**
 * Set of related counters.
 */
public class Counters {

  private final Map values;

  public Counters() {
    this(new HashMap<>());
  }

  Counters(Map values) {
    this.values = values;
  }

  public long get(String name) {
    Long result = values.get(name);
    return result == null ? 0 : result.longValue();
  }

  /**
   * Increase the metric with the given name by one.
   * @param name The name of the metric
   */
  public void inc(String name) {
    inc(name, 1);
  }

  /**
   * Increase the metric with the given name by the given amount.
   * @param name The name of the metric
   * @param delta The amount to increase the metric with
   */
  public void inc(String name, long delta) {
    set(name, get(name) + delta);
  }

  /**
   * Set the metric with the given name to the given value.
   * @param name The name of the metric
   * @param value The value to set the metric to
   */
  public void set(String name, long value) {
    values.put(name, value);
  }

  /**
   * Reset all metrics to zero.
   */
  public void reset() {
    values.clear();
  }

  public Counters forReading() {
    return new Counters(Collections.unmodifiableMap(new HashMap(values)));
  }

  @Override
  public String toString() {
    return values.toString();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy