Completely remove Preconditions.checkArgument

This commit is contained in:
Jason Tedor 2015-09-13 20:55:03 -04:00
parent 78bff93ab1
commit acbede48d7
30 changed files with 175 additions and 159 deletions

View File

@ -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<IndexMetaData>, FromXContentBuild
private final boolean useTypeForRouting;
private IndexMetaData(String index, long version, State state, Settings settings, ImmutableOpenMap<String, MappingMetaData> mappings, ImmutableOpenMap<String, AliasMetaData> aliases, ImmutableOpenMap<String, Custom> 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;

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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<K, V> extends AbstractMap<K, V> {
@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<K, V> extends AbstractMap<K, V> {
* of the hash table. The current hash table is not modified.
*/
public CopyOnWriteHashMap<K, V> 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<K, V> newRoot = root.put(key, hash, TOTAL_HASH_BITS, value, newValue);
@ -522,7 +527,9 @@ public final class CopyOnWriteHashMap<K, V> extends AbstractMap<K, V> {
* Remove the given key from this map. The current hash table is not modified.
*/
public CopyOnWriteHashMap<K, V> 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<K, V> newRoot = root.remove(key, hash);
if (root == newRoot) {

View File

@ -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<Message> messages) {
this.messages = ImmutableSet.copyOf(messages);
checkArgument(!this.messages.isEmpty());
if (this.messages.isEmpty()) {
throw new IllegalArgumentException();
}
initCause(Errors.getOnlyCause(this.messages));
}

View File

@ -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<T> {
private static void ensureRetainedAtRuntime(
Class<? extends Annotation> 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<? extends Annotation> 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 {

View File

@ -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<Message> messages) {
this.messages = ImmutableSet.copyOf(messages);
checkArgument(!this.messages.isEmpty());
if (this.messages.isEmpty()) {
throw new IllegalArgumentException();
}
initCause(Errors.getOnlyCause(this.messages));
}

View File

@ -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<T> {
* @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<T> {
* @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<T> {
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<T> {
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<T> {
* @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());
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}
/**

View File

@ -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;
}

View File

@ -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<? extends Annotation> 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");
}
}
/**

View File

@ -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;

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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 &gt;= <code>minTargetSize</code>.
* 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) {

View File

@ -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) {

View File

@ -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);

View File

@ -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);

View File

@ -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) {

View File

@ -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
}

View File

@ -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<T> 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;
}

View File

@ -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<T> {
* @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;

View File

@ -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;
}

View File

@ -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<AtomicNume
CircuitBreakerService breakerService) {
super(index, indexSettings, fieldNames, fieldDataType, cache);
Objects.requireNonNull(numericType);
Preconditions.checkArgument(EnumSet.of(NumericType.BOOLEAN, NumericType.BYTE, NumericType.SHORT, NumericType.INT, NumericType.LONG).contains(numericType), getClass().getSimpleName() + " only supports integer types, not " + numericType);
if (!EnumSet.of(NumericType.BOOLEAN, NumericType.BYTE, NumericType.SHORT, NumericType.INT, NumericType.LONG).contains(numericType)) {
throw new IllegalArgumentException(getClass().getSimpleName() + " only supports integer types, not " + numericType);
}
this.numericType = numericType;
this.breakerService = breakerService;
}

View File

@ -22,7 +22,6 @@ package org.elasticsearch.index.fielddata.plain;
import org.apache.lucene.index.*;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.Preconditions;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.fielddata.*;
import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested;
@ -45,7 +44,9 @@ public class SortedNumericDVIndexFieldData extends DocValuesIndexFieldData imple
public SortedNumericDVIndexFieldData(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;
}

View File

@ -28,7 +28,6 @@ import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.RamUsageEstimator;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Preconditions;
import org.elasticsearch.common.lease.Releasable;
import org.elasticsearch.common.lease.Releasables;
import org.elasticsearch.common.util.BigArrays;
@ -233,7 +232,9 @@ public class CardinalityAggregator extends NumericMetricsAggregator.SingleValue
private ObjectArray<FixedBitSet> 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;

View File

@ -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);