From fd014473d03eab91313100970b2256156d3630c4 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Tue, 3 Dec 2013 22:29:54 +0100 Subject: [PATCH] expose key and value iterators in ImmutableOpenMap --- .../cluster/node/DiscoveryNodes.java | 19 +------- .../common/collect/ImmutableOpenMap.java | 48 +++++++++++++++++-- .../inject/internal/UnmodifiableIterator.java | 36 -------------- 3 files changed, 45 insertions(+), 58 deletions(-) delete mode 100644 src/main/java/org/elasticsearch/common/inject/internal/UnmodifiableIterator.java diff --git a/src/main/java/org/elasticsearch/cluster/node/DiscoveryNodes.java b/src/main/java/org/elasticsearch/cluster/node/DiscoveryNodes.java index 8e563638a20..5466cd7babe 100644 --- a/src/main/java/org/elasticsearch/cluster/node/DiscoveryNodes.java +++ b/src/main/java/org/elasticsearch/cluster/node/DiscoveryNodes.java @@ -34,7 +34,6 @@ import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.transport.TransportAddress; import java.io.IOException; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -42,7 +41,7 @@ import java.util.Set; import static com.google.common.collect.Lists.newArrayList; /** - * This class holds all {@link DiscoveryNode} in the cluster and provides convinience methods to + * This class holds all {@link DiscoveryNode} in the cluster and provides convenience methods to * access, modify merge / diff discovery nodes. */ public class DiscoveryNodes implements Iterable { @@ -50,13 +49,10 @@ public class DiscoveryNodes implements Iterable { public static final DiscoveryNodes EMPTY_NODES = builder().build(); private final ImmutableOpenMap nodes; - private final ImmutableOpenMap dataNodes; - private final ImmutableOpenMap masterNodes; private final String masterNodeId; - private final String localNodeId; private DiscoveryNodes(ImmutableOpenMap nodes, ImmutableOpenMap dataNodes, ImmutableOpenMap masterNodes, String masterNodeId, String localNodeId) { @@ -69,18 +65,7 @@ public class DiscoveryNodes implements Iterable { @Override public UnmodifiableIterator iterator() { - final Iterator> cursor = nodes.values().iterator(); - return new UnmodifiableIterator() { - @Override - public boolean hasNext() { - return cursor.hasNext(); - } - - @Override - public DiscoveryNode next() { - return cursor.next().value; - } - }; + return nodes.valuesIt(); } /** diff --git a/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java b/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java index d8475f0fe70..befcfc5151d 100644 --- a/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java +++ b/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java @@ -20,9 +20,11 @@ package org.elasticsearch.common.collect; import com.carrotsearch.hppc.*; +import com.carrotsearch.hppc.cursors.ObjectCursor; import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.carrotsearch.hppc.predicates.ObjectPredicate; import com.carrotsearch.hppc.procedures.ObjectObjectProcedure; +import com.google.common.collect.UnmodifiableIterator; import java.util.Iterator; import java.util.Map; @@ -43,11 +45,11 @@ public final class ImmutableOpenMap implements Iterable - * Important note: For primitive type values, the value returned for a non-existing - * key may not be the default value of the primitive type (it may be any value previously - * assigned to that slot). + * for the key type, if the key is not associated with any value. + *

+ * Important note: For primitive type values, the value returned for a non-existing + * key may not be the default value of the primitive type (it may be any value previously + * assigned to that slot). */ public VType get(KType key) { return map.get(key); @@ -106,6 +108,24 @@ public final class ImmutableOpenMap implements Iterable keysIt() { + final Iterator> iterator = map.keys().iterator(); + return new UnmodifiableIterator() { + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public KType next() { + return iterator.next().value; + } + }; + } + /** * @return Returns a container with all values stored in this map. */ @@ -113,6 +133,24 @@ public final class ImmutableOpenMap implements Iterable valuesIt() { + final Iterator> iterator = map.values().iterator(); + return new UnmodifiableIterator() { + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public VType next() { + return iterator.next().value; + } + }; + } + @Override public String toString() { return map.toString(); diff --git a/src/main/java/org/elasticsearch/common/inject/internal/UnmodifiableIterator.java b/src/main/java/org/elasticsearch/common/inject/internal/UnmodifiableIterator.java deleted file mode 100644 index 19eb6ecd938..00000000000 --- a/src/main/java/org/elasticsearch/common/inject/internal/UnmodifiableIterator.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2008 Google Inc. - * - * 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.elasticsearch.common.inject.internal; - -import java.util.Iterator; - -/** - * An iterator that does not support {@link #remove}. - * - * @author Jared Levy - */ -public abstract class UnmodifiableIterator implements Iterator { - - /** - * Guaranteed to throw an exception and leave the underlying data unmodified. - * - * @throws UnsupportedOperationException always - */ - public final void remove() { - throw new UnsupportedOperationException(); - } -}