BUG-7464: Switch to use forked TrieMap
[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 com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.Map;
14 import java.util.Set;
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;
20
21 /**
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.
25  *
26  * <p>FIXME: this map does not support modification view the keySet()/values()/entrySet()
27  *        methods.
28  *
29  * @param <K> Key type
30  * @param <V> Value type
31  */
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;
35     private int size;
36
37     ReadWriteTrieMap() {
38         this.delegate = TrieMap.create();
39         this.size = 0;
40     }
41
42     ReadWriteTrieMap(final MutableTrieMap<K, V> delegate, final int size) {
43         this.delegate = Preconditions.checkNotNull(delegate);
44         this.size = size;
45     }
46
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);
50         return ret;
51     }
52
53     @Override
54     public int size() {
55         return size;
56     }
57
58     @Override
59     public boolean isEmpty() {
60         return size == 0;
61     }
62
63     @Override
64     public boolean containsKey(final Object key) {
65         return delegate.containsKey(key);
66     }
67
68     @Override
69     public boolean containsValue(final Object value) {
70         return delegate.containsValue(value);
71     }
72
73     @Override
74     public V get(final Object key) {
75         return delegate.get(key);
76     }
77
78     @Override
79     public V put(final K key, final V value) {
80         final V ret = delegate.put(key, value);
81         if (ret == null) {
82             size++;
83         }
84         return ret;
85     }
86
87     @Override
88     public V remove(final Object key) {
89         final V ret = delegate.remove(key);
90         if (ret != null) {
91             size--;
92         }
93         return ret;
94     }
95
96     @Override
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());
100         }
101     }
102
103     @Override
104     public void clear() {
105         delegate.clear();
106         size = 0;
107     }
108
109     @Override
110     public Set<K> keySet() {
111         return Collections.unmodifiableSet(delegate.keySet());
112     }
113
114     @Override
115     public Collection<V> values() {
116         return Collections.unmodifiableCollection(delegate.values());
117     }
118
119     @Override
120     public Set<Entry<K, V>> entrySet() {
121         return Collections.unmodifiableSet(delegate.entrySet());
122     }
123
124     @Override
125     public boolean equals(final Object o) {
126         return delegate.equals(o);
127     }
128
129     @Override
130     public int hashCode() {
131         return delegate.hashCode();
132     }
133
134     @Override
135     public String toString() {
136         return delegate.toString();
137     }
138 }