2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util;
10 import com.google.common.base.Preconditions;
11 import com.romix.scala.collection.concurrent.TrieMap;
12 import java.util.Collection;
13 import java.util.Collections;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * A TrieMap facade tracking modifications. Since we change structures based on
21 * their size, and determining the size of a TrieMap is expensive, we make sure
22 * to update it as we go.
24 * FIXME: this map does not support modification view the keySet()/values()/entrySet()
28 * @param <V> Value type
30 final class ReadWriteTrieMap<K, V> implements Map<K, V> {
31 private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTrieMap.class);
32 private final TrieMap<K, V> delegate;
36 this.delegate = new TrieMap<K, V>();
40 ReadWriteTrieMap(final TrieMap<K, V> delegate, final int size) {
41 this.delegate = Preconditions.checkNotNull(delegate);
45 Map<K, V> toReadOnly() {
46 final Map<K, V> ret = new ReadOnlyTrieMap<>(delegate, size);
47 LOG.trace("Converted read-write TrieMap {} to read-only {}", this, ret);
57 public boolean isEmpty() {
62 public boolean containsKey(final Object key) {
63 return delegate.containsKey(key);
67 public boolean containsValue(final Object value) {
68 return delegate.containsValue(value);
72 public V get(final Object key) {
73 return delegate.get(key);
77 public V put(final K key, final V value) {
78 final V ret = delegate.put(key, value);
86 public V remove(final Object key) {
87 final V ret = delegate.remove(key);
95 public void putAll(final Map<? extends K, ? extends V> m) {
96 for (Entry<? extends K, ? extends V> e : m.entrySet()) {
97 put(e.getKey(), e.getValue());
102 public void clear() {
108 public Set<K> keySet() {
109 return Collections.unmodifiableSet(delegate.keySet());
113 public Collection<V> values() {
114 return Collections.unmodifiableCollection(delegate.values());
118 public Set<Entry<K, V>> entrySet() {
119 return Collections.unmodifiableSet(delegate.entrySet());
123 public boolean equals(final Object o) {
124 return delegate.equals(o);
128 public int hashCode() {
129 return delegate.hashCode();