Remove @(Not)ThreadSafe annotation
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / DOMDataTreePrefixTable.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 java.util.EnumMap;
12 import java.util.Map;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Prefix table indexed by {@link DOMDataTreeIdentifier}. Stores values in tree and provides lookup of closest ancestor.
22  * This class is not thread-safe.
23  *
24  * @param <V> Value type
25  */
26 @Beta
27 public final class DOMDataTreePrefixTable<V> {
28
29     private static final Logger LOG = LoggerFactory.getLogger(DOMDataTreePrefixTable.class);
30     private final Map<LogicalDatastoreType, DOMDataTreePrefixTableEntry<V>> roots =
31         new EnumMap<>(LogicalDatastoreType.class);
32
33     private DOMDataTreePrefixTable() {
34
35     }
36
37     public static <V> DOMDataTreePrefixTable<V> create() {
38         return new DOMDataTreePrefixTable<>();
39     }
40
41     /**
42      * Lookups entry by provided {@link DOMDataTreeIdentifier}, if entry is not present returns
43      * closest non-null entry towards root or null if no entry towards root exists.
44      *
45      * @param prefix Prefix for lookup
46      * @return closest non-null entry towards root or null if no entry towards root exists.
47      */
48     public @Nullable DOMDataTreePrefixTableEntry<V> lookup(final @NonNull DOMDataTreeIdentifier prefix) {
49         final DOMDataTreePrefixTableEntry<V> t = roots.get(prefix.getDatastoreType());
50         return t == null ? null : t.lookup(prefix.getRootIdentifier());
51     }
52
53     /**
54      * Stores value associated to the prefix.
55      *
56      * @param prefix DOM prefix of value
57      * @param value Value to be stored
58      * @throws IllegalStateException If value is already stored for provided prefix
59      */
60     public void store(final @NonNull DOMDataTreeIdentifier prefix, final @NonNull V value) {
61         DOMDataTreePrefixTableEntry<V> domDataTreePrefixTableEntry = roots.get(prefix.getDatastoreType());
62         if (domDataTreePrefixTableEntry == null) {
63             domDataTreePrefixTableEntry = new DOMDataTreePrefixTableEntry<>();
64             roots.put(prefix.getDatastoreType(), domDataTreePrefixTableEntry);
65         }
66
67         domDataTreePrefixTableEntry.store(prefix.getRootIdentifier(), value);
68     }
69
70     /**
71      * Removes value associated to the prefix.
72      * Value is removed only and only if full prefix match for stored value. Removal of prefix does
73      * not remove child prefixes.
74      *
75      * @param prefix to be removed
76      */
77     public void remove(final @NonNull DOMDataTreeIdentifier prefix) {
78         final DOMDataTreePrefixTableEntry<V> t = roots.get(prefix.getDatastoreType());
79         if (t == null) {
80             LOG.warn("Shard registration {} points to non-existent table", prefix);
81             return;
82         }
83
84         t.remove(prefix.getRootIdentifier());
85     }
86 }