04ddb8818931e9a0b694696e7fca30329ea5b6d0
[yangtools.git] / third-party / triemap / src / main / java / org / opendaylight / yangtools / triemap / MutableEntrySet.java
1 /*
2  * (C) Copyright 2017 Pantheon Technologies, s.r.o. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.opendaylight.yangtools.triemap;
17
18 import static com.google.common.base.Preconditions.checkArgument;
19
20 import java.util.Iterator;
21 import java.util.Map.Entry;
22
23 /**
24  * Support for EntrySet operations required by the Map interface.
25  *
26  * @param <K> the type of keys
27  * @param <V> the type of values
28  */
29 final class MutableEntrySet<K, V> extends AbstractEntrySet<K, V> {
30     MutableEntrySet(final TrieMap<K, V> map) {
31         super(map);
32     }
33
34     @Override
35     @SuppressWarnings("checkstyle:parameterName")
36     public boolean add(final Entry<K, V> e) {
37         final K k = e.getKey();
38         checkArgument(k != null);
39         final V v = e.getValue();
40         checkArgument(v != null);
41
42         final V prev = map().putIfAbsent(k, v);
43         return prev == null || !v.equals(prev);
44     }
45
46     @Override
47     public void clear() {
48         map().clear();
49     }
50
51     @Override
52     public Iterator<Entry<K, V>> iterator() {
53         return map().iterator();
54     }
55
56     @Override
57     @SuppressWarnings("checkstyle:parameterName")
58     public boolean remove(final Object o) {
59         if (!(o instanceof Entry)) {
60             return false;
61         }
62
63         final Entry<?, ?> e = (Entry<?, ?>) o;
64         final Object key = e.getKey();
65         if (key == null) {
66             return false;
67         }
68         final Object value = e.getValue();
69         if (value == null) {
70             return false;
71         }
72
73         return map().remove(key, value);
74     }
75 }