expose key and value iterators in ImmutableOpenMap

This commit is contained in:
Shay Banon 2013-12-03 22:29:54 +01:00
parent 84d7929c3b
commit fd014473d0
3 changed files with 45 additions and 58 deletions

View File

@ -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<DiscoveryNode> {
@ -50,13 +49,10 @@ public class DiscoveryNodes implements Iterable<DiscoveryNode> {
public static final DiscoveryNodes EMPTY_NODES = builder().build();
private final ImmutableOpenMap<String, DiscoveryNode> nodes;
private final ImmutableOpenMap<String, DiscoveryNode> dataNodes;
private final ImmutableOpenMap<String, DiscoveryNode> masterNodes;
private final String masterNodeId;
private final String localNodeId;
private DiscoveryNodes(ImmutableOpenMap<String, DiscoveryNode> nodes, ImmutableOpenMap<String, DiscoveryNode> dataNodes, ImmutableOpenMap<String, DiscoveryNode> masterNodes, String masterNodeId, String localNodeId) {
@ -69,18 +65,7 @@ public class DiscoveryNodes implements Iterable<DiscoveryNode> {
@Override
public UnmodifiableIterator<DiscoveryNode> iterator() {
final Iterator<ObjectCursor<DiscoveryNode>> cursor = nodes.values().iterator();
return new UnmodifiableIterator<DiscoveryNode>() {
@Override
public boolean hasNext() {
return cursor.hasNext();
}
@Override
public DiscoveryNode next() {
return cursor.next().value;
}
};
return nodes.valuesIt();
}
/**

View File

@ -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<KType, VType> implements Iterable<ObjectObje
/**
* @return Returns the value associated with the given key or the default value
* for the key type, if the key is not associated with any value.
* <p/>
* <b>Important note:</b> 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.
* <p/>
* <b>Important note:</b> 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<KType, VType> implements Iterable<ObjectObje
return map.keys();
}
/**
* Returns a direct iterator over the keys.
*/
public UnmodifiableIterator<KType> keysIt() {
final Iterator<ObjectCursor<KType>> iterator = map.keys().iterator();
return new UnmodifiableIterator<KType>() {
@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<KType, VType> implements Iterable<ObjectObje
return map.values();
}
/**
* Returns a direct iterator over the keys.
*/
public UnmodifiableIterator<VType> valuesIt() {
final Iterator<ObjectCursor<VType>> iterator = map.values().iterator();
return new UnmodifiableIterator<VType>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public VType next() {
return iterator.next().value;
}
};
}
@Override
public String toString() {
return map.toString();

View File

@ -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<E> implements Iterator<E> {
/**
* Guaranteed to throw an exception and leave the underlying data unmodified.
*
* @throws UnsupportedOperationException always
*/
public final void remove() {
throw new UnsupportedOperationException();
}
}