BUG-7464: Add dedicated key set classes
[yangtools.git] / third-party / triemap / src / main / java / org / opendaylight / yangtools / triemap / MutableKeySet.java
1 /*
2  * (C) Copyright 2016 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 java.util.Iterator;
19
20 /**
21  * A mutable view of a TrieMap's key set.
22  *
23  * @author Robert Varga
24  *
25  * @param <K> the type of keys
26  */
27 final class MutableKeySet<K> extends AbstractKeySet<K> {
28     MutableKeySet(final MutableTrieMap<K, ?> map) {
29         super(map);
30     }
31
32     @Override
33     public Iterator<K> iterator() {
34         final AbstractIterator<K, ?> itr = map().iterator();
35         return new Iterator<K>() {
36             @Override
37             public boolean hasNext() {
38                 return itr.hasNext();
39             }
40
41             @Override
42             public K next() {
43                 return itr.next().getKey();
44             }
45
46             @Override
47             public void remove() {
48                 itr.remove();
49             }
50         };
51     }
52
53     @Override
54     public void clear() {
55         map().clear();
56     }
57
58     @Override
59     public boolean remove(final Object o) {
60         return map().remove(o) != null;
61     }
62 }