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 static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ForwardingMap;
13 import java.util.Collection;
14 import java.util.Collections;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.triemap.MutableTrieMap;
19 import org.opendaylight.yangtools.triemap.TrieMap;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * A TrieMap facade tracking modifications. Since we change structures based on
25 * their size, and determining the size of a TrieMap is expensive, we make sure
26 * to update it as we go.
29 * FIXME: this map does not support modification view the keySet()/values()/entrySet() methods.
32 * @param <V> Value type
34 final class ReadWriteTrieMap<K, V> extends ForwardingMap<K, V> {
35 private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTrieMap.class);
37 private final MutableTrieMap<K, V> delegate;
42 this.delegate = TrieMap.create();
46 ReadWriteTrieMap(final MutableTrieMap<K, V> delegate, final int size) {
47 this.delegate = requireNonNull(delegate);
52 protected Map<K, V> delegate() {
56 Map<K, V> toReadOnly() {
57 final Map<K, V> ret = new ReadOnlyTrieMap<>(delegate, size);
58 LOG.trace("Converted read-write TrieMap {} to read-only {}", this, ret);
68 public boolean isEmpty() {
73 public V put(final K key, final V value) {
74 final V ret = delegate.put(key, value);
82 public V remove(final Object key) {
83 final V ret = delegate.remove(key);
91 @SuppressWarnings("checkstyle:parameterName")
92 public void putAll(@Nonnull 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());