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 java.util.Collection;
11 import java.util.Collections;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 import com.google.common.base.Preconditions;
19 import com.romix.scala.collection.concurrent.TrieMap;
22 * A TrieMap facade tracking modifications. Since we change structures based on
23 * their size, and determining the size of a TrieMap is expensive, we make sure
24 * to update it as we go.
26 * FIXME: this map does not support modification view the keySet()/values()/entrySet()
30 * @param <V> Value type
32 final class ReadWriteTrieMap<K, V> implements Map<K, V> {
33 private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTrieMap.class);
34 private final TrieMap<K, V> delegate;
37 ReadWriteTrieMap(final TrieMap<K, V> delegate, final int size) {
38 this.delegate = Preconditions.checkNotNull(delegate);
42 Map<K, V> toReadOnly() {
43 final Map<K, V> ret = new ReadOnlyTrieMap<>(delegate, size);
44 LOG.trace("Converted read-write TrieMap {} to read-only {}", this, ret);
54 public boolean isEmpty() {
59 public boolean containsKey(final Object key) {
60 return delegate.containsKey(key);
64 public boolean containsValue(final Object value) {
65 return delegate.containsValue(value);
69 public V get(final Object key) {
70 return delegate.get(key);
74 public V put(final K key, final V value) {
75 final V ret = delegate.put(key, value);
83 public V remove(final Object key) {
84 final V ret = delegate.remove(key);
92 public void putAll(final Map<? extends K, ? extends V> m) {
93 for (Entry<? extends K, ? extends V> e : m.entrySet()) {
94 put(e.getKey(), e.getValue());
105 public Set<K> keySet() {
106 return Collections.unmodifiableSet(delegate.keySet());
110 public Collection<V> values() {
111 return Collections.unmodifiableCollection(delegate.values());
115 public Set<Entry<K, V>> entrySet() {
116 return Collections.unmodifiableSet(delegate.entrySet());
120 public boolean equals(final Object o) {
121 return delegate.equals(o);
125 public int hashCode() {
126 return delegate.hashCode();