Replace UnsignedInt32Counter by LongAdder
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / CountersUtil.java
1 /*
2  * Copyright (c) 2016 Cisco 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
9 package org.opendaylight.protocol.bgp.rib.impl;
10
11 import java.util.concurrent.atomic.LongAdder;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.ZeroBasedCounter32;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Provides util for increment/decrement counters whenever is not null
21  * Otherwise prints an informative warn.
22  */
23 public final class CountersUtil {
24     private static final Logger LOG = LoggerFactory.getLogger(CountersUtil.class);
25
26     private CountersUtil() {
27         throw new UnsupportedOperationException();
28     }
29
30     /**
31      * Increments counter by 1 if supported, otherwise produce a warn
32      *
33      * @param counter counter
34      * @param tablesKey tablesKey Type
35      */
36     static void increment(@Nullable final LongAdder counter, @Nonnull TablesKey tablesKey) {
37         if (counter != null) {
38             counter.increment();
39             return;
40         }
41         LOG.warn("Family {} not supported", tablesKey);
42     }
43
44     /**
45      * Increments counter by 1 if supported, otherwise produce a warn
46      *  @param counter counter
47      * @param tablesKey tablesKey Type
48      */
49     static void decrement(@Nullable final LongAdder counter, @Nonnull TablesKey tablesKey) {
50         if (counter != null) {
51             counter.decrement();
52             return;
53         }
54         LOG.warn("Family {} not supported", tablesKey);
55     }
56
57     public static ZeroBasedCounter32 toZeroBasedCounter32(final LongAdder longAdder){
58         return new ZeroBasedCounter32(longAdder.longValue());
59     }
60 }