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     @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());
101         }
102     }
103
104     @Override
105     public void clear() {
106         delegate.clear();
107         size = 0;
108     }
109
110     @Override
111     public Set<K> keySet() {
112         return Collections.unmodifiableSet(delegate.keySet());
113     }
114
115     @Override
116     public Collection<V> values() {
117         return Collections.unmodifiableCollection(delegate.values());
118     }
119
120     @Override
121     public Set<Entry<K, V>> entrySet() {
122         return Collections.unmodifiableSet(delegate.entrySet());
123     }
124
125     @Override
126     public boolean equals(final Object obj) {
127         return delegate.equals(obj);
128     }
129
130     @Override
131     public int hashCode() {
132         return delegate.hashCode();
133     }
134
135     @Override
136     public String toString() {
137         return delegate.toString();
138     }
139 }