From: Robert Varga Date: Tue, 3 Jan 2017 21:16:17 +0000 (+0100) Subject: BUG-7464: Add dedicated key set classes X-Git-Tag: release/carbon~118 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=422344ce9059492bebc277edf22189cd3d4ec178;p=yangtools.git BUG-7464: Add dedicated key set classes These are slightly more efficient versions of the default key set and react to Set.remove(). Change-Id: If67d4c75210d22dafbd2cd2dabeebc8554505a5e Signed-off-by: Robert Varga --- diff --git a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/AbstractKeySet.java b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/AbstractKeySet.java new file mode 100644 index 0000000000..8e80392fab --- /dev/null +++ b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/AbstractKeySet.java @@ -0,0 +1,65 @@ +/* + * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.opendaylight.yangtools.triemap; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.AbstractSet; +import java.util.Collection; + +/** + * Abstract base class for key set views of a TrieMap + * + * @author Robert Varga + * + * @param the type of keys + */ +abstract class AbstractKeySet extends AbstractSet { + private final TrieMap map; + + AbstractKeySet(final TrieMap map) { + this.map = checkNotNull(map); + } + + final TrieMap map() { + return map; + } + + @Override + public final boolean add(final K e) { + throw new UnsupportedOperationException(); + } + + @Override + public final boolean addAll(final Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public final boolean contains(final Object o) { + return map.containsKey(o); + } + + @Override + public final boolean isEmpty() { + return map.isEmpty(); + } + + @Override + public final int size() { + return map.size(); + } +} diff --git a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/ImmutableKeySet.java b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/ImmutableKeySet.java new file mode 100644 index 0000000000..fa8f39f3af --- /dev/null +++ b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/ImmutableKeySet.java @@ -0,0 +1,60 @@ +/* + * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.opendaylight.yangtools.triemap; + +import static org.opendaylight.yangtools.triemap.ImmutableTrieMap.unsupported; + +import com.google.common.collect.Iterators; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map.Entry; + +/** + * An immutable view of a TrieMap's key set. + * + * @author Robert Varga + * + * @param the type of keys + */ +final class ImmutableKeySet extends AbstractKeySet { + ImmutableKeySet(final TrieMap map) { + super(map); + } + + @Override + public Iterator iterator() { + return Iterators.transform(map().immutableIterator(), Entry::getKey); + } + + @Override + public void clear() { + throw unsupported(); + } + + @Override + public boolean remove(final Object o) { + throw unsupported(); + } + @Override + public boolean retainAll(final Collection c) { + throw unsupported(); + } + + @Override + public boolean removeAll(final Collection c) { + throw unsupported(); + } +} diff --git a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/ImmutableTrieMap.java b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/ImmutableTrieMap.java index 0949f99db6..17391cfcfb 100644 --- a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/ImmutableTrieMap.java +++ b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/ImmutableTrieMap.java @@ -123,6 +123,11 @@ public final class ImmutableTrieMap extends TrieMap { return new ImmutableEntrySet<>(this); } + @Override + ImmutableKeySet createKeySet() { + return new ImmutableKeySet<>(this); + } + @Override boolean isReadOnly() { return true; diff --git a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/MutableKeySet.java b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/MutableKeySet.java new file mode 100644 index 0000000000..f6d5d2c772 --- /dev/null +++ b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/MutableKeySet.java @@ -0,0 +1,62 @@ +/* + * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.opendaylight.yangtools.triemap; + +import java.util.Iterator; + +/** + * A mutable view of a TrieMap's key set. + * + * @author Robert Varga + * + * @param the type of keys + */ +final class MutableKeySet extends AbstractKeySet { + MutableKeySet(final MutableTrieMap map) { + super(map); + } + + @Override + public Iterator iterator() { + final AbstractIterator itr = map().iterator(); + return new Iterator() { + @Override + public boolean hasNext() { + return itr.hasNext(); + } + + @Override + public K next() { + return itr.next().getKey(); + } + + @Override + public void remove() { + itr.remove(); + } + }; + } + + @Override + public void clear() { + map().clear(); + } + + @Override + public boolean remove(final Object o) { + return map().remove(o) != null; + } +} diff --git a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/MutableTrieMap.java b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/MutableTrieMap.java index fa74bf0bb1..ed12e7c2e4 100644 --- a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/MutableTrieMap.java +++ b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/MutableTrieMap.java @@ -140,6 +140,11 @@ final class MutableTrieMap extends TrieMap { return new MutableEntrySet<>(this); } + @Override + MutableKeySet createKeySet() { + return new MutableKeySet<>(this); + } + @Override MutableIterator iterator() { return new MutableIterator<>(this); diff --git a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/TrieMap.java b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/TrieMap.java index 2a0111af41..a7a92ee1bc 100644 --- a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/TrieMap.java +++ b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/TrieMap.java @@ -44,6 +44,7 @@ public abstract class TrieMap extends AbstractMap implements Concurr private final Equivalence equiv; private AbstractEntrySet entrySet; + private AbstractKeySet keySet; TrieMap(final Equivalence equiv) { this.equiv = equiv; @@ -99,6 +100,15 @@ public abstract class TrieMap extends AbstractMap implements Concurr return ret; } + @Override + public final Set keySet() { + AbstractKeySet ret = keySet; + if (ret == null) { + keySet = ret = createKeySet(); + } + return ret; + } + @Override public final V get(final Object key) { @SuppressWarnings("unchecked") @@ -134,6 +144,8 @@ public abstract class TrieMap extends AbstractMap implements Concurr abstract AbstractEntrySet createEntrySet(); + abstract AbstractKeySet createKeySet(); + abstract boolean isReadOnly(); abstract INode RDCSS_READ_ROOT(boolean abort);