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 org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import tech.pantheon.triemap.MutableTrieMap;
20 import tech.pantheon.triemap.TrieMap;
23 * A TrieMap facade tracking modifications. Since we change structures based on
24 * their size, and determining the size of a TrieMap is expensive, we make sure
25 * to update it as we go.
28 * FIXME: this map does not support modification view the keySet()/values()/entrySet() methods.
31 * @param <V> Value type
33 final class ReadWriteTrieMap<K, V> extends ForwardingMap<K, V> {
34 private static final Logger LOG = LoggerFactory.getLogger(ReadWriteTrieMap.class);
36 private final MutableTrieMap<K, V> delegate;
41 this.delegate = TrieMap.create();
45 ReadWriteTrieMap(final MutableTrieMap<K, V> delegate, final int size) {
46 this.delegate = requireNonNull(delegate);
51 protected Map<K, V> delegate() {
55 Map<K, V> toReadOnly() {
56 final Map<K, V> ret = new ReadOnlyTrieMap<>(delegate, size);
57 LOG.trace("Converted read-write TrieMap {} to read-only {}", this, ret);
67 public boolean isEmpty() {
72 public V put(final K key, final V value) {
73 final V ret = delegate.put(key, value);
81 public V remove(final Object key) {
82 final V ret = delegate.remove(key);
90 @SuppressWarnings("checkstyle:parameterName")
91 public void putAll(final Map<? extends K, ? extends V> m) {
92 for (Entry<? extends K, ? extends V> e : m.entrySet()) {
93 put(e.getKey(), e.getValue());
104 public Set<K> keySet() {
105 return Collections.unmodifiableSet(delegate.keySet());
109 public Collection<V> values() {
110 return Collections.unmodifiableCollection(delegate.values());
114 public Set<Entry<K, V>> entrySet() {
115 return Collections.unmodifiableSet(delegate.entrySet());