Fix a few warnings
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / DOMDataTreePrefixTableEntry.java
1 /*
2  * Copyright (c) 2015 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.mdsal.dom.spi;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.Map;
15 import javax.annotation.concurrent.NotThreadSafe;
16 import org.opendaylight.yangtools.concepts.Identifiable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 @Beta
23 @NotThreadSafe
24 public final class DOMDataTreePrefixTableEntry<V> implements Identifiable<PathArgument> {
25     private static final Logger LOG = LoggerFactory.getLogger(DOMDataTreePrefixTableEntry.class);
26     // FIXME: We do probably want to adapt map
27     private final Map<PathArgument, DOMDataTreePrefixTableEntry<V>> children = new HashMap<>();
28     private final PathArgument identifier;
29     private V value = null;
30
31     DOMDataTreePrefixTableEntry() {
32         identifier = null;
33     }
34
35     DOMDataTreePrefixTableEntry(final PathArgument identifier) {
36         this.identifier = Preconditions.checkNotNull(identifier);
37     }
38
39     @Override
40     public PathArgument getIdentifier() {
41         return identifier;
42     }
43
44     public V getValue() {
45         return value;
46     }
47
48     DOMDataTreePrefixTableEntry<V> lookup(final YangInstanceIdentifier id) {
49         final Iterator<PathArgument> it = id.getPathArguments().iterator();
50         DOMDataTreePrefixTableEntry<V> entry = this;
51         DOMDataTreePrefixTableEntry<V> lastPresentEntry = entry;
52
53         while (it.hasNext()) {
54             final PathArgument a = it.next();
55             final DOMDataTreePrefixTableEntry<V> child = entry.children.get(a);
56             if (child == null) {
57                 LOG.debug("Lookup of {} stopped at {}", id, a);
58                 break;
59             }
60
61             entry = child;
62
63             if (child.getValue() != null) {
64                 lastPresentEntry = child;
65             }
66         }
67
68         return lastPresentEntry;
69     }
70
71     void store(final YangInstanceIdentifier id, final V reg) {
72         final Iterator<PathArgument> it = id.getPathArguments().iterator();
73         DOMDataTreePrefixTableEntry<V> entry = this;
74
75         while (it.hasNext()) {
76             final PathArgument a = it.next();
77             DOMDataTreePrefixTableEntry<V> child = entry.children.get(a);
78             if (child == null) {
79                 child = new DOMDataTreePrefixTableEntry<>(a);
80                 entry.children.put(a, child);
81             }
82             // TODO: Is this correct? We want to enter child
83             entry = child;
84         }
85
86         Preconditions.checkState(entry.value == null);
87         entry.value = reg;
88     }
89
90     private boolean remove(final Iterator<PathArgument> it) {
91         if (it.hasNext()) {
92             final PathArgument arg = it.next();
93             final DOMDataTreePrefixTableEntry<V> child = children.get(arg);
94             if (child != null) {
95                 if (child.remove(it)) {
96                     children.remove(arg);
97                 }
98             } else {
99                 LOG.warn("Cannot remove non-existent child {}", arg);
100             }
101         } else {
102             /*
103              * Iterator is empty, this effectively means is table entry to remove registration.
104              * FIXME: We probably want to compare value to make sure we are removing correct value.
105              */
106             value = null;
107         }
108         return value == null && children.isEmpty();
109     }
110
111     void remove(final YangInstanceIdentifier id) {
112         this.remove(id.getPathArguments().iterator());
113     }
114 }