diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java b/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java index 158d5818b29..2e2458cab60 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java @@ -33,7 +33,6 @@ import org.elasticsearch.cluster.routing.HashFunction; import org.elasticsearch.cluster.routing.Murmur3HashFunction; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseFieldMatcher; -import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.compress.CompressedXContent; @@ -205,8 +204,12 @@ public class IndexMetaData implements Diffable, FromXContentBuild private final boolean useTypeForRouting; private IndexMetaData(String index, long version, State state, Settings settings, ImmutableOpenMap mappings, ImmutableOpenMap aliases, ImmutableOpenMap customs) { - Preconditions.checkArgument(settings.getAsInt(SETTING_NUMBER_OF_SHARDS, null) != null, "must specify numberOfShards for index [" + index + "]"); - Preconditions.checkArgument(settings.getAsInt(SETTING_NUMBER_OF_REPLICAS, null) != null, "must specify numberOfReplicas for index [" + index + "]"); + if (settings.getAsInt(SETTING_NUMBER_OF_SHARDS, null) == null) { + throw new IllegalArgumentException("must specify numberOfShards for index [" + index + "]"); + } + if (settings.getAsInt(SETTING_NUMBER_OF_REPLICAS, null) == null) { + throw new IllegalArgumentException("must specify numberOfReplicas for index [" + index + "]"); + } this.index = index; this.version = version; this.state = state; diff --git a/core/src/main/java/org/elasticsearch/common/Preconditions.java b/core/src/main/java/org/elasticsearch/common/Preconditions.java deleted file mode 100644 index 7eddef1f010..00000000000 --- a/core/src/main/java/org/elasticsearch/common/Preconditions.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you 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; - -import java.util.Locale; - -public class Preconditions { - private Preconditions() { - } - - public static void checkArgument(boolean expression) { - if (!expression) { - throw new IllegalArgumentException(); - } - } - - public static void checkArgument(boolean expression, @Nullable Object errorMessage) { - if (!expression) { - throw new IllegalArgumentException(String.valueOf(errorMessage)); - } - } - - public static void checkArgument(boolean expression, @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs) { - if (!expression) { - throw new IllegalArgumentException(format(errorMessageTemplate, errorMessageArgs)); - } - } - - private static String format(@Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs) { - return String.format(Locale.ROOT, errorMessageTemplate, errorMessageArgs); - } -} diff --git a/core/src/main/java/org/elasticsearch/common/cli/CliTool.java b/core/src/main/java/org/elasticsearch/common/cli/CliTool.java index bea819151f9..0d32b6e2779 100644 --- a/core/src/main/java/org/elasticsearch/common/cli/CliTool.java +++ b/core/src/main/java/org/elasticsearch/common/cli/CliTool.java @@ -22,7 +22,6 @@ package org.elasticsearch.common.cli; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; -import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.node.internal.InternalSettingsPreparer; @@ -100,7 +99,9 @@ public abstract class CliTool { } protected CliTool(CliToolConfig config, Terminal terminal) { - Preconditions.checkArgument(config.cmds().size() != 0, "At least one command must be configured"); + if (config.cmds().size() == 0) { + throw new IllegalArgumentException("At least one command must be configured"); + } this.config = config; this.terminal = terminal; env = InternalSettingsPreparer.prepareEnvironment(EMPTY_SETTINGS, terminal); diff --git a/core/src/main/java/org/elasticsearch/common/collect/CopyOnWriteHashMap.java b/core/src/main/java/org/elasticsearch/common/collect/CopyOnWriteHashMap.java index fd288e859e7..eab20aaa23b 100644 --- a/core/src/main/java/org/elasticsearch/common/collect/CopyOnWriteHashMap.java +++ b/core/src/main/java/org/elasticsearch/common/collect/CopyOnWriteHashMap.java @@ -21,7 +21,6 @@ package org.elasticsearch.common.collect; import com.google.common.collect.UnmodifiableIterator; import org.apache.lucene.util.mutable.MutableValueInt; -import org.elasticsearch.common.Preconditions; import java.lang.reflect.Array; import java.util.*; @@ -474,7 +473,9 @@ public final class CopyOnWriteHashMap extends AbstractMap { @Override public V get(Object key) { - Preconditions.checkArgument(key != null, "Null keys are not supported"); + if (key == null) { + throw new IllegalArgumentException("null keys are not supported"); + } final int hash = key.hashCode(); return root.get(key, hash); } @@ -490,8 +491,12 @@ public final class CopyOnWriteHashMap extends AbstractMap { * of the hash table. The current hash table is not modified. */ public CopyOnWriteHashMap copyAndPut(K key, V value) { - Preconditions.checkArgument(key != null, "null keys are not supported"); - Preconditions.checkArgument(value != null, "null values are not supported"); + if (key == null) { + throw new IllegalArgumentException("null keys are not supported"); + } + if (value == null) { + throw new IllegalArgumentException("null values are not supported"); + } final int hash = key.hashCode(); final MutableValueInt newValue = new MutableValueInt(); final InnerNode newRoot = root.put(key, hash, TOTAL_HASH_BITS, value, newValue); @@ -522,7 +527,9 @@ public final class CopyOnWriteHashMap extends AbstractMap { * Remove the given key from this map. The current hash table is not modified. */ public CopyOnWriteHashMap copyAndRemove(Object key) { - Preconditions.checkArgument(key != null, "Null keys are not supported"); + if (key == null) { + throw new IllegalArgumentException("null keys are not supported"); + } final int hash = key.hashCode(); final InnerNode newRoot = root.remove(key, hash); if (root == newRoot) { 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 05c5f04823e..5c44111e600 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/CreationException.java +++ b/core/src/main/java/org/elasticsearch/common/inject/CreationException.java @@ -22,8 +22,6 @@ import org.elasticsearch.common.inject.spi.Message; import java.util.Collection; -import static org.elasticsearch.common.Preconditions.checkArgument; - /** * Thrown when errors occur while creating a {@link Injector}. Includes a list of encountered * errors. Clients should catch this exception, log it, and stop execution. @@ -39,7 +37,9 @@ public class CreationException extends RuntimeException { */ public CreationException(Collection messages) { this.messages = ImmutableSet.copyOf(messages); - checkArgument(!this.messages.isEmpty()); + if (this.messages.isEmpty()) { + throw new IllegalArgumentException(); + } initCause(Errors.getOnlyCause(this.messages)); } diff --git a/core/src/main/java/org/elasticsearch/common/inject/Key.java b/core/src/main/java/org/elasticsearch/common/inject/Key.java index 8ee801ccea3..92925c69b43 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/Key.java +++ b/core/src/main/java/org/elasticsearch/common/inject/Key.java @@ -24,8 +24,6 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.Objects; -import static org.elasticsearch.common.Preconditions.checkArgument; - /** * Binding key consisting of an injection type and an optional annotation. * Matches the type and annotation at a point of injection. @@ -367,16 +365,20 @@ public class Key { private static void ensureRetainedAtRuntime( Class annotationType) { - checkArgument(Annotations.isRetainedAtRuntime(annotationType), - "%s is not retained at runtime. Please annotate it with @Retention(RUNTIME).", - annotationType.getName()); + if (!Annotations.isRetainedAtRuntime(annotationType)) { + throw new IllegalArgumentException( + annotationType.getName() + " is not retained at runtime. Please annotate it with @Retention(RUNTIME)." + ); + } } private static void ensureIsBindingAnnotation( Class annotationType) { - checkArgument(isBindingAnnotation(annotationType), - "%s is not a binding annotation. Please annotate it with @BindingAnnotation.", - annotationType.getName()); + if (!isBindingAnnotation(annotationType)) { + throw new IllegalArgumentException( + annotationType.getName() + " is not a binding annotation. Please annotate it with @BindingAnnotation." + ); + } } static enum NullAnnotationStrategy implements AnnotationStrategy { 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 dea1bfba5ad..9497169d179 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/ProvisionException.java +++ b/core/src/main/java/org/elasticsearch/common/inject/ProvisionException.java @@ -23,8 +23,6 @@ import org.elasticsearch.common.inject.spi.Message; import java.util.Collection; import java.util.Collections; -import static org.elasticsearch.common.Preconditions.checkArgument; - /** * Indicates that there was a runtime failure while providing an instance. * @@ -41,7 +39,9 @@ public final class ProvisionException extends RuntimeException { */ public ProvisionException(Iterable messages) { this.messages = ImmutableSet.copyOf(messages); - checkArgument(!this.messages.isEmpty()); + if (this.messages.isEmpty()) { + throw new IllegalArgumentException(); + } initCause(Errors.getOnlyCause(this.messages)); } diff --git a/core/src/main/java/org/elasticsearch/common/inject/TypeLiteral.java b/core/src/main/java/org/elasticsearch/common/inject/TypeLiteral.java index 44c89e4a3c4..ee50b129dc3 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/TypeLiteral.java +++ b/core/src/main/java/org/elasticsearch/common/inject/TypeLiteral.java @@ -24,7 +24,6 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import static org.elasticsearch.common.Preconditions.checkArgument; import static org.elasticsearch.common.inject.internal.MoreTypes.canonicalize; /** @@ -257,8 +256,9 @@ public class TypeLiteral { * @since 2.0 */ public TypeLiteral getSupertype(Class supertype) { - checkArgument(supertype.isAssignableFrom(rawType), - "%s is not a supertype of %s", supertype, this.type); + if (!supertype.isAssignableFrom(rawType)) { + throw new IllegalArgumentException(supertype + " is not a supertype of " + type); + } return resolve(MoreTypes.getGenericSupertype(type, rawType, supertype)); } @@ -269,8 +269,9 @@ public class TypeLiteral { * @since 2.0 */ public TypeLiteral getFieldType(Field field) { - checkArgument(field.getDeclaringClass().isAssignableFrom(rawType), - "%s is not defined by a supertype of %s", field, type); + if (!field.getDeclaringClass().isAssignableFrom(rawType)) { + throw new IllegalArgumentException(field + " is not defined by a supertype of " + type); + } return resolve(field.getGenericType()); } @@ -285,14 +286,17 @@ public class TypeLiteral { if (methodOrConstructor instanceof Method) { Method method = (Method) methodOrConstructor; - checkArgument(method.getDeclaringClass().isAssignableFrom(rawType), - "%s is not defined by a supertype of %s", method, type); + if (!method.getDeclaringClass().isAssignableFrom(rawType)) { + throw new IllegalArgumentException(method + " is not defined by a supertype of " + type); + } genericParameterTypes = method.getGenericParameterTypes(); } else if (methodOrConstructor instanceof Constructor) { Constructor constructor = (Constructor) methodOrConstructor; - checkArgument(constructor.getDeclaringClass().isAssignableFrom(rawType), - "%s does not construct a supertype of %s", constructor, type); + if (!constructor.getDeclaringClass().isAssignableFrom(rawType)) { + throw new IllegalArgumentException(constructor + " does not construct a supertype of " + type); + } + genericParameterTypes = constructor.getGenericParameterTypes(); } else { @@ -313,14 +317,17 @@ public class TypeLiteral { if (methodOrConstructor instanceof Method) { Method method = (Method) methodOrConstructor; - checkArgument(method.getDeclaringClass().isAssignableFrom(rawType), - "%s is not defined by a supertype of %s", method, type); + if (!method.getDeclaringClass().isAssignableFrom(rawType)) { + throw new IllegalArgumentException(method + " is not defined by a supertype of " + type); + } + genericExceptionTypes = method.getGenericExceptionTypes(); } else if (methodOrConstructor instanceof Constructor) { Constructor constructor = (Constructor) methodOrConstructor; - checkArgument(constructor.getDeclaringClass().isAssignableFrom(rawType), - "%s does not construct a supertype of %s", constructor, type); + if (!constructor.getDeclaringClass().isAssignableFrom(rawType)) { + throw new IllegalArgumentException(constructor + " does not construct a supertype of " + type); + } genericExceptionTypes = constructor.getGenericExceptionTypes(); } else { @@ -337,8 +344,10 @@ public class TypeLiteral { * @since 2.0 */ public TypeLiteral getReturnType(Method method) { - checkArgument(method.getDeclaringClass().isAssignableFrom(rawType), - "%s is not defined by a supertype of %s", method, type); + if (!method.getDeclaringClass().isAssignableFrom(rawType)) { + throw new IllegalArgumentException(method + " is not defined by a supertype of " + type); + } + return resolve(method.getGenericReturnType()); } } diff --git a/core/src/main/java/org/elasticsearch/common/inject/assistedinject/Parameter.java b/core/src/main/java/org/elasticsearch/common/inject/assistedinject/Parameter.java index 1872a419a76..16e4deb65df 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/assistedinject/Parameter.java +++ b/core/src/main/java/org/elasticsearch/common/inject/assistedinject/Parameter.java @@ -22,8 +22,6 @@ import java.lang.annotation.Annotation; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import static org.elasticsearch.common.Preconditions.checkArgument; - /** * Models a method or constructor parameter. * @@ -144,8 +142,9 @@ class Parameter { Annotation bindingAnnotation = null; for (Annotation a : annotations) { if (a.annotationType().getAnnotation(BindingAnnotation.class) != null) { - checkArgument(bindingAnnotation == null, - "Parameter has multiple binding annotations: %s and %s", bindingAnnotation, a); + if (bindingAnnotation != null) { + throw new IllegalArgumentException("Parameter has multiple binding annotations: " + bindingAnnotation + " and " + a); + } bindingAnnotation = a; } } diff --git a/core/src/main/java/org/elasticsearch/common/inject/internal/MoreTypes.java b/core/src/main/java/org/elasticsearch/common/inject/internal/MoreTypes.java index f156a389501..2a6111674e7 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/internal/MoreTypes.java +++ b/core/src/main/java/org/elasticsearch/common/inject/internal/MoreTypes.java @@ -30,8 +30,6 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; -import static org.elasticsearch.common.Preconditions.checkArgument; - /** * Static methods for working with types that we aren't publishing in the * public {@code Types} API. @@ -152,8 +150,11 @@ public class MoreTypes { // Neal isn't either but suspects some pathological case related // to nested classes exists. Type rawType = parameterizedType.getRawType(); - checkArgument(rawType instanceof Class, - "Expected a Class, but <%s> is of type %s", type, type.getClass().getName()); + if (!(rawType instanceof Class)) { + throw new IllegalArgumentException( + "Expected a Class, but <" + type +"> is of type " + type.getClass().getName() + ); + } return (Class) rawType; } else if (type instanceof GenericArrayType) { @@ -445,10 +446,13 @@ public class MoreTypes { // require an owner type if the raw type needs it if (rawType instanceof Class) { Class rawTypeAsClass = (Class) rawType; - checkArgument(ownerType != null || rawTypeAsClass.getEnclosingClass() == null, - "No owner type for enclosed %s", rawType); - checkArgument(ownerType == null || rawTypeAsClass.getEnclosingClass() != null, - "Owner type for unenclosed %s", rawType); + if (ownerType == null && rawTypeAsClass.getEnclosingClass() != null) { + throw new IllegalArgumentException("No owner type for enclosed " + rawType); + } + if (ownerType != null && rawTypeAsClass.getEnclosingClass() == null) { + throw new IllegalArgumentException("Owner type for unenclosed " + rawType); + } + } this.ownerType = ownerType == null ? null : canonicalize(ownerType); @@ -561,13 +565,18 @@ public class MoreTypes { private final Type lowerBound; public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) { - checkArgument(lowerBounds.length <= 1, "Must have at most one lower bound."); - checkArgument(upperBounds.length == 1, "Must have exactly one upper bound."); - + if (lowerBounds.length > 1) { + throw new IllegalArgumentException("Must have at most one lower bound."); + } + if (upperBounds.length != 1) { + throw new IllegalArgumentException("Must have exactly one upper bound."); + } if (lowerBounds.length == 1) { Objects.requireNonNull(lowerBounds[0], "lowerBound"); checkNotPrimitive(lowerBounds[0], "wildcard bounds"); - checkArgument(upperBounds[0] == Object.class, "bounded both ways"); + if (upperBounds[0] != Object.class) { + throw new IllegalArgumentException("bounded both ways"); + } this.lowerBound = canonicalize(lowerBounds[0]); this.upperBound = Object.class; @@ -615,8 +624,9 @@ public class MoreTypes { } private static void checkNotPrimitive(Type type, String use) { - checkArgument(!(type instanceof Class) || !((Class) type).isPrimitive(), - "Primitive types are not allowed in %s: %s", use, type); + if (type instanceof Class && ((Class) type).isPrimitive()) { + throw new IllegalArgumentException("Primitive types are not allowed in " + use + ": " + type); + } } /** diff --git a/core/src/main/java/org/elasticsearch/common/inject/internal/PrivateElementsImpl.java b/core/src/main/java/org/elasticsearch/common/inject/internal/PrivateElementsImpl.java index 125c4d367af..6ab5454325a 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/internal/PrivateElementsImpl.java +++ b/core/src/main/java/org/elasticsearch/common/inject/internal/PrivateElementsImpl.java @@ -27,8 +27,6 @@ import org.elasticsearch.common.inject.spi.PrivateElements; import java.util.*; -import static org.elasticsearch.common.Preconditions.checkArgument; - /** * @author jessewilson@google.com (Jesse Wilson) */ @@ -132,7 +130,9 @@ public final class PrivateElementsImpl implements PrivateElements { public Object getExposedSource(Key key) { getExposedKeys(); // ensure exposedKeysToSources is populated Object source = exposedKeysToSources.get(key); - checkArgument(source != null, "%s not exposed by %s.", key, this); + if (source == null) { + throw new IllegalArgumentException(key + " not exposed by " + "."); + } return source; } diff --git a/core/src/main/java/org/elasticsearch/common/inject/matcher/Matchers.java b/core/src/main/java/org/elasticsearch/common/inject/matcher/Matchers.java index b502b28c61c..c29a48ddd91 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/matcher/Matchers.java +++ b/core/src/main/java/org/elasticsearch/common/inject/matcher/Matchers.java @@ -24,8 +24,6 @@ import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; import java.util.Objects; -import static org.elasticsearch.common.Preconditions.checkArgument; - /** * Matcher implementations. Supports matching classes and methods. * @@ -103,8 +101,9 @@ public class Matchers { private static void checkForRuntimeRetention( Class annotationType) { Retention retention = annotationType.getAnnotation(Retention.class); - checkArgument(retention != null && retention.value() == RetentionPolicy.RUNTIME, - "Annotation " + annotationType.getSimpleName() + " is missing RUNTIME retention"); + if (retention == null || retention.value() != RetentionPolicy.RUNTIME) { + throw new IllegalArgumentException("Annotation " + annotationType.getSimpleName() + " is missing RUNTIME retention"); + } } /** diff --git a/core/src/main/java/org/elasticsearch/common/inject/spi/Elements.java b/core/src/main/java/org/elasticsearch/common/inject/spi/Elements.java index c8786d375cc..afc85458df0 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/spi/Elements.java +++ b/core/src/main/java/org/elasticsearch/common/inject/spi/Elements.java @@ -28,8 +28,6 @@ import org.elasticsearch.common.logging.Loggers; import java.lang.annotation.Annotation; import java.util.*; -import static org.elasticsearch.common.Preconditions.checkArgument; - /** * Exposes elements of a module so they can be inspected, validated or {@link * Element#applyTo(Binder) rewritten}. @@ -132,7 +130,9 @@ public final class Elements { */ private RecordingBinder( RecordingBinder prototype, Object source, SourceProvider sourceProvider) { - checkArgument(source == null ^ sourceProvider == null); + if (!(source == null ^ sourceProvider == null)) { + throw new IllegalArgumentException(); + } this.stage = prototype.stage; this.modules = prototype.modules; diff --git a/core/src/main/java/org/elasticsearch/common/unit/Fuzziness.java b/core/src/main/java/org/elasticsearch/common/unit/Fuzziness.java index 60335ed49fb..3b4a0733ec5 100644 --- a/core/src/main/java/org/elasticsearch/common/unit/Fuzziness.java +++ b/core/src/main/java/org/elasticsearch/common/unit/Fuzziness.java @@ -19,7 +19,6 @@ package org.elasticsearch.common.unit; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; @@ -44,7 +43,9 @@ public final class Fuzziness implements ToXContent { private final String fuzziness; private Fuzziness(int fuzziness) { - Preconditions.checkArgument(fuzziness >= 0 && fuzziness <= 2, "Valid edit distances are [0, 1, 2] but was [" + fuzziness + "]"); + if (fuzziness != 0 && fuzziness != 1 && fuzziness != 2) { + throw new IllegalArgumentException("Valid edit distances are [0, 1, 2] but was [" + fuzziness + "]"); + } this.fuzziness = Integer.toString(fuzziness); } diff --git a/core/src/main/java/org/elasticsearch/common/unit/SizeValue.java b/core/src/main/java/org/elasticsearch/common/unit/SizeValue.java index bc7d913d9d8..fcbcff3c3d7 100644 --- a/core/src/main/java/org/elasticsearch/common/unit/SizeValue.java +++ b/core/src/main/java/org/elasticsearch/common/unit/SizeValue.java @@ -20,7 +20,6 @@ package org.elasticsearch.common.unit; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -46,7 +45,9 @@ public class SizeValue implements Streamable { } public SizeValue(long size, SizeUnit sizeUnit) { - Preconditions.checkArgument(size >= 0, "size in SizeValue may not be negative"); + if (size < 0) { + throw new IllegalArgumentException("size in SizeValue may not be negative"); + } this.size = size; this.sizeUnit = sizeUnit; } diff --git a/core/src/main/java/org/elasticsearch/common/util/AbstractBigArray.java b/core/src/main/java/org/elasticsearch/common/util/AbstractBigArray.java index 2328c8bc65e..29beee99d5a 100644 --- a/core/src/main/java/org/elasticsearch/common/util/AbstractBigArray.java +++ b/core/src/main/java/org/elasticsearch/common/util/AbstractBigArray.java @@ -22,7 +22,6 @@ package org.elasticsearch.common.util; import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.RamUsageEstimator; import org.elasticsearch.cache.recycler.PageCacheRecycler; -import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.recycler.Recycler; @@ -42,8 +41,12 @@ abstract class AbstractBigArray extends AbstractArray { protected AbstractBigArray(int pageSize, BigArrays bigArrays, boolean clearOnResize) { super(bigArrays, clearOnResize); this.recycler = bigArrays.recycler; - Preconditions.checkArgument(pageSize >= 128, "pageSize must be >= 128"); - Preconditions.checkArgument((pageSize & (pageSize - 1)) == 0, "pageSize must be a power of two"); + if (pageSize < 128) { + throw new IllegalArgumentException("pageSize must be >= 128"); + } + if ((pageSize & (pageSize - 1)) != 0) { + throw new IllegalArgumentException("pageSize must be a power of two"); + } this.pageShift = Integer.numberOfTrailingZeros(pageSize); this.pageMask = pageSize - 1; size = 0; @@ -56,7 +59,9 @@ abstract class AbstractBigArray extends AbstractArray { final int numPages(long capacity) { final long numPages = (capacity + pageMask) >>> pageShift; - Preconditions.checkArgument(numPages <= Integer.MAX_VALUE, "pageSize=" + (pageMask + 1) + " is too small for such as capacity: " + capacity); + if (numPages > Integer.MAX_VALUE) { + throw new IllegalArgumentException("pageSize=" + (pageMask + 1) + " is too small for such as capacity: " + capacity); + } return (int) numPages; } diff --git a/core/src/main/java/org/elasticsearch/common/util/AbstractPagedHashMap.java b/core/src/main/java/org/elasticsearch/common/util/AbstractPagedHashMap.java index 7b1298beab8..2368a5dde0a 100644 --- a/core/src/main/java/org/elasticsearch/common/util/AbstractPagedHashMap.java +++ b/core/src/main/java/org/elasticsearch/common/util/AbstractPagedHashMap.java @@ -20,7 +20,6 @@ package org.elasticsearch.common.util; import com.carrotsearch.hppc.BitMixer; -import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.lease.Releasable; /** @@ -48,8 +47,12 @@ abstract class AbstractPagedHashMap implements Releasable { long mask; AbstractPagedHashMap(long capacity, float maxLoadFactor, BigArrays bigArrays) { - Preconditions.checkArgument(capacity >= 0, "capacity must be >= 0"); - Preconditions.checkArgument(maxLoadFactor > 0 && maxLoadFactor < 1, "maxLoadFactor must be > 0 and < 1"); + if (capacity < 0) { + throw new IllegalArgumentException("capacity must be >= 0"); + } + if (maxLoadFactor <= 0 || maxLoadFactor >= 1) { + throw new IllegalArgumentException("maxLoadFactor must be > 0 and < 1"); + } this.bigArrays = bigArrays; this.maxLoadFactor = maxLoadFactor; long buckets = 1L + (long) (capacity / maxLoadFactor); diff --git a/core/src/main/java/org/elasticsearch/common/util/BigArrays.java b/core/src/main/java/org/elasticsearch/common/util/BigArrays.java index 49e338ef87b..0f0bced9cf2 100644 --- a/core/src/main/java/org/elasticsearch/common/util/BigArrays.java +++ b/core/src/main/java/org/elasticsearch/common/util/BigArrays.java @@ -24,7 +24,6 @@ import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.RamUsageEstimator; import org.elasticsearch.cache.recycler.PageCacheRecycler; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.breaker.CircuitBreaker; import org.elasticsearch.common.breaker.CircuitBreakingException; import org.elasticsearch.common.inject.Inject; @@ -55,9 +54,15 @@ public class BigArrays { /** Return the next size to grow to that is >= minTargetSize. * Inspired from {@link ArrayUtil#oversize(int, int)} and adapted to play nicely with paging. */ public static long overSize(long minTargetSize, int pageSize, int bytesPerElement) { - Preconditions.checkArgument(minTargetSize >= 0, "minTargetSize must be >= 0"); - Preconditions.checkArgument(pageSize >= 0, "pageSize must be > 0"); - Preconditions.checkArgument(bytesPerElement > 0, "bytesPerElement must be > 0"); + if (minTargetSize < 0) { + throw new IllegalArgumentException("minTargetSize must be >= 0"); + } + if (pageSize < 0) { + throw new IllegalArgumentException("pageSize must be > 0"); + } + if (bytesPerElement <= 0) { + throw new IllegalArgumentException("bytesPerElement must be > 0"); + } long newSize; if (minTargetSize < pageSize) { diff --git a/core/src/main/java/org/elasticsearch/common/util/BigByteArray.java b/core/src/main/java/org/elasticsearch/common/util/BigByteArray.java index 7b7c69de9e3..da4bc28408d 100644 --- a/core/src/main/java/org/elasticsearch/common/util/BigByteArray.java +++ b/core/src/main/java/org/elasticsearch/common/util/BigByteArray.java @@ -22,7 +22,6 @@ package org.elasticsearch.common.util; import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.Preconditions; import java.util.Arrays; @@ -110,7 +109,9 @@ final class BigByteArray extends AbstractBigArray implements ByteArray { @Override public void fill(long fromIndex, long toIndex, byte value) { - Preconditions.checkArgument(fromIndex <= toIndex); + if (fromIndex > toIndex) { + throw new IllegalArgumentException(); + } final int fromPage = pageIndex(fromIndex); final int toPage = pageIndex(toIndex - 1); if (fromPage == toPage) { diff --git a/core/src/main/java/org/elasticsearch/common/util/BigDoubleArray.java b/core/src/main/java/org/elasticsearch/common/util/BigDoubleArray.java index e12138deafc..1f739188377 100644 --- a/core/src/main/java/org/elasticsearch/common/util/BigDoubleArray.java +++ b/core/src/main/java/org/elasticsearch/common/util/BigDoubleArray.java @@ -21,7 +21,6 @@ package org.elasticsearch.common.util; import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.Preconditions; import java.util.Arrays; @@ -94,7 +93,9 @@ final class BigDoubleArray extends AbstractBigArray implements DoubleArray { @Override public void fill(long fromIndex, long toIndex, double value) { - Preconditions.checkArgument(fromIndex <= toIndex); + if (fromIndex > toIndex) { + throw new IllegalArgumentException(); + } final long longBits = Double.doubleToRawLongBits(value); final int fromPage = pageIndex(fromIndex); final int toPage = pageIndex(toIndex - 1); diff --git a/core/src/main/java/org/elasticsearch/common/util/BigFloatArray.java b/core/src/main/java/org/elasticsearch/common/util/BigFloatArray.java index a56ac31a149..f6fc2d8fce0 100644 --- a/core/src/main/java/org/elasticsearch/common/util/BigFloatArray.java +++ b/core/src/main/java/org/elasticsearch/common/util/BigFloatArray.java @@ -21,7 +21,6 @@ package org.elasticsearch.common.util; import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.Preconditions; import java.util.Arrays; @@ -94,7 +93,9 @@ final class BigFloatArray extends AbstractBigArray implements FloatArray { @Override public void fill(long fromIndex, long toIndex, float value) { - Preconditions.checkArgument(fromIndex <= toIndex); + if (fromIndex > toIndex) { + throw new IllegalArgumentException(); + } final int intBits = Float.floatToRawIntBits(value); final int fromPage = pageIndex(fromIndex); final int toPage = pageIndex(toIndex - 1); diff --git a/core/src/main/java/org/elasticsearch/common/util/BigIntArray.java b/core/src/main/java/org/elasticsearch/common/util/BigIntArray.java index 82bc451df29..1c0e9fe017c 100644 --- a/core/src/main/java/org/elasticsearch/common/util/BigIntArray.java +++ b/core/src/main/java/org/elasticsearch/common/util/BigIntArray.java @@ -21,7 +21,6 @@ package org.elasticsearch.common.util; import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.Preconditions; import java.util.Arrays; @@ -71,7 +70,9 @@ final class BigIntArray extends AbstractBigArray implements IntArray { @Override public void fill(long fromIndex, long toIndex, int value) { - Preconditions.checkArgument(fromIndex <= toIndex); + if (fromIndex > toIndex) { + throw new IllegalArgumentException(); + } final int fromPage = pageIndex(fromIndex); final int toPage = pageIndex(toIndex - 1); if (fromPage == toPage) { diff --git a/core/src/main/java/org/elasticsearch/common/util/BigLongArray.java b/core/src/main/java/org/elasticsearch/common/util/BigLongArray.java index 57750b3b2c9..fe0323ba67c 100644 --- a/core/src/main/java/org/elasticsearch/common/util/BigLongArray.java +++ b/core/src/main/java/org/elasticsearch/common/util/BigLongArray.java @@ -21,7 +21,6 @@ package org.elasticsearch.common.util; import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.Preconditions; import java.util.Arrays; @@ -93,7 +92,9 @@ final class BigLongArray extends AbstractBigArray implements LongArray { @Override public void fill(long fromIndex, long toIndex, long value) { - Preconditions.checkArgument(fromIndex <= toIndex); + if (fromIndex > toIndex) { + throw new IllegalArgumentException(); + } if (fromIndex == toIndex) { return; // empty range } diff --git a/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java b/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java index 6f3d701a050..5d520ffbbd4 100644 --- a/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java +++ b/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java @@ -25,7 +25,6 @@ import com.carrotsearch.hppc.LongArrayList; import com.carrotsearch.hppc.ObjectArrayList; import com.google.common.collect.Iterators; import org.apache.lucene.util.*; -import org.elasticsearch.common.Preconditions; import java.util.*; @@ -284,8 +283,12 @@ public enum CollectionUtils { private final int distance; public RotatedList(List list, int distance) { - Preconditions.checkArgument(distance >= 0 && distance < list.size()); - Preconditions.checkArgument(list instanceof RandomAccess); + if (distance < 0 || distance >= list.size()) { + throw new IllegalArgumentException(); + } + if (!(list instanceof RandomAccess)) { + throw new IllegalArgumentException(); + } this.in = list; this.distance = distance; } diff --git a/core/src/main/java/org/elasticsearch/gateway/MetaDataStateFormat.java b/core/src/main/java/org/elasticsearch/gateway/MetaDataStateFormat.java index 86a849b82cc..9ac1768522c 100644 --- a/core/src/main/java/org/elasticsearch/gateway/MetaDataStateFormat.java +++ b/core/src/main/java/org/elasticsearch/gateway/MetaDataStateFormat.java @@ -25,7 +25,6 @@ import org.apache.lucene.index.IndexFormatTooOldException; import org.apache.lucene.store.*; import org.apache.lucene.util.IOUtils; import org.elasticsearch.ExceptionsHelper; -import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.lucene.store.IndexOutputOutputStream; @@ -90,8 +89,12 @@ public abstract class MetaDataStateFormat { * @throws IOException if an IOException occurs */ public final void write(final T state, final long version, final Path... locations) throws IOException { - Preconditions.checkArgument(locations != null, "Locations must not be null"); - Preconditions.checkArgument(locations.length > 0, "One or more locations required"); + if (locations == null) { + throw new IllegalArgumentException("Locations must not be null"); + } + if (locations.length <= 0) { + throw new IllegalArgumentException("One or more locations required"); + } final long maxStateId = findMaxStateId(prefix, locations)+1; assert maxStateId >= 0 : "maxStateId must be positive but was: [" + maxStateId + "]"; final String fileName = prefix + maxStateId + STATE_FILE_EXTENSION; diff --git a/core/src/main/java/org/elasticsearch/index/fielddata/plain/BinaryDVNumericIndexFieldData.java b/core/src/main/java/org/elasticsearch/index/fielddata/plain/BinaryDVNumericIndexFieldData.java index e00f7acde8d..86966617fcc 100644 --- a/core/src/main/java/org/elasticsearch/index/fielddata/plain/BinaryDVNumericIndexFieldData.java +++ b/core/src/main/java/org/elasticsearch/index/fielddata/plain/BinaryDVNumericIndexFieldData.java @@ -27,7 +27,6 @@ import org.apache.lucene.store.ByteArrayDataInput; import org.apache.lucene.util.Accountable; import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.util.ByteUtils; import org.elasticsearch.index.Index; import org.elasticsearch.index.fielddata.AtomicNumericFieldData; @@ -51,7 +50,9 @@ public class BinaryDVNumericIndexFieldData extends DocValuesIndexFieldData imple public BinaryDVNumericIndexFieldData(Index index, Names fieldNames, NumericType numericType, FieldDataType fieldDataType) { super(index, fieldNames, fieldDataType); - Preconditions.checkArgument(numericType != null, "numericType must be non-null"); + if (numericType == null) { + throw new IllegalArgumentException("numericType must be non-null"); + } this.numericType = numericType; } diff --git a/core/src/main/java/org/elasticsearch/index/fielddata/plain/PackedArrayIndexFieldData.java b/core/src/main/java/org/elasticsearch/index/fielddata/plain/PackedArrayIndexFieldData.java index 42cba5bf6d8..1b54e386c0c 100644 --- a/core/src/main/java/org/elasticsearch/index/fielddata/plain/PackedArrayIndexFieldData.java +++ b/core/src/main/java/org/elasticsearch/index/fielddata/plain/PackedArrayIndexFieldData.java @@ -26,7 +26,6 @@ import org.apache.lucene.util.packed.PackedInts; import org.apache.lucene.util.packed.PackedLongValues; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.breaker.CircuitBreaker; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; @@ -73,7 +72,9 @@ public class PackedArrayIndexFieldData extends AbstractIndexFieldData visitedOrds; OrdinalsCollector(HyperLogLogPlusPlus counts, RandomAccessOrds values, BigArrays bigArrays) { - Preconditions.checkArgument(values.getValueCount() <= Integer.MAX_VALUE); + if (values.getValueCount() > Integer.MAX_VALUE) { + throw new IllegalArgumentException(); + } maxOrd = (int) values.getValueCount(); this.bigArrays = bigArrays; this.counts = counts; diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/HyperLogLogPlusPlus.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/HyperLogLogPlusPlus.java index 3293f2372e7..6b389182fc7 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/HyperLogLogPlusPlus.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/HyperLogLogPlusPlus.java @@ -23,7 +23,6 @@ import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.LongBitSet; import org.apache.lucene.util.RamUsageEstimator; import org.apache.lucene.util.packed.PackedInts; -import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lease.Releasable; @@ -160,8 +159,12 @@ public final class HyperLogLogPlusPlus implements Releasable { private final double alphaMM; public HyperLogLogPlusPlus(int precision, BigArrays bigArrays, long initialBucketCount) { - Preconditions.checkArgument(precision >= 4, "precision must be >= 4"); - Preconditions.checkArgument(precision <= 18, "precision must be <= 18"); + if (precision < 4) { + throw new IllegalArgumentException("precision must be >= 4"); + } + if (precision > 18) { + throw new IllegalArgumentException("precision must be <= 18"); + } p = precision; m = 1 << p; this.bigArrays = bigArrays; @@ -196,7 +199,9 @@ public final class HyperLogLogPlusPlus implements Releasable { } public void merge(long thisBucket, HyperLogLogPlusPlus other, long otherBucket) { - Preconditions.checkArgument(p == other.p); + if (p != other.p) { + throw new IllegalArgumentException(); + } ensureCapacity(thisBucket + 1); if (other.algorithm.get(otherBucket) == LINEAR_COUNTING) { final IntArray values = other.hashSet.values(otherBucket);