BUG-648: Create MapAdaptor and friends
[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 java.util.Collection;
11 import java.util.Collections;
12 import java.util.Map;
13 import java.util.Set;
14
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 import com.google.common.base.Preconditions;
19 import com.romix.scala.collection.concurrent.TrieMap;
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  * 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 TrieMap<K, V> delegate;
35     private int size;
36
37     ReadWriteTrieMap(final TrieMap<K, V> delegate, final int size) {
38         this.delegate = Preconditions.checkNotNull(delegate);
39         this.size = size;
40     }
41
42     Map<K, V> toReadOnly() {
43         final Map<K, V> ret = new ReadOnlyTrieMap<>(delegate, size);
44         LOG.trace("Converted read-write TrieMap {} to read-only {}", this, ret);
45         return ret;
46     }
47
48     @Override
49     public int size() {
50         return size;
51     }
52
53     @Override
54     public boolean isEmpty() {
55         return size == 0;
56     }
57
58     @Override
59     public boolean containsKey(final Object key) {
60         return delegate.containsKey(key);
61     }
62
63     @Override
64     public boolean containsValue(final Object value) {
65         return delegate.containsValue(value);
66     }
67
68     @Override
69     public V get(final Object key) {
70         return delegate.get(key);
71     }
72
73     @Override
74     public V put(final K key, final V value) {
75         final V ret = delegate.put(key, value);
76         if (ret == null) {
77             size++;
78         }
79         return ret;
80     }
81
82     @Override
83     public V remove(final Object key) {
84         final V ret = delegate.remove(key);
85         if (ret != null) {
86             size--;
87         }
88         return ret;
89     }
90
91     @Override
92     public void putAll(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
119     @Override
120     public boolean equals(final Object o) {
121         return delegate.equals(o);
122     }
123
124     @Override
125     public int hashCode() {
126         return delegate.hashCode();
127     }
128 }