Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / stats / peer / route / PerTableTypeRouteCounter.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.rib.impl.stats.peer.route;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import java.util.Map;
14 import java.util.Set;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.atomic.LongAdder;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public final class PerTableTypeRouteCounter {
23     private static final Logger LOG = LoggerFactory.getLogger(PerTableTypeRouteCounter.class);
24
25     private final Map<TablesKey, LongAdder> counters = new ConcurrentHashMap<>();
26
27     public PerTableTypeRouteCounter(@Nonnull final Set<TablesKey> tablesKeySet) {
28         init(tablesKeySet);
29     }
30
31     public PerTableTypeRouteCounter() {
32     }
33
34     private synchronized void init(@Nonnull final Set<TablesKey> tablesKeySet) {
35         tablesKeySet.forEach(this::init);
36     }
37
38     public final synchronized LongAdder init(@Nonnull final TablesKey tablesKey) {
39         LongAdder counter = this.counters.get(requireNonNull(tablesKey));
40         if (counter == null) {
41             counter = new LongAdder();
42             this.counters.put(tablesKey, counter);
43         }
44         LOG.debug("Initializing route counter for tablesKey {}", tablesKey);
45         counter.reset();
46         return counter;
47     }
48
49     /**
50      * Get the counter for given tablesKey. Return an empty counter if it doesn't exist
51      * NOTE: the created empty counter won't be put into the original map
52      *
53      * @param tablesKey
54      */
55     @Nonnull public final LongAdder getCounterOrDefault(@Nonnull final TablesKey tablesKey) {
56         return this.counters.getOrDefault(requireNonNull(tablesKey), new LongAdder());
57     }
58
59     /**
60      * Get the counter with given tablesKey.  Create an empty counter if it doesn't exist
61      * This method will put the created empty counter back to map
62      *
63      * @param tablesKey
64      */
65     public final LongAdder getCounterOrSetDefault(@Nonnull final TablesKey tablesKey) {
66         if (!this.counters.containsKey(tablesKey)) {
67             return init(tablesKey);
68         }
69
70         return this.counters.get(requireNonNull(tablesKey));
71     }
72
73     public final Map<TablesKey, LongAdder> getCounters() {
74         return this.counters;
75     }
76
77     public final void resetAll() {
78         LOG.debug("Resetting all route counters..");
79         this.counters.values().forEach(LongAdder::reset);
80     }
81
82     public void setValueToCounterOrSetDefault(final TablesKey tablesKey, final int size) {
83         final LongAdder counter = getCounterOrSetDefault(tablesKey);
84         counter.reset();
85         counter.add(size);
86     }
87 }