BUG-7464: enable FindBugs enforcement
[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     public boolean add(final Entry<K, V> e) {
36         final K k = e.getKey();
37         checkArgument(k != null);
38         final V v = e.getValue();
39         checkArgument(v != null);
40
41         final V prev = map().putIfAbsent(k, v);
42         return prev == null || !v.equals(prev);
43     }
44
45     @Override
46     public void clear() {
47         map().clear();
48     }
49
50     @Override
51     public Iterator<Entry<K, V>> iterator() {
52         return map().iterator();
53     }
54
55     @Override
56     public boolean remove(final Object o) {
57         if (!(o instanceof Entry)) {
58             return false;
59         }
60
61         final Entry<?, ?> e = (Entry<?, ?>) o;
62         final Object key = e.getKey();
63         if (key == null) {
64             return false;
65         }
66         final Object value = e.getValue();
67         if (value == null) {
68             return false;
69         }
70
71         return map().remove(key, value);
72     }
73 }