From b0ab02e35cd3ae582122c5e8c5ce3f580e82523f Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 23 Sep 2015 15:37:49 -0400 Subject: [PATCH] Remove and ban ImmutableSet#copyOf It was used heavily in the Guice we've embedded. --- .../cluster/block/ClusterBlocks.java | 3 +- .../routing/allocation/RoutingAllocation.java | 10 +-- .../common/inject/ConfigurationException.java | 10 +-- .../common/inject/CreationException.java | 6 +- .../common/inject/InjectorBuilder.java | 65 ++++++++++--------- .../common/inject/InjectorShell.java | 5 +- .../common/inject/ProvisionException.java | 15 +++-- .../assistedinject/FactoryProvider.java | 8 ++- .../inject/internal/BindingBuilder.java | 17 +++-- .../internal/ConstantBindingBuilderImpl.java | 6 +- .../common/inject/internal/Errors.java | 5 +- .../inject/internal/InstanceBindingImpl.java | 18 +++-- .../internal/ProviderInstanceBindingImpl.java | 18 +++-- .../inject/internal/ProviderMethod.java | 11 ++-- .../internal/ProviderMethodsModule.java | 9 ++- .../inject/internal/SourceProvider.java | 38 +++++------ .../inject/multibindings/Multibinder.java | 9 ++- .../common/inject/spi/Dependency.java | 13 ++-- .../common/inject/spi/InjectionPoint.java | 19 +++--- .../common/inject/util/Modules.java | 14 ++-- .../elasticsearch/common/util/set/Sets.java | 49 ++++++++++++++ .../gateway/AsyncShardFetch.java | 11 +++- .../analysis/HtmlStripCharFilterFactory.java | 15 ++--- .../elasticsearch/indices/IndicesService.java | 13 ++-- .../cluster/ClusterInfoServiceIT.java | 9 ++- .../resources/forbidden/all-signatures.txt | 4 ++ 26 files changed, 250 insertions(+), 150 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlocks.java b/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlocks.java index 950b5381f49..d4945747c01 100644 --- a/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlocks.java +++ b/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlocks.java @@ -20,7 +20,6 @@ package org.elasticsearch.cluster.block; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.metadata.IndexMetaData; @@ -152,7 +151,7 @@ public class ClusterBlocks extends AbstractDiffable { if (global(level).isEmpty()) { return null; } - return new ClusterBlockException(ImmutableSet.copyOf(global(level))); + return new ClusterBlockException(global(level)); } public void indexBlockedRaiseException(ClusterBlockLevel level, String index) throws ClusterBlockException { diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/allocation/RoutingAllocation.java b/core/src/main/java/org/elasticsearch/cluster/routing/allocation/RoutingAllocation.java index 5e1d150b26b..1874a7b020b 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/allocation/RoutingAllocation.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/allocation/RoutingAllocation.java @@ -19,7 +19,6 @@ package org.elasticsearch.cluster.routing.allocation; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.cluster.ClusterInfo; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.node.DiscoveryNodes; @@ -34,6 +33,9 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import static java.util.Collections.emptySet; +import static java.util.Collections.unmodifiableSet; + /** * The {@link RoutingAllocation} keep the state of the current allocation * of shards and holds the {@link AllocationDeciders} which are responsible @@ -220,13 +222,13 @@ public class RoutingAllocation { public Set getIgnoreNodes(ShardId shardId) { if (ignoredShardToNodes == null) { - return ImmutableSet.of(); + return emptySet(); } Set ignore = ignoredShardToNodes.get(shardId); if (ignore == null) { - return ImmutableSet.of(); + return emptySet(); } - return ImmutableSet.copyOf(ignore); + return unmodifiableSet(new HashSet<>(ignore)); } /** diff --git a/core/src/main/java/org/elasticsearch/common/inject/ConfigurationException.java b/core/src/main/java/org/elasticsearch/common/inject/ConfigurationException.java index 3aaed0d27ac..870fece6fdf 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/ConfigurationException.java +++ b/core/src/main/java/org/elasticsearch/common/inject/ConfigurationException.java @@ -16,12 +16,15 @@ package org.elasticsearch.common.inject; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.inject.internal.Errors; import org.elasticsearch.common.inject.spi.Message; import java.util.Collection; import java.util.Locale; +import java.util.Set; + +import static java.util.Collections.unmodifiableSet; +import static org.elasticsearch.common.util.set.Sets.newHashSet; /** * Thrown when a programming error such as a misplaced annotation, illegal binding, or unsupported @@ -31,15 +34,14 @@ import java.util.Locale; * @since 2.0 */ public final class ConfigurationException extends RuntimeException { - - private final ImmutableSet messages; + private final Set messages; private Object partialValue = null; /** * Creates a ConfigurationException containing {@code messages}. */ public ConfigurationException(Iterable messages) { - this.messages = ImmutableSet.copyOf(messages); + this.messages = unmodifiableSet(newHashSet(messages)); initCause(Errors.getOnlyCause(this.messages)); } diff --git a/core/src/main/java/org/elasticsearch/common/inject/CreationException.java b/core/src/main/java/org/elasticsearch/common/inject/CreationException.java index 4900efe6a3e..4c8e94bd8f8 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/CreationException.java +++ b/core/src/main/java/org/elasticsearch/common/inject/CreationException.java @@ -16,7 +16,6 @@ package org.elasticsearch.common.inject; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.inject.internal.Errors; import org.elasticsearch.common.inject.spi.Message; @@ -29,14 +28,13 @@ import java.util.Collection; * @author crazybob@google.com (Bob Lee) */ public class CreationException extends RuntimeException { - - private final ImmutableSet messages; + private final Collection messages; /** * Creates a CreationException containing {@code messages}. */ public CreationException(Collection messages) { - this.messages = ImmutableSet.copyOf(messages); + this.messages = messages; if (this.messages.isEmpty()) { throw new IllegalArgumentException(); } diff --git a/core/src/main/java/org/elasticsearch/common/inject/InjectorBuilder.java b/core/src/main/java/org/elasticsearch/common/inject/InjectorBuilder.java index 4a9ba4ae3fc..2f9a3f1e7cb 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/InjectorBuilder.java +++ b/core/src/main/java/org/elasticsearch/common/inject/InjectorBuilder.java @@ -16,15 +16,15 @@ package org.elasticsearch.common.inject; -import com.google.common.collect.ImmutableSet; -import org.elasticsearch.common.inject.internal.*; +import org.elasticsearch.common.inject.internal.BindingImpl; +import org.elasticsearch.common.inject.internal.Errors; +import org.elasticsearch.common.inject.internal.ErrorsException; +import org.elasticsearch.common.inject.internal.InternalContext; +import org.elasticsearch.common.inject.internal.Stopwatch; import org.elasticsearch.common.inject.spi.Dependency; -import org.elasticsearch.common.util.iterable.Iterables; -import java.util.Collection; import java.util.List; import java.util.Map; -import java.util.Set; /** * Builds a tree of injectors. This is a primary injector, plus child injectors needed for each @@ -182,35 +182,38 @@ class InjectorBuilder { * Loads eager singletons, or all singletons if we're in Stage.PRODUCTION. Bindings discovered * while we're binding these singletons are not be eager. */ - public void loadEagerSingletons(InjectorImpl injector, Stage stage, final Errors errors) { - @SuppressWarnings("unchecked") // casting Collection to Collection is safe - Set> candidateBindings = ImmutableSet.copyOf(Iterables.concat( - (Collection) injector.state.getExplicitBindingsThisLevel().values(), - injector.jitBindings.values())); - for (final BindingImpl binding : candidateBindings) { - if (binding.getScoping().isEagerSingleton(stage)) { - try { - injector.callInContext(new ContextualCallable() { - Dependency dependency = Dependency.get(binding.getKey()); + public void loadEagerSingletons(InjectorImpl injector, Stage stage, Errors errors) { + for (final Binding binding : injector.state.getExplicitBindingsThisLevel().values()) { + loadEagerSingletons(injector, stage, errors, (BindingImpl)binding); + } + for (final Binding binding : injector.jitBindings.values()) { + loadEagerSingletons(injector, stage, errors, (BindingImpl)binding); + } + } - @Override - public Void call(InternalContext context) { - context.setDependency(dependency); - Errors errorsForBinding = errors.withSource(dependency); - try { - binding.getInternalFactory().get(errorsForBinding, context, dependency); - } catch (ErrorsException e) { - errorsForBinding.merge(e.getErrors()); - } finally { - context.setDependency(null); - } + private void loadEagerSingletons(InjectorImpl injector, Stage stage, final Errors errors, BindingImpl binding) { + if (binding.getScoping().isEagerSingleton(stage)) { + try { + injector.callInContext(new ContextualCallable() { + Dependency dependency = Dependency.get(binding.getKey()); - return null; + @Override + public Void call(InternalContext context) { + context.setDependency(dependency); + Errors errorsForBinding = errors.withSource(dependency); + try { + binding.getInternalFactory().get(errorsForBinding, context, dependency); + } catch (ErrorsException e) { + errorsForBinding.merge(e.getErrors()); + } finally { + context.setDependency(null); } - }); - } catch (ErrorsException e) { - throw new AssertionError(); - } + + return null; + } + }); + } catch (ErrorsException e) { + throw new AssertionError(); } } } diff --git a/core/src/main/java/org/elasticsearch/common/inject/InjectorShell.java b/core/src/main/java/org/elasticsearch/common/inject/InjectorShell.java index aeb3f2e5e66..8368ec1dfd7 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/InjectorShell.java +++ b/core/src/main/java/org/elasticsearch/common/inject/InjectorShell.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Objects; import java.util.logging.Logger; +import static java.util.Collections.emptySet; import static org.elasticsearch.common.inject.Scopes.SINGLETON; /** @@ -185,7 +186,7 @@ class InjectorShell { injector.state.putBinding(key, new ProviderInstanceBindingImpl<>(injector, key, SourceProvider.UNKNOWN_SOURCE, injectorFactory, Scoping.UNSCOPED, injectorFactory, - ImmutableSet.of())); + emptySet())); } private static class InjectorFactory implements InternalFactory, Provider { @@ -222,7 +223,7 @@ class InjectorShell { injector.state.putBinding(key, new ProviderInstanceBindingImpl<>(injector, key, SourceProvider.UNKNOWN_SOURCE, loggerFactory, Scoping.UNSCOPED, - loggerFactory, ImmutableSet.of())); + loggerFactory, emptySet())); } private static class LoggerFactory implements InternalFactory, Provider { diff --git a/core/src/main/java/org/elasticsearch/common/inject/ProvisionException.java b/core/src/main/java/org/elasticsearch/common/inject/ProvisionException.java index 6fc7c30c7d9..c8887af7b33 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/ProvisionException.java +++ b/core/src/main/java/org/elasticsearch/common/inject/ProvisionException.java @@ -16,12 +16,16 @@ package org.elasticsearch.common.inject; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.inject.internal.Errors; import org.elasticsearch.common.inject.spi.Message; import java.util.Collection; import java.util.Collections; +import java.util.Set; + +import static java.util.Collections.singleton; +import static java.util.Collections.unmodifiableSet; +import static org.elasticsearch.common.util.set.Sets.newHashSet; /** * Indicates that there was a runtime failure while providing an instance. @@ -31,14 +35,13 @@ import java.util.Collections; * @since 2.0 */ public final class ProvisionException extends RuntimeException { - - private final ImmutableSet messages; + private final Set messages; /** * Creates a ConfigurationException containing {@code messages}. */ public ProvisionException(Iterable messages) { - this.messages = ImmutableSet.copyOf(messages); + this.messages = unmodifiableSet(newHashSet(messages)); if (this.messages.isEmpty()) { throw new IllegalArgumentException(); } @@ -47,11 +50,11 @@ public final class ProvisionException extends RuntimeException { public ProvisionException(String message, Throwable cause) { super(cause); - this.messages = ImmutableSet.of(new Message(Collections.emptyList(), message, cause)); + this.messages = singleton(new Message(Collections.emptyList(), message, cause)); } public ProvisionException(String message) { - this.messages = ImmutableSet.of(new Message(message)); + this.messages = singleton(new Message(message)); } /** diff --git a/core/src/main/java/org/elasticsearch/common/inject/assistedinject/FactoryProvider.java b/core/src/main/java/org/elasticsearch/common/inject/assistedinject/FactoryProvider.java index 78aeb56fdca..fb1395d834c 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/assistedinject/FactoryProvider.java +++ b/core/src/main/java/org/elasticsearch/common/inject/assistedinject/FactoryProvider.java @@ -18,6 +18,7 @@ package org.elasticsearch.common.inject.assistedinject; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; + import org.elasticsearch.common.inject.ConfigurationException; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Injector; @@ -37,10 +38,13 @@ import java.lang.reflect.Proxy; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import static java.util.Collections.unmodifiableSet; + /** * Provides a factory that combines the caller's arguments with injector-supplied values to * construct objects. @@ -279,7 +283,7 @@ public class FactoryProvider implements Provider, HasDependencies { @Override public Set> getDependencies() { - List> dependencies = new ArrayList<>(); + Set> dependencies = new HashSet<>(); for (AssistedConstructor constructor : factoryMethodToConstructor.values()) { for (Parameter parameter : constructor.getAllParameters()) { if (!parameter.isProvidedByFactory()) { @@ -287,7 +291,7 @@ public class FactoryProvider implements Provider, HasDependencies { } } } - return ImmutableSet.copyOf(dependencies); + return unmodifiableSet(dependencies); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/inject/internal/BindingBuilder.java b/core/src/main/java/org/elasticsearch/common/inject/internal/BindingBuilder.java index 6b03be5796d..6e441d7422d 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/internal/BindingBuilder.java +++ b/core/src/main/java/org/elasticsearch/common/inject/internal/BindingBuilder.java @@ -16,18 +16,25 @@ package org.elasticsearch.common.inject.internal; -import com.google.common.collect.ImmutableSet; -import org.elasticsearch.common.inject.*; +import org.elasticsearch.common.inject.Binder; +import org.elasticsearch.common.inject.ConfigurationException; +import org.elasticsearch.common.inject.Key; +import org.elasticsearch.common.inject.Provider; +import org.elasticsearch.common.inject.TypeLiteral; import org.elasticsearch.common.inject.binder.AnnotatedBindingBuilder; import org.elasticsearch.common.inject.spi.Element; import org.elasticsearch.common.inject.spi.InjectionPoint; import org.elasticsearch.common.inject.spi.Message; import java.lang.annotation.Annotation; +import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Set; +import static java.util.Collections.emptySet; +import static java.util.Collections.unmodifiableSet; + /** * Bind a non-constant key. * @@ -85,11 +92,11 @@ public class BindingBuilder extends AbstractBindingBuilder for (Message message : e.getErrorMessages()) { binder.addError(message); } - injectionPoints = e.getPartialValue(); + injectionPoints = unmodifiableSet(new HashSet<>(e.getPartialValue())); } } else { binder.addError(BINDING_TO_NULL); - injectionPoints = ImmutableSet.of(); + injectionPoints = emptySet(); } BindingImpl base = getBinding(); @@ -110,7 +117,7 @@ public class BindingBuilder extends AbstractBindingBuilder for (Message message : e.getErrorMessages()) { binder.addError(message); } - injectionPoints = e.getPartialValue(); + injectionPoints = unmodifiableSet(new HashSet<>(e.getPartialValue())); } BindingImpl base = getBinding(); diff --git a/core/src/main/java/org/elasticsearch/common/inject/internal/ConstantBindingBuilderImpl.java b/core/src/main/java/org/elasticsearch/common/inject/internal/ConstantBindingBuilderImpl.java index 1cf37c11994..4e67892c3f4 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/internal/ConstantBindingBuilderImpl.java +++ b/core/src/main/java/org/elasticsearch/common/inject/internal/ConstantBindingBuilderImpl.java @@ -16,17 +16,17 @@ package org.elasticsearch.common.inject.internal; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.inject.Binder; import org.elasticsearch.common.inject.Key; import org.elasticsearch.common.inject.binder.AnnotatedConstantBindingBuilder; import org.elasticsearch.common.inject.binder.ConstantBindingBuilder; import org.elasticsearch.common.inject.spi.Element; -import org.elasticsearch.common.inject.spi.InjectionPoint; import java.lang.annotation.Annotation; import java.util.List; +import static java.util.Collections.emptySet; + /** * Bind a constant. * @@ -130,7 +130,7 @@ public final class ConstantBindingBuilderImpl } setBinding(new InstanceBindingImpl<>( - base.getSource(), key, base.getScoping(), ImmutableSet.of(), instanceAsT)); + base.getSource(), key, base.getScoping(), emptySet(), instanceAsT)); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/inject/internal/Errors.java b/core/src/main/java/org/elasticsearch/common/inject/internal/Errors.java index 6c9d155d6b7..acfcc178814 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/internal/Errors.java +++ b/core/src/main/java/org/elasticsearch/common/inject/internal/Errors.java @@ -17,6 +17,7 @@ package org.elasticsearch.common.inject.internal; import com.google.common.collect.ImmutableSet; + import org.apache.lucene.util.CollectionUtil; import org.elasticsearch.common.inject.ConfigurationException; import org.elasticsearch.common.inject.CreationException; @@ -49,6 +50,8 @@ import java.util.Formatter; import java.util.List; import java.util.Locale; +import static java.util.Collections.unmodifiableList; + /** * A collection of error messages. If this type is passed as a method parameter, the method is * considered to have executed successfully only if new errors were not added to this collection. @@ -463,7 +466,7 @@ public final class Errors implements Serializable { } }); - return result; + return unmodifiableList(result); } /** diff --git a/core/src/main/java/org/elasticsearch/common/inject/internal/InstanceBindingImpl.java b/core/src/main/java/org/elasticsearch/common/inject/internal/InstanceBindingImpl.java index 73c447ea1fb..7b74e6483a3 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/internal/InstanceBindingImpl.java +++ b/core/src/main/java/org/elasticsearch/common/inject/internal/InstanceBindingImpl.java @@ -16,27 +16,33 @@ package org.elasticsearch.common.inject.internal; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.inject.Binder; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.inject.Key; import org.elasticsearch.common.inject.Provider; -import org.elasticsearch.common.inject.spi.*; +import org.elasticsearch.common.inject.spi.BindingTargetVisitor; +import org.elasticsearch.common.inject.spi.Dependency; +import org.elasticsearch.common.inject.spi.HasDependencies; +import org.elasticsearch.common.inject.spi.InjectionPoint; +import org.elasticsearch.common.inject.spi.InstanceBinding; import org.elasticsearch.common.inject.util.Providers; +import java.util.HashSet; import java.util.Set; +import static java.util.Collections.unmodifiableSet; + public class InstanceBindingImpl extends BindingImpl implements InstanceBinding { final T instance; final Provider provider; - final ImmutableSet injectionPoints; + final Set injectionPoints; public InstanceBindingImpl(Injector injector, Key key, Object source, InternalFactory internalFactory, Set injectionPoints, T instance) { super(injector, key, source, internalFactory, Scoping.UNSCOPED); - this.injectionPoints = ImmutableSet.copyOf(injectionPoints); + this.injectionPoints = injectionPoints; this.instance = instance; this.provider = Providers.of(instance); } @@ -44,7 +50,7 @@ public class InstanceBindingImpl extends BindingImpl implements InstanceBi public InstanceBindingImpl(Object source, Key key, Scoping scoping, Set injectionPoints, T instance) { super(source, key, scoping); - this.injectionPoints = ImmutableSet.copyOf(injectionPoints); + this.injectionPoints = injectionPoints; this.instance = instance; this.provider = Providers.of(instance); } @@ -72,7 +78,7 @@ public class InstanceBindingImpl extends BindingImpl implements InstanceBi @Override public Set> getDependencies() { return instance instanceof HasDependencies - ? ImmutableSet.copyOf(((HasDependencies) instance).getDependencies()) + ? unmodifiableSet(new HashSet<>((((HasDependencies) instance).getDependencies()))) : Dependency.forInjectionPoints(injectionPoints); } diff --git a/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderInstanceBindingImpl.java b/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderInstanceBindingImpl.java index 833793213c9..e95d8fe491b 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderInstanceBindingImpl.java +++ b/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderInstanceBindingImpl.java @@ -16,20 +16,26 @@ limitations under the License. package org.elasticsearch.common.inject.internal; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.inject.Binder; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.inject.Key; import org.elasticsearch.common.inject.Provider; -import org.elasticsearch.common.inject.spi.*; +import org.elasticsearch.common.inject.spi.BindingTargetVisitor; +import org.elasticsearch.common.inject.spi.Dependency; +import org.elasticsearch.common.inject.spi.HasDependencies; +import org.elasticsearch.common.inject.spi.InjectionPoint; +import org.elasticsearch.common.inject.spi.ProviderInstanceBinding; +import java.util.HashSet; import java.util.Set; +import static java.util.Collections.unmodifiableSet; + public final class ProviderInstanceBindingImpl extends BindingImpl implements ProviderInstanceBinding { final Provider providerInstance; - final ImmutableSet injectionPoints; + final Set injectionPoints; public ProviderInstanceBindingImpl(Injector injector, Key key, Object source, InternalFactory internalFactory, Scoping scoping, @@ -37,13 +43,13 @@ public final class ProviderInstanceBindingImpl extends BindingImpl Set injectionPoints) { super(injector, key, source, internalFactory, scoping); this.providerInstance = providerInstance; - this.injectionPoints = ImmutableSet.copyOf(injectionPoints); + this.injectionPoints = injectionPoints; } public ProviderInstanceBindingImpl(Object source, Key key, Scoping scoping, Set injectionPoints, Provider providerInstance) { super(source, key, scoping); - this.injectionPoints = ImmutableSet.copyOf(injectionPoints); + this.injectionPoints = injectionPoints; this.providerInstance = providerInstance; } @@ -65,7 +71,7 @@ public final class ProviderInstanceBindingImpl extends BindingImpl @Override public Set> getDependencies() { return providerInstance instanceof HasDependencies - ? ImmutableSet.copyOf(((HasDependencies) providerInstance).getDependencies()) + ? unmodifiableSet(new HashSet<>((((HasDependencies) providerInstance).getDependencies()))) : Dependency.forInjectionPoints(injectionPoints); } diff --git a/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderMethod.java b/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderMethod.java index a5d2f091d38..0cfafc4a30a 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderMethod.java +++ b/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderMethod.java @@ -16,8 +16,11 @@ package org.elasticsearch.common.inject.internal; -import com.google.common.collect.ImmutableSet; -import org.elasticsearch.common.inject.*; +import org.elasticsearch.common.inject.Binder; +import org.elasticsearch.common.inject.Exposed; +import org.elasticsearch.common.inject.Key; +import org.elasticsearch.common.inject.PrivateBinder; +import org.elasticsearch.common.inject.Provider; import org.elasticsearch.common.inject.spi.Dependency; import org.elasticsearch.common.inject.spi.ProviderWithDependencies; @@ -37,7 +40,7 @@ public class ProviderMethod implements ProviderWithDependencies { private final Class scopeAnnotation; private final Object instance; private final Method method; - private final ImmutableSet> dependencies; + private final Set> dependencies; private final List> parameterProviders; private final boolean exposed; @@ -45,7 +48,7 @@ public class ProviderMethod implements ProviderWithDependencies { * @param method the method to invoke. Its return type must be the same type as {@code key}. */ ProviderMethod(Key key, Method method, Object instance, - ImmutableSet> dependencies, List> parameterProviders, + Set> dependencies, List> parameterProviders, Class scopeAnnotation) { this.key = key; this.scopeAnnotation = scopeAnnotation; diff --git a/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderMethodsModule.java b/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderMethodsModule.java index fdd9402d144..1671b4a8eac 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderMethodsModule.java +++ b/core/src/main/java/org/elasticsearch/common/inject/internal/ProviderMethodsModule.java @@ -16,7 +16,6 @@ package org.elasticsearch.common.inject.internal; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.inject.Binder; import org.elasticsearch.common.inject.Key; import org.elasticsearch.common.inject.Module; @@ -31,8 +30,12 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Set; + +import static java.util.Collections.unmodifiableSet; /** * Creates bindings to methods annotated with {@literal @}{@link Provides}. Use the scope and @@ -94,7 +97,7 @@ public final class ProviderMethodsModule implements Module { Errors errors = new Errors(method); // prepare the parameter providers - List> dependencies = new ArrayList<>(); + Set> dependencies = new HashSet<>(); List> parameterProviders = new ArrayList<>(); List> parameterTypes = typeLiteral.getParameterTypes(method); Annotation[][] parameterAnnotations = method.getParameterAnnotations(); @@ -115,7 +118,7 @@ public final class ProviderMethodsModule implements Module { binder.addError(message); } - return new ProviderMethod<>(key, method, delegate, ImmutableSet.copyOf(dependencies), + return new ProviderMethod<>(key, method, delegate, unmodifiableSet(dependencies), parameterProviders, scopeAnnotation); } diff --git a/core/src/main/java/org/elasticsearch/common/inject/internal/SourceProvider.java b/core/src/main/java/org/elasticsearch/common/inject/internal/SourceProvider.java index d498de90c0a..138b8b0e735 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/internal/SourceProvider.java +++ b/core/src/main/java/org/elasticsearch/common/inject/internal/SourceProvider.java @@ -16,11 +16,11 @@ package org.elasticsearch.common.inject.internal; -import com.google.common.collect.ImmutableSet; -import org.elasticsearch.common.util.iterable.Iterables; +import java.util.HashSet; +import java.util.Set; -import java.util.ArrayList; -import java.util.List; +import static java.util.Collections.singleton; +import static java.util.Collections.unmodifiableSet; /** * Provides access to the calling line of code. @@ -34,35 +34,29 @@ public class SourceProvider { */ public static final Object UNKNOWN_SOURCE = "[unknown source]"; - private final ImmutableSet classNamesToSkip; + private final Set classNamesToSkip; public SourceProvider() { - this.classNamesToSkip = ImmutableSet.of(SourceProvider.class.getName()); + this.classNamesToSkip = singleton(SourceProvider.class.getName()); } - public static final SourceProvider DEFAULT_INSTANCE - = new SourceProvider(ImmutableSet.of(SourceProvider.class.getName())); + public static final SourceProvider DEFAULT_INSTANCE = new SourceProvider(); - private SourceProvider(Iterable classesToSkip) { - this.classNamesToSkip = ImmutableSet.copyOf(classesToSkip); + @SuppressWarnings("rawtypes") + private SourceProvider(SourceProvider copy, Class[] moreClassesToSkip) { + Set classNamesToSkip = new HashSet<>(copy.classNamesToSkip); + for (Class toSkip : moreClassesToSkip) { + classNamesToSkip.add(toSkip.getName()); + } + this.classNamesToSkip = unmodifiableSet(classNamesToSkip); } /** * Returns a new instance that also skips {@code moreClassesToSkip}. */ + @SuppressWarnings("rawtypes") public SourceProvider plusSkippedClasses(Class... moreClassesToSkip) { - return new SourceProvider(Iterables.concat(classNamesToSkip, asStrings(moreClassesToSkip))); - } - - /** - * Returns the class names as Strings - */ - private static List asStrings(Class... classes) { - List strings = new ArrayList<>(); - for (Class c : classes) { - strings.add(c.getName()); - } - return strings; + return new SourceProvider(this, moreClassesToSkip); } /** diff --git a/core/src/main/java/org/elasticsearch/common/inject/multibindings/Multibinder.java b/core/src/main/java/org/elasticsearch/common/inject/multibindings/Multibinder.java index 9455dc57ba5..1c84f7886d9 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/multibindings/Multibinder.java +++ b/core/src/main/java/org/elasticsearch/common/inject/multibindings/Multibinder.java @@ -17,6 +17,7 @@ package org.elasticsearch.common.inject.multibindings; import com.google.common.collect.ImmutableSet; + import org.elasticsearch.common.inject.Binder; import org.elasticsearch.common.inject.Binding; import org.elasticsearch.common.inject.ConfigurationException; @@ -37,11 +38,14 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; import java.util.Set; +import static java.util.Collections.unmodifiableSet; + /** * An API to bind multiple values separately, only to later inject them as a * complete collection. Multibinder is intended for use in your application's @@ -238,9 +242,8 @@ public abstract class Multibinder { @Inject public void initialize(Injector injector) { providers = new ArrayList<>(); - List> dependencies = new ArrayList<>(); + Set> dependencies = new HashSet<>(); for (Binding entry : injector.findBindingsByType(elementType)) { - if (keyMatches(entry.getKey())) { @SuppressWarnings("unchecked") // protected by findBindingsByType() Binding binding = (Binding) entry; @@ -249,7 +252,7 @@ public abstract class Multibinder { } } - this.dependencies = ImmutableSet.copyOf(dependencies); + this.dependencies = unmodifiableSet(dependencies); this.binder = null; } diff --git a/core/src/main/java/org/elasticsearch/common/inject/spi/Dependency.java b/core/src/main/java/org/elasticsearch/common/inject/spi/Dependency.java index 2bac853d2d9..07d70b2c05b 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/spi/Dependency.java +++ b/core/src/main/java/org/elasticsearch/common/inject/spi/Dependency.java @@ -16,14 +16,14 @@ package org.elasticsearch.common.inject.spi; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.inject.Key; -import java.util.ArrayList; -import java.util.List; import java.util.Objects; import java.util.Set; +import static java.util.Collections.unmodifiableSet; +import static org.elasticsearch.common.util.set.Sets.toFlatSet; + /** * A variable that can be resolved by an injector. *

@@ -60,11 +60,8 @@ public final class Dependency { * Returns the dependencies from the given injection points. */ public static Set> forInjectionPoints(Set injectionPoints) { - List> dependencies = new ArrayList<>(); - for (InjectionPoint injectionPoint : injectionPoints) { - dependencies.addAll(injectionPoint.getDependencies()); - } - return ImmutableSet.copyOf(dependencies); + return unmodifiableSet( + injectionPoints.stream().map(InjectionPoint::getDependencies).collect(toFlatSet())); } /** diff --git a/core/src/main/java/org/elasticsearch/common/inject/spi/InjectionPoint.java b/core/src/main/java/org/elasticsearch/common/inject/spi/InjectionPoint.java index 2aa07b4f009..53e0cb04263 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/spi/InjectionPoint.java +++ b/core/src/main/java/org/elasticsearch/common/inject/spi/InjectionPoint.java @@ -17,6 +17,7 @@ package org.elasticsearch.common.inject.spi; import com.google.common.collect.ImmutableSet; + import org.elasticsearch.common.inject.ConfigurationException; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Key; @@ -38,10 +39,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; +import static java.util.Collections.unmodifiableSet; import static org.elasticsearch.common.inject.internal.MoreTypes.getRawType; /** @@ -253,13 +256,13 @@ public final class InjectionPoint { * of the valid injection points. */ public static Set forStaticMethodsAndFields(TypeLiteral type) { - List sink = new ArrayList<>(); + Set result = new HashSet<>(); Errors errors = new Errors(); - addInjectionPoints(type, Factory.FIELDS, true, sink, errors); - addInjectionPoints(type, Factory.METHODS, true, sink, errors); + addInjectionPoints(type, Factory.FIELDS, true, result, errors); + addInjectionPoints(type, Factory.METHODS, true, result, errors); - ImmutableSet result = ImmutableSet.copyOf(sink); + result = unmodifiableSet(result); if (errors.hasErrors()) { throw new ConfigurationException(errors.getMessages()).withPartialValue(result); } @@ -293,14 +296,14 @@ public final class InjectionPoint { * of the valid injection points. */ public static Set forInstanceMethodsAndFields(TypeLiteral type) { - List sink = new ArrayList<>(); + Set result = new HashSet<>(); Errors errors = new Errors(); // TODO (crazybob): Filter out overridden members. - addInjectionPoints(type, Factory.FIELDS, false, sink, errors); - addInjectionPoints(type, Factory.METHODS, false, sink, errors); + addInjectionPoints(type, Factory.FIELDS, false, result, errors); + addInjectionPoints(type, Factory.METHODS, false, result, errors); - ImmutableSet result = ImmutableSet.copyOf(sink); + result = unmodifiableSet(result); if (errors.hasErrors()) { throw new ConfigurationException(errors.getMessages()).withPartialValue(result); } diff --git a/core/src/main/java/org/elasticsearch/common/inject/util/Modules.java b/core/src/main/java/org/elasticsearch/common/inject/util/Modules.java index f29e5aadae7..25dc07bec1e 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/util/Modules.java +++ b/core/src/main/java/org/elasticsearch/common/inject/util/Modules.java @@ -16,7 +16,6 @@ package org.elasticsearch.common.inject.util; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Binder; import org.elasticsearch.common.inject.Binding; @@ -40,6 +39,9 @@ import java.util.List; import java.util.Map; import java.util.Set; +import static java.util.Collections.unmodifiableSet; +import static org.elasticsearch.common.util.set.Sets.newHashSet; + /** * Static utility methods for creating and working with instances of {@link Module}. * @@ -94,15 +96,14 @@ public final class Modules { * Returns a new module that installs all of {@code modules}. */ public static Module combine(Module... modules) { - return combine(ImmutableSet.copyOf(modules)); + return combine(Arrays.asList(modules)); } /** * Returns a new module that installs all of {@code modules}. */ public static Module combine(Iterable modules) { - // TODO: infer type once JI-9019884 is fixed - final Set modulesSet = ImmutableSet.copyOf(modules); + final Set modulesSet = newHashSet(modules); return new Module() { @Override public void configure(Binder binder) { @@ -131,11 +132,10 @@ public final class Modules { } private static final class RealOverriddenModuleBuilder implements OverriddenModuleBuilder { - private final ImmutableSet baseModules; + private final Set baseModules; private RealOverriddenModuleBuilder(Iterable baseModules) { - // TODO: infer type once JI-9019884 is fixed - this.baseModules = ImmutableSet.copyOf(baseModules); + this.baseModules = unmodifiableSet(newHashSet(baseModules)); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/util/set/Sets.java b/core/src/main/java/org/elasticsearch/common/util/set/Sets.java index a4bfe5200af..7d8623f588f 100644 --- a/core/src/main/java/org/elasticsearch/common/util/set/Sets.java +++ b/core/src/main/java/org/elasticsearch/common/util/set/Sets.java @@ -21,11 +21,17 @@ package org.elasticsearch.common.util.set; import java.util.Collection; import java.util.Collections; +import java.util.EnumSet; import java.util.HashSet; import java.util.Iterator; import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BiConsumer; +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Collector; import java.util.stream.Collectors; public final class Sets { @@ -66,6 +72,14 @@ public final class Sets { return set; } + /** + * Collects {@code Stream>} into {@code Set}. + */ + @SuppressWarnings("unchecked") + public static Collector, Set, Set> toFlatSet() { + return (Collector, Set, Set>) (Object) ToFlatSetCollector.INSTANCE; + } + public static Set newConcurrentHashSet() { return Collections.newSetFromMap(new ConcurrentHashMap<>()); } @@ -89,4 +103,39 @@ public final class Sets { union.addAll(right); return union; } + + /** + * Collects {@code Stream>} into {@code Set}. + */ + private static enum ToFlatSetCollector implements Collector, Set, Set> { + INSTANCE; + + @Override + public Supplier> supplier() { + return HashSet::new; + } + + @Override + public BiConsumer, Collection> accumulator() { + return Collection::addAll; + } + + @Override + public BinaryOperator> combiner() { + return (lhs, rhs) -> { + lhs.addAll(rhs); + return lhs; + }; + } + + @Override + public Function, Set> finisher() { + return Function.identity(); + } + + @Override + public Set characteristics() { + return EnumSet.of(Collector.Characteristics.IDENTITY_FINISH, Collector.Characteristics.UNORDERED); + } + } } diff --git a/core/src/main/java/org/elasticsearch/gateway/AsyncShardFetch.java b/core/src/main/java/org/elasticsearch/gateway/AsyncShardFetch.java index 20a3dd6da61..eccff4d2bf6 100644 --- a/core/src/main/java/org/elasticsearch/gateway/AsyncShardFetch.java +++ b/core/src/main/java/org/elasticsearch/gateway/AsyncShardFetch.java @@ -20,6 +20,7 @@ package org.elasticsearch.gateway; import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.google.common.collect.ImmutableSet; + import org.elasticsearch.ElasticsearchTimeoutException; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.action.ActionListener; @@ -38,7 +39,13 @@ import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.transport.ReceiveTimeoutTransportException; -import java.util.*; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import static java.util.Collections.unmodifiableSet; /** * Allows to asynchronously fetch shard related data from other nodes for allocation, without blocking @@ -143,7 +150,7 @@ public abstract class AsyncShardFetch implements Rel } } } - Set allIgnoreNodes = ImmutableSet.copyOf(nodesToIgnore); + Set allIgnoreNodes = unmodifiableSet(new HashSet<>(nodesToIgnore)); // clear the nodes to ignore, we had a successful run in fetching everything we can // we need to try them if another full run is needed nodesToIgnore.clear(); diff --git a/core/src/main/java/org/elasticsearch/index/analysis/HtmlStripCharFilterFactory.java b/core/src/main/java/org/elasticsearch/index/analysis/HtmlStripCharFilterFactory.java index 18b2dda8375..93c80205867 100644 --- a/core/src/main/java/org/elasticsearch/index/analysis/HtmlStripCharFilterFactory.java +++ b/core/src/main/java/org/elasticsearch/index/analysis/HtmlStripCharFilterFactory.java @@ -19,7 +19,6 @@ package org.elasticsearch.index.analysis; -import com.google.common.collect.ImmutableSet; import org.apache.lucene.analysis.charfilter.HTMLStripCharFilter; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.assistedinject.Assisted; @@ -28,26 +27,26 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.settings.IndexSettings; import java.io.Reader; +import java.util.Set; + +import static java.util.Collections.unmodifiableSet; +import static org.elasticsearch.common.util.set.Sets.newHashSet; -/** - * - */ public class HtmlStripCharFilterFactory extends AbstractCharFilterFactory { - - private final ImmutableSet escapedTags; + private final Set escapedTags; @Inject public HtmlStripCharFilterFactory(Index index, @IndexSettings Settings indexSettings, @Assisted String name, @Assisted Settings settings) { super(index, indexSettings, name); String[] escapedTags = settings.getAsArray("escaped_tags"); if (escapedTags.length > 0) { - this.escapedTags = ImmutableSet.copyOf(escapedTags); + this.escapedTags = unmodifiableSet(newHashSet(escapedTags)); } else { this.escapedTags = null; } } - public ImmutableSet escapedTags() { + public Set escapedTags() { return escapedTags; } diff --git a/core/src/main/java/org/elasticsearch/indices/IndicesService.java b/core/src/main/java/org/elasticsearch/indices/IndicesService.java index 9cb8ec81634..3b24544267d 100644 --- a/core/src/main/java/org/elasticsearch/indices/IndicesService.java +++ b/core/src/main/java/org/elasticsearch/indices/IndicesService.java @@ -20,7 +20,7 @@ package org.elasticsearch.indices; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; + import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.util.CollectionUtil; import org.apache.lucene.util.IOUtils; @@ -87,14 +87,15 @@ import java.io.IOException; import java.nio.file.Files; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; import java.util.stream.Stream; import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; @@ -162,11 +163,11 @@ public class IndicesService extends AbstractLifecycleComponent i @Override protected void doStop() { - ImmutableSet indices = ImmutableSet.copyOf(this.indices.keySet()); + ExecutorService indicesStopExecutor = Executors.newFixedThreadPool(5, EsExecutors.daemonThreadFactory("indices_shutdown")); + + // Copy indices because we modify it asynchronously in the body of the loop + Set indices = new HashSet<>(this.indices.keySet()); final CountDownLatch latch = new CountDownLatch(indices.size()); - - final ExecutorService indicesStopExecutor = Executors.newFixedThreadPool(5, EsExecutors.daemonThreadFactory("indices_shutdown")); - for (final String index : indices) { indicesStopExecutor.execute(new Runnable() { @Override diff --git a/core/src/test/java/org/elasticsearch/cluster/ClusterInfoServiceIT.java b/core/src/test/java/org/elasticsearch/cluster/ClusterInfoServiceIT.java index 03721281116..c5ec864c54c 100644 --- a/core/src/test/java/org/elasticsearch/cluster/ClusterInfoServiceIT.java +++ b/core/src/test/java/org/elasticsearch/cluster/ClusterInfoServiceIT.java @@ -20,6 +20,7 @@ package org.elasticsearch.cluster; import com.google.common.collect.ImmutableSet; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionModule; @@ -59,7 +60,10 @@ import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicBoolean; +import static java.util.Collections.emptySet; +import static java.util.Collections.unmodifiableSet; import static org.elasticsearch.common.settings.Settings.settingsBuilder; +import static org.elasticsearch.common.util.set.Sets.newHashSet; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.Matchers.greaterThan; @@ -89,8 +93,7 @@ public class ClusterInfoServiceIT extends ESIntegTestCase { } public static class BlockingActionFilter extends org.elasticsearch.action.support.ActionFilter.Simple { - - ImmutableSet blockedActions = ImmutableSet.of(); + private Set blockedActions = emptySet(); @Inject public BlockingActionFilter(Settings settings) { @@ -116,7 +119,7 @@ public class ClusterInfoServiceIT extends ESIntegTestCase { } public void blockActions(String... actions) { - blockedActions = ImmutableSet.copyOf(actions); + blockedActions = unmodifiableSet(newHashSet(actions)); } } diff --git a/dev-tools/src/main/resources/forbidden/all-signatures.txt b/dev-tools/src/main/resources/forbidden/all-signatures.txt index 885f2b694c9..8fb564df8e7 100644 --- a/dev-tools/src/main/resources/forbidden/all-signatures.txt +++ b/dev-tools/src/main/resources/forbidden/all-signatures.txt @@ -129,6 +129,10 @@ com.google.common.io.Files com.google.common.primitives.Ints com.google.common.collect.ImmutableMap#entrySet() com.google.common.collect.ImmutableSet#builder() +com.google.common.collect.ImmutableSet#copyOf(java.lang.Iterable) +com.google.common.collect.ImmutableSet#copyOf(java.util.Iterator) +com.google.common.collect.ImmutableSet#copyOf(java.util.Collection) +com.google.common.collect.ImmutableSet#copyOf(java.lang.Object[]) @defaultMessage Do not violate java's access system java.lang.reflect.AccessibleObject#setAccessible(boolean)