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 javax.annotation.Nonnull;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * A TrieMap facade tracking modifications. Since we change structures based on
22 * their size, and determining the size of a TrieMap is expensive, we make sure
23 * to update it as we go.
25 * <p>FIXME: this map does not support modification view the keySet()/values()/entrySet()
29 * @param <V> Value type
31 final class ReadWriteTrieMap<K, V> implements Map<K, V> {
32 private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTrieMap.class);
33 private final TrieMap<K, V> delegate;
37 this.delegate = new TrieMap<>();
41 ReadWriteTrieMap(final TrieMap<K, V> delegate, final int size) {
42 this.delegate = Preconditions.checkNotNull(delegate);
46 Map<K, V> toReadOnly() {
47 final Map<K, V> ret = new ReadOnlyTrieMap<>(delegate, size);
48 LOG.trace("Converted read-write TrieMap {} to read-only {}", this, ret);
58 public boolean isEmpty() {
63 public boolean containsKey(final Object key) {
64 return delegate.containsKey(key);
68 public boolean containsValue(final Object value) {
69 return delegate.containsValue(value);
73 public V get(final Object key) {
74 return delegate.get(key);
78 public V put(final K key, final V value) {
79 final V ret = delegate.put(key, value);
87 public V remove(final Object key) {
88 final V ret = delegate.remove(key);
96 @SuppressWarnings("checkstyle:parameterName")
97 public void putAll(@Nonnull final Map<? extends K, ? extends V> m) {
98 for (Entry<? extends K, ? extends V> e : m.entrySet()) {
99 put(e.getKey(), e.getValue());
104 public void clear() {
110 public Set<K> keySet() {
111 return Collections.unmodifiableSet(delegate.keySet());
115 public Collection<V> values() {
116 return Collections.unmodifiableCollection(delegate.values());
120 public Set<Entry<K, V>> entrySet() {
121 return Collections.unmodifiableSet(delegate.entrySet());
125 public boolean equals(final Object obj) {
126 return delegate.equals(obj);
130 public int hashCode() {
131 return delegate.hashCode();