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 java.util.Collection;
12 import java.util.Collections;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.triemap.MutableTrieMap;
17 import org.opendaylight.yangtools.triemap.TrieMap;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
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 * <p>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 MutableTrieMap<K, V> delegate;
38 this.delegate = TrieMap.create();
42 ReadWriteTrieMap(final MutableTrieMap<K, V> delegate, final int size) {
43 this.delegate = Preconditions.checkNotNull(delegate);
47 Map<K, V> toReadOnly() {
48 final Map<K, V> ret = new ReadOnlyTrieMap<>(delegate, size);
49 LOG.trace("Converted read-write TrieMap {} to read-only {}", this, ret);
59 public boolean isEmpty() {
64 public boolean containsKey(final Object key) {
65 return delegate.containsKey(key);
69 public boolean containsValue(final Object value) {
70 return delegate.containsValue(value);
74 public V get(final Object key) {
75 return delegate.get(key);
79 public V put(final K key, final V value) {
80 final V ret = delegate.put(key, value);
88 public V remove(final Object key) {
89 final V ret = delegate.remove(key);
97 @SuppressWarnings("checkstyle:parameterName")
98 public void putAll(@Nonnull final Map<? extends K, ? extends V> m) {
99 for (Entry<? extends K, ? extends V> e : m.entrySet()) {
100 put(e.getKey(), e.getValue());
105 public void clear() {
111 public Set<K> keySet() {
112 return Collections.unmodifiableSet(delegate.keySet());
116 public Collection<V> values() {
117 return Collections.unmodifiableCollection(delegate.values());
121 public Set<Entry<K, V>> entrySet() {
122 return Collections.unmodifiableSet(delegate.entrySet());
126 public boolean equals(final Object obj) {
127 return delegate.equals(obj);
131 public int hashCode() {
132 return delegate.hashCode();
136 public String toString() {
137 return delegate.toString();