Bug-731 Fixed few minor Sonar warnings
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / AdjRIBOutEntry.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
12 import org.opendaylight.protocol.bgp.rib.spi.RouteEncoder;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
15 import org.opendaylight.yangtools.yang.binding.DataContainer;
16
17 final class AdjRIBOutEntry<K, V extends Route> {
18     @SuppressWarnings("rawtypes")
19     private static final AtomicReferenceFieldUpdater<AdjRIBOutEntry, Route> NLRIENTRY_ADV_UPDATER = AtomicReferenceFieldUpdater.newUpdater(AdjRIBOutEntry.class, Route.class, "advertisedValue");
20     @SuppressWarnings("rawtypes")
21     private static final AtomicReferenceFieldUpdater<AdjRIBOutEntry, Route> NLRIENTRY_CUR_UPDATER = AtomicReferenceFieldUpdater.newUpdater(AdjRIBOutEntry.class, Route.class, "currentValue");
22
23     /*
24      * Marker object for uninitialized value. Distinct from null.
25      */
26     private static final Route NLRIENTRY_NONE_VALUE = new Route() {
27         @Override
28         public Class<? extends DataContainer> getImplementedInterface() {
29             throw new IllegalStateException("This method should never be invoked");
30         }
31
32         @Override
33         public Attributes getAttributes() {
34             throw new IllegalStateException("This method should never be invoked");
35         }
36     };
37
38     // Referenced via AtomicReferenceFieldUpdaters
39     @SuppressWarnings("unused")
40     private volatile Route currentValue = NLRIENTRY_NONE_VALUE;
41     @SuppressWarnings("unused")
42     private volatile Route advertisedValue = NLRIENTRY_NONE_VALUE;
43     private final RouteEncoder ribOut;
44
45     AdjRIBOutEntry(final RouteEncoder ribOut) {
46         this.ribOut = Preconditions.checkNotNull(ribOut);
47     }
48
49     Route getAdverised() {
50         return NLRIENTRY_ADV_UPDATER.get(this);
51     }
52
53     void setAdverised(final V value) {
54         NLRIENTRY_ADV_UPDATER.set(this, value);
55     }
56
57     @SuppressWarnings("unchecked")
58     V getCurrent() {
59         final Route o = NLRIENTRY_CUR_UPDATER.get(this);
60         Preconditions.checkState(!isNone(o), "Value cannot be NONE here");
61         return (V) o;
62     }
63
64     Route getAndSetCurrent(final V value) {
65         return NLRIENTRY_CUR_UPDATER.getAndSet(this, value);
66     }
67
68     RouteEncoder getRibOut() {
69         return ribOut;
70     }
71
72     static boolean isNone(final Object o) {
73         return o == NLRIENTRY_NONE_VALUE;
74     }
75 }