07f8310063b7a22f58ad92d04ac097252b00ad3c
[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 import java.util.Spliterator;
23
24 /**
25  * Support for EntrySet operations required by the Map interface.
26  *
27  * @param <K> the type of keys
28  * @param <V> the type of values
29  */
30 final class MutableEntrySet<K, V> extends AbstractEntrySet<K, V> {
31     MutableEntrySet(final TrieMap<K, V> map) {
32         super(map);
33     }
34
35     @Override
36     @SuppressWarnings("checkstyle:parameterName")
37     public boolean add(final Entry<K, V> e) {
38         final K k = e.getKey();
39         checkArgument(k != null);
40         final V v = e.getValue();
41         checkArgument(v != null);
42
43         final V prev = map().putIfAbsent(k, v);
44         return prev == null || !v.equals(prev);
45     }
46
47     @Override
48     public void clear() {
49         map().clear();
50     }
51
52     @Override
53     public Iterator<Entry<K, V>> iterator() {
54         return map().iterator();
55     }
56
57     @Override
58     @SuppressWarnings("checkstyle:parameterName")
59     public boolean remove(final Object o) {
60         if (!(o instanceof Entry)) {
61             return false;
62         }
63
64         final Entry<?, ?> e = (Entry<?, ?>) o;
65         final Object key = e.getKey();
66         if (key == null) {
67             return false;
68         }
69         final Object value = e.getValue();
70         if (value == null) {
71             return false;
72         }
73
74         return map().remove(key, value);
75     }
76
77     @Override
78     int characteristics() {
79         return Spliterator.DISTINCT | Spliterator.CONCURRENT | Spliterator.NONNULL;
80     }
81 }