com.google.gwt.core.shared.impl.InternalPreconditions Maven / Gradle / Ivy
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.core.shared.impl;
import java.util.NoSuchElementException;
/**
* A utility class that provides utility functions to do precondition checks inside GWT-SDK.
*/
public class InternalPreconditions {
// Some parts adapted from Guava
/**
* Ensures the truth of an expression that verifies type.
*/
public static void checkType(boolean expression) {
if (!expression) {
throw new ClassCastException();
}
}
/**
* Ensures the truth of an expression that verifies array type.
*/
public static void checkArrayType(boolean expression) {
if (!expression) {
throw new ArrayStoreException();
}
}
/**
* Ensures the truth of an expression that verifies array type.
*/
public static void checkArrayType(boolean expression, Object errorMessage) {
if (!expression) {
throw new ArrayStoreException(String.valueOf(errorMessage));
}
}
/**
* Ensures the truth of an expression involving existence of an element.
*/
public static void checkElement(boolean expression) {
checkCriticalElement(expression);
}
/**
* Ensures the truth of an expression involving existence of an element.
*/
public static void checkElement(boolean expression, Object errorMessage) {
if (!expression) {
throw new NoSuchElementException(String.valueOf(errorMessage));
}
}
/**
* Ensures the truth of an expression involving existence of an element.
*
* For cases where failing fast is pretty important and not failing early could cause bugs that
* are much harder to debug.
*/
public static void checkCriticalElement(boolean expression) {
if (!expression) {
throw new NoSuchElementException();
}
}
/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*/
public static void checkArgument(boolean expression) {
checkCriticalArgument(expression);
}
/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*/
public static void checkArgument(boolean expression, Object errorMessage) {
checkCriticalArgument(expression, errorMessage);
}
/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*/
public static void checkArgument(boolean expression, String errorMessageTemplate,
Object... errorMessageArgs) {
checkCriticalArgument(expression, errorMessageTemplate, errorMessageArgs);
}
/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* For cases where failing fast is pretty important and not failing early could cause bugs that
* are much harder to debug.
*/
public static void checkCriticalArgument(boolean expression) {
if (!expression) {
throw new IllegalArgumentException();
}
}
/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* For cases where failing fast is pretty important and not failing early could cause bugs that
* are much harder to debug.
*/
public static void checkCriticalArgument(boolean expression, Object errorMessage) {
if (!expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}
/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* For cases where failing fast is pretty important and not failing early could cause bugs that
* are much harder to debug.
*/
public static void checkCriticalArgument(boolean expression, String errorMessageTemplate,
Object... errorMessageArgs) {
if (!expression) {
throw new IllegalArgumentException(format(errorMessageTemplate, errorMessageArgs));
}
}
/**
* Ensures the truth of an expression involving the state of the calling instance, but not
* involving any parameters to the calling method.
*
* @param expression a boolean expression
* @throws IllegalStateException if {@code expression} is false
*/
public static void checkState(boolean expression) {
if (!expression) {
throw new IllegalStateException();
}
}
/**
* Ensures the truth of an expression involving the state of the calling instance, but not
* involving any parameters to the calling method.
*/
public static void checkState(boolean expression, Object errorMessage) {
if (!expression) {
throw new IllegalStateException(String.valueOf(errorMessage));
}
}
/**
* Ensures that an object reference passed as a parameter to the calling method is not null.
*/
public static T checkNotNull(T reference) {
if (reference == null) {
throw new NullPointerException();
}
return reference;
}
/**
* Ensures that an object reference passed as a parameter to the calling method is not null.
*/
public static void checkNotNull(Object reference, Object errorMessage) {
if (reference == null) {
throw new NullPointerException(String.valueOf(errorMessage));
}
}
/**
* Ensures that {@code size} specifies a valid array size (i.e. non-negative).
*/
public static void checkArraySize(int size) {
if (size < 0) {
throw new NegativeArraySizeException("Negative array size: " + size);
}
}
/**
* Ensures that {@code index} specifies a valid element in an array, list or string of size
* {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive.
*/
public static void checkElementIndex(int index, int size) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
}
/**
* Ensures that {@code index} specifies a valid position in an array, list or string of
* size {@code size}. A position index may range from zero to {@code size}, inclusive.
*/
public static void checkPositionIndex(int index, int size) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
}
/**
* Ensures that {@code start} and {@code end} specify a valid positions in an array, list
* or string of size {@code size}, and are in order. A position index may range from zero to
* {@code size}, inclusive.
*/
public static void checkPositionIndexes(int start, int end, int size) {
checkCriticalPositionIndexes(start, end, size);
}
/**
* Ensures that {@code start} and {@code end} specify a valid positions in an array, list
* or string of size {@code size}, and are in order. A position index may range from zero to
* {@code size}, inclusive.
*/
public static void checkCriticalPositionIndexes(int start, int end, int size) {
if (start < 0) {
throw new IndexOutOfBoundsException("fromIndex: " + start + " < 0");
}
if (end > size) {
throw new IndexOutOfBoundsException("toIndex: " + end + " > size " + size);
}
if (start > end) {
throw new IllegalArgumentException("fromIndex: " + start + " > toIndex: " + end);
}
}
/**
* Substitutes each {@code %s} in {@code template} with an argument. These are matched by
* position: the first {@code %s} gets {@code args[0]}, etc. If there are more arguments than
* placeholders, the unmatched arguments will be appended to the end of the formatted message in
* square braces.
*/
private static String format(String template, Object... args) {
template = String.valueOf(template); // null -> "null"
// start substituting the arguments into the '%s' placeholders
StringBuilder builder = new StringBuilder(template.length() + 16 * args.length);
int templateStart = 0;
int i = 0;
while (i < args.length) {
int placeholderStart = template.indexOf("%s", templateStart);
if (placeholderStart == -1) {
break;
}
builder.append(template.substring(templateStart, placeholderStart));
builder.append(args[i++]);
templateStart = placeholderStart + 2;
}
builder.append(template.substring(templateStart));
// if we run out of placeholders, append the extra args in square braces
if (i < args.length) {
builder.append(" [");
builder.append(args[i++]);
while (i < args.length) {
builder.append(", ");
builder.append(args[i++]);
}
builder.append(']');
}
return builder.toString();
}
// Hides the constructor for this static utility class.
private InternalPreconditions() { }
}