BUG-7464: Split TrieMap into read-only and read-write
[yangtools.git] / third-party / triemap / src / main / java / org / opendaylight / yangtools / triemap / ImmutableTrieMap.java
diff --git a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/ImmutableTrieMap.java b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/ImmutableTrieMap.java
new file mode 100644 (file)
index 0000000..4ea607e
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.opendaylight.yangtools.triemap;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import com.google.common.annotations.Beta;
+import java.util.Map;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+
+/**
+ * An immutable TrieMap
+ *
+ * @author Robert Varga
+ *
+ * @param <K> the type of keys maintained by this map
+ * @param <V> the type of mapped values
+ */
+@Beta
+public final class ImmutableTrieMap<K, V> extends TrieMap<K, V> {
+    private final INode<K, V> root;
+
+    ImmutableTrieMap(final INode<K, V> root, final Equivalence<? super K> equiv) {
+        super(equiv);
+        this.root = checkNotNull(root);
+    }
+
+    @Override
+    public void clear() {
+        throw unsupported();
+    }
+
+    @Override
+    public V compute(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
+        throw unsupported();
+    }
+
+    @Override
+    public V computeIfAbsent(final K key, final Function<? super K, ? extends V> mappingFunction) {
+        throw unsupported();
+    }
+
+    @Override
+    public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
+        throw unsupported();
+    }
+
+    @Override
+    public V merge(final K key, final V value, final BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
+        throw unsupported();
+    }
+
+    @Override
+    public V put(final K key, final V value) {
+        throw unsupported();
+    }
+
+    @Override
+    public void putAll(final Map<? extends K, ? extends V> m) {
+        throw unsupported();
+    }
+
+    @Override
+    public V putIfAbsent(final K key, final V value) {
+        throw unsupported();
+    }
+
+    @Override
+    public V remove(final Object key) {
+        throw unsupported();
+    }
+
+    @Override
+    public boolean remove(final Object key, final Object value) {
+        throw unsupported();
+    }
+
+    @Override
+    public boolean replace(final K key, final V oldValue, final V newValue) {
+        throw unsupported();
+    }
+
+    @Override
+    public V replace(final K key, final V value) {
+        throw unsupported();
+    }
+
+    @Override
+    public int size() {
+        return root.cachedSize(this);
+    }
+
+    @Override
+    public final TrieMap<K, V> mutableSnapshot() {
+        return new MutableTrieMap<>(equiv(), new INode<>(new Gen(), root.gcasRead(this)));
+    }
+
+    @Override
+    public ImmutableTrieMap<K, V> immutableSnapshot() {
+        return this;
+    }
+
+    @Override
+    boolean isReadOnly() {
+        return true;
+    }
+
+    @Override
+    INode<K, V> RDCSS_READ_ROOT(final boolean abort) {
+        return root;
+    }
+
+    private static UnsupportedOperationException unsupported() {
+        return new UnsupportedOperationException("Attempted to modify a read-only view");
+    }
+}