Migrate to tech.pantheon.TrieMap
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / ReadWriteTrieMap.java
index 039e7340a5a7a2260c999b1626d92a2dae1b4037..702fd00730bbed13b91ab314c45d038cb5eaa4a9 100644 (file)
@@ -7,38 +7,52 @@
  */
 package org.opendaylight.yangtools.util;
 
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.collect.ForwardingMap;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
-
+import javax.annotation.Nonnull;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
-import com.google.common.base.Preconditions;
-import com.romix.scala.collection.concurrent.TrieMap;
+import tech.pantheon.triemap.MutableTrieMap;
+import tech.pantheon.triemap.TrieMap;
 
 /**
  * A TrieMap facade tracking modifications. Since we change structures based on
  * their size, and determining the size of a TrieMap is expensive, we make sure
  * to update it as we go.
  *
- * FIXME: this map does not support modification view the keySet()/values()/entrySet()
- *        methods.
+ * <p>
+ * FIXME: this map does not support modification view the keySet()/values()/entrySet() methods.
  *
  * @param <K> Key type
  * @param <V> Value type
  */
-final class ReadWriteTrieMap<K, V> implements Map<K, V> {
-    private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTrieMap.class);
-    private final TrieMap<K, V> delegate;
+final class ReadWriteTrieMap<K, V> extends ForwardingMap<K, V> {
+    private static final Logger LOG = LoggerFactory.getLogger(ReadWriteTrieMap.class);
+
+    private final MutableTrieMap<K, V> delegate;
+
     private int size;
 
-    ReadWriteTrieMap(final TrieMap<K, V> delegate, final int size) {
-        this.delegate = Preconditions.checkNotNull(delegate);
+    ReadWriteTrieMap() {
+        this.delegate = TrieMap.create();
+        this.size = 0;
+    }
+
+    ReadWriteTrieMap(final MutableTrieMap<K, V> delegate, final int size) {
+        this.delegate = requireNonNull(delegate);
         this.size = size;
     }
 
+    @Override
+    protected Map<K, V> delegate() {
+        return delegate;
+    }
+
     Map<K, V> toReadOnly() {
         final Map<K, V> ret = new ReadOnlyTrieMap<>(delegate, size);
         LOG.trace("Converted read-write TrieMap {} to read-only {}", this, ret);
@@ -55,21 +69,6 @@ final class ReadWriteTrieMap<K, V> implements Map<K, V> {
         return size == 0;
     }
 
-    @Override
-    public boolean containsKey(final Object key) {
-        return delegate.containsKey(key);
-    }
-
-    @Override
-    public boolean containsValue(final Object value) {
-        return delegate.containsValue(value);
-    }
-
-    @Override
-    public V get(final Object key) {
-        return delegate.get(key);
-    }
-
     @Override
     public V put(final K key, final V value) {
         final V ret = delegate.put(key, value);
@@ -89,7 +88,8 @@ final class ReadWriteTrieMap<K, V> implements Map<K, V> {
     }
 
     @Override
-    public void putAll(final Map<? extends K, ? extends V> m) {
+    @SuppressWarnings("checkstyle:parameterName")
+    public void putAll(@Nonnull final Map<? extends K, ? extends V> m) {
         for (Entry<? extends K, ? extends V> e : m.entrySet()) {
             put(e.getKey(), e.getValue());
         }
@@ -115,14 +115,4 @@ final class ReadWriteTrieMap<K, V> implements Map<K, V> {
     public Set<Entry<K, V>> entrySet() {
         return Collections.unmodifiableSet(delegate.entrySet());
     }
-
-    @Override
-    public boolean equals(final Object o) {
-        return delegate.equals(o);
-    }
-
-    @Override
-    public int hashCode() {
-        return delegate.hashCode();
-    }
 }