// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricsUtils.java
// static final int DEFAULT_CONCURRENCY_LEVEL = 16;
// 
// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricsUtils.java
// static <K, V> ConcurrentHashMap<K, V> newConcurrentHashMap(int concurrencyLevel) {
//   return new ConcurrentHashMap<>(HASHMAP_INITIAL_CAPACITY, HASHMAP_LOAD_FACTOR, concurrencyLevel);
// }
// 
// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricSchema.java
// public enum Kind {
//   CUMULATIVE,
//   GAUGE,
// }


import static com.google.common.base.Preconditions.checkArgument;
import static com.google.monitoring.metrics.MetricsUtils.DEFAULT_CONCURRENCY_LEVEL;
import static com.google.monitoring.metrics.MetricsUtils.newConcurrentHashMap;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;
import com.google.common.util.concurrent.AtomicLongMap;
import com.google.common.util.concurrent.Striped;
import com.google.monitoring.metrics.MetricSchema.Kind;
import java.time.Instant;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import javax.annotation.concurrent.ThreadSafe;
// Copyright 2017 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.monitoring.metrics;



/**
 * A metric which stores Long values. It is stateful and can be changed in increments.
 *
 * <p>This metric is generally suitable for counters, such as requests served or errors generated.
 *
 * <p>The start of the {@link MetricPoint#interval()} of values of instances of this metric will be
 * set to the time that the metric was first set or last {@link #reset()}.
 */
@ThreadSafe
public final class Counter extends AbstractMetric<Long>
    implements SettableMetric<Long>, IncrementableMetric {

  /**
   * A map of the {@link Counter} values, with a list of label values as the keys.
   *
   * <p>This should be modified in a critical section with {@code valueStartTimestamps} so that the
   * values are in sync.
   */
  private final AtomicLongMap<ImmutableList<String>> values = AtomicLongMap.create();

  /**
   * A map of the {@link Instant} that each value was created, with a list of label values as the
   * keys. The start timestamp (as part of the {@link MetricPoint#interval()} can be used by
   * implementations of {@link MetricWriter} to encode resets of monotonic counters.
   */
  private final ConcurrentHashMap<ImmutableList<String>, Instant> valueStartTimestamps =
// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricsUtils.java
// static final int DEFAULT_CONCURRENCY_LEVEL = 16;
// 
// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricsUtils.java
// static <K, V> ConcurrentHashMap<K, V> newConcurrentHashMap(int concurrencyLevel) {
//   return new ConcurrentHashMap<>(HASHMAP_INITIAL_CAPACITY, HASHMAP_LOAD_FACTOR, concurrencyLevel);
// }
// 
// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricSchema.java
// public enum Kind {
//   CUMULATIVE,
//   GAUGE,
// }


// Path: metrics/src/main/java/com/google/monitoring/metrics/Counter.java
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.monitoring.metrics.MetricsUtils.DEFAULT_CONCURRENCY_LEVEL;
import static com.google.monitoring.metrics.MetricsUtils.newConcurrentHashMap;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;
import com.google.common.util.concurrent.AtomicLongMap;
import com.google.common.util.concurrent.Striped;
import com.google.monitoring.metrics.MetricSchema.Kind;
import java.time.Instant;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import javax.annotation.concurrent.ThreadSafe;

// Copyright 2017 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.monitoring.metrics;



/**
 * A metric which stores Long values. It is stateful and can be changed in increments.
 *
 * <p>This metric is generally suitable for counters, such as requests served or errors generated.
 *
 * <p>The start of the {@link MetricPoint#interval()} of values of instances of this metric will be
 * set to the time that the metric was first set or last {@link #reset()}.
 */
@ThreadSafe
public final class Counter extends AbstractMetric<Long>
    implements SettableMetric<Long>, IncrementableMetric {

  /**
   * A map of the {@link Counter} values, with a list of label values as the keys.
   *
   * <p>This should be modified in a critical section with {@code valueStartTimestamps} so that the
   * values are in sync.
   */
  private final AtomicLongMap<ImmutableList<String>> values = AtomicLongMap.create();

  /**
   * A map of the {@link Instant} that each value was created, with a list of label values as the
   * keys. The start timestamp (as part of the {@link MetricPoint#interval()} can be used by
   * implementations of {@link MetricWriter} to encode resets of monotonic counters.
   */
  private final ConcurrentHashMap<ImmutableList<String>, Instant> valueStartTimestamps =
      newConcurrentHashMap(DEFAULT_CONCURRENCY_LEVEL);