Correct Spliterator characteristics
[yangtools.git] / third-party / triemap / src / main / java / org / opendaylight / yangtools / triemap / AbstractEntrySet.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 java.util.Objects.requireNonNull;
19
20 import java.util.AbstractSet;
21 import java.util.Map.Entry;
22 import java.util.Spliterator;
23 import java.util.Spliterators;
24
25 /**
26  * Abstract base class for implementing {@link TrieMap} entry sets.
27  *
28  * @author Robert Varga
29  *
30  * @param <K> the type of entry keys
31  * @param <V> the type of entry values
32  */
33 abstract class AbstractEntrySet<K, V> extends AbstractSet<Entry<K, V>> {
34     private final TrieMap<K, V> map;
35
36     AbstractEntrySet(final TrieMap<K, V> map) {
37         this.map = requireNonNull(map);
38     }
39
40     final TrieMap<K, V> map() {
41         return map;
42     }
43
44     @Override
45     @SuppressWarnings("checkstyle:parameterName")
46     public final boolean contains(final Object o) {
47         if (!(o instanceof Entry)) {
48             return false;
49         }
50
51         final Entry<?, ?> e = (Entry<?, ?>) o;
52         final Object key = e.getKey();
53         if (key == null) {
54             return false;
55         }
56
57         final V v = map.get(key);
58         return v != null && v.equals(e.getValue());
59     }
60
61     @Override
62     public final int size() {
63         return map.size();
64     }
65
66     @Override
67     public final Spliterator<Entry<K, V>> spliterator() {
68         // TODO: this is backed by an Iterator, we should be able to do better
69         return Spliterators.spliterator(map.immutableIterator(), Long.MAX_VALUE, characteristics());
70     }
71
72     abstract int characteristics();
73 }