Cleanup use of Guava library
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / ReadWriteTrieMap.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ForwardingMap;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.Map;
16 import java.util.Set;
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;
22
23 /**
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.
27  *
28  * <p>
29  * FIXME: this map does not support modification view the keySet()/values()/entrySet() methods.
30  *
31  * @param <K> Key type
32  * @param <V> Value type
33  */
34 final class ReadWriteTrieMap<K, V> extends ForwardingMap<K, V> {
35     private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTrieMap.class);
36
37     private final MutableTrieMap<K, V> delegate;
38
39     private int size;
40
41     ReadWriteTrieMap() {
42         this.delegate = TrieMap.create();
43         this.size = 0;
44     }
45
46     ReadWriteTrieMap(final MutableTrieMap<K, V> delegate, final int size) {
47         this.delegate = requireNonNull(delegate);
48         this.size = size;
49     }
50
51     @Override
52     protected Map<K, V> delegate() {
53         return delegate;
54     }
55
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);
59         return ret;
60     }
61
62     @Override
63     public int size() {
64         return size;
65     }
66
67     @Override
68     public boolean isEmpty() {
69         return size == 0;
70     }
71
72     @Override
73     public V put(final K key, final V value) {
74         final V ret = delegate.put(key, value);
75         if (ret == null) {
76             size++;
77         }
78         return ret;
79     }
80
81     @Override
82     public V remove(final Object key) {
83         final V ret = delegate.remove(key);
84         if (ret != null) {
85             size--;
86         }
87         return ret;
88     }
89
90     @Override
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());
95         }
96     }
97
98     @Override
99     public void clear() {
100         delegate.clear();
101         size = 0;
102     }
103
104     @Override
105     public Set<K> keySet() {
106         return Collections.unmodifiableSet(delegate.keySet());
107     }
108
109     @Override
110     public Collection<V> values() {
111         return Collections.unmodifiableCollection(delegate.values());
112     }
113
114     @Override
115     public Set<Entry<K, V>> entrySet() {
116         return Collections.unmodifiableSet(delegate.entrySet());
117     }
118 }