Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package java.util;
import static com.google.gwt.core.client.impl.Coercions.ensureInt;
import static com.google.gwt.core.shared.impl.InternalPreconditions.checkArgument;
import static com.google.gwt.core.shared.impl.InternalPreconditions.checkArraySize;
import static com.google.gwt.core.shared.impl.InternalPreconditions.checkElementIndex;
import static com.google.gwt.core.shared.impl.InternalPreconditions.checkPositionIndexes;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.UnsafeNativeLong;
import com.google.gwt.lang.Array;
import java.io.Serializable;
/**
* Utility methods related to native arrays. [Sun
* docs]
*/
public class Arrays {
private static final class ArrayList extends AbstractList implements
RandomAccess, Serializable {
/**
* The only reason this is non-final is so that E[] (and E) will be exposed
* for serialization.
*/
private E[] array;
ArrayList(E[] array) {
assert (array != null);
this.array = array;
}
@Override
public boolean contains(Object o) {
return (indexOf(o) != -1);
}
@Override
public E get(int index) {
checkElementIndex(index, size());
return array[index];
}
@Override
public E set(int index, E value) {
E was = get(index);
array[index] = value;
return was;
}
@Override
public int size() {
return array.length;
}
/*
* Semantics are to return an array of identical type.
*/
@Override
public Object[] toArray() {
return Array.clone(array);
}
/*
* Faster than the iterator-based implementation in AbstractCollection.
*/
@SuppressWarnings("unchecked")
@Override
public T[] toArray(T[] out) {
int size = size();
if (out.length < size) {
out = Array.createFrom(out, size);
}
for (int i = 0; i < size; ++i) {
out[i] = (T) array[i];
}
if (out.length > size) {
out[size] = null;
}
return out;
}
}
public static List asList(T... array) {
return new ArrayList(array);
}
/**
* Perform a binary search on a sorted byte array.
*
* @param sortedArray byte array to search
* @param key value to search for
* @return the index of an element with a matching value, or a negative number
* which is the index of the next larger value (or just past the end
* of the array if the searched value is larger than all elements in
* the array) minus 1 (to ensure error returns are negative)
*/
public static int binarySearch(final byte[] sortedArray, final byte key) {
int low = 0;
int high = sortedArray.length - 1;
while (low <= high) {
final int mid = low + ((high - low) >> 1);
final byte midVal = sortedArray[mid];
if (midVal < key) {
low = mid + 1;
} else if (midVal > key) {
high = mid - 1;
} else {
// key found
return mid;
}
}
// key not found.
return -low - 1;
}
/**
* Perform a binary search on a sorted char array.
*
* @param a char array to search
* @param key value to search for
* @return the index of an element with a matching value, or a negative number
* which is the index of the next larger value (or just past the end
* of the array if the searched value is larger than all elements in
* the array) minus 1 (to ensure error returns are negative)
*/
public static int binarySearch(final char[] a, final char key) {
int low = 0;
int high = a.length - 1;
while (low <= high) {
final int mid = low + ((high - low) >> 1);
final char midVal = a[mid];
if (midVal < key) {
low = mid + 1;
} else if (midVal > key) {
high = mid - 1;
} else {
// key found
return mid;
}
}
// key not found.
return -low - 1;
}
/**
* Perform a binary search on a sorted double array.
*
* @param sortedArray double array to search
* @param key value to search for
* @return the index of an element with a matching value, or a negative number
* which is the index of the next larger value (or just past the end
* of the array if the searched value is larger than all elements in
* the array) minus 1 (to ensure error returns are negative)
*/
public static int binarySearch(final double[] sortedArray, final double key) {
int low = 0;
int high = sortedArray.length - 1;
while (low <= high) {
final int mid = low + ((high - low) >> 1);
final double midVal = sortedArray[mid];
if (midVal < key) {
low = mid + 1;
} else if (midVal > key) {
high = mid - 1;
} else {
// key found
return mid;
}
}
// key not found.
return -low - 1;
}
/**
* Perform a binary search on a sorted float array.
*
* Note that some underlying JavaScript interpreters do not actually implement
* floats (using double instead), so you may get slightly different behavior
* regarding values that are very close (or equal) since conversion errors
* to/from double may change the values slightly.
*
* @param sortedArray float array to search
* @param key value to search for
* @return the index of an element with a matching value, or a negative number
* which is the index of the next larger value (or just past the end
* of the array if the searched value is larger than all elements in
* the array) minus 1 (to ensure error returns are negative)
*/
public static int binarySearch(final float[] sortedArray, final float key) {
int low = 0;
int high = sortedArray.length - 1;
while (low <= high) {
final int mid = low + ((high - low) >> 1);
final float midVal = sortedArray[mid];
if (midVal < key) {
low = mid + 1;
} else if (midVal > key) {
high = mid - 1;
} else {
// key found
return mid;
}
}
// key not found.
return -low - 1;
}
/**
* Perform a binary search on a sorted int array.
*
* @param sortedArray int array to search
* @param key value to search for
* @return the index of an element with a matching value, or a negative number
* which is the index of the next larger value (or just past the end
* of the array if the searched value is larger than all elements in
* the array) minus 1 (to ensure error returns are negative)
*/
public static int binarySearch(final int[] sortedArray, final int key) {
int low = 0;
int high = sortedArray.length - 1;
while (low <= high) {
final int mid = low + ((high - low) >> 1);
final int midVal = sortedArray[mid];
if (midVal < key) {
low = mid + 1;
} else if (midVal > key) {
high = mid - 1;
} else {
// key found
return mid;
}
}
// key not found.
return -low - 1;
}
/**
* Perform a binary search on a sorted long array.
*
* Note that most underlying JavaScript interpreters do not actually implement
* longs, so the values must be stored in doubles instead. This means that
* certain legal values cannot be represented, and comparison of two unequal
* long values may result in unexpected results if they are not also
* representable as doubles.
*
* @param sortedArray long array to search
* @param key value to search for
* @return the index of an element with a matching value, or a negative number
* which is the index of the next larger value (or just past the end
* of the array if the searched value is larger than all elements in
* the array) minus 1 (to ensure error returns are negative)
*/
public static int binarySearch(final long[] sortedArray, final long key) {
int low = 0;
int high = sortedArray.length - 1;
while (low <= high) {
final int mid = low + ((high - low) >> 1);
final long midVal = sortedArray[mid];
if (midVal < key) {
low = mid + 1;
} else if (midVal > key) {
high = mid - 1;
} else {
// key found
return mid;
}
}
// key not found.
return -low - 1;
}
/**
* Perform a binary search on a sorted object array, using natural ordering.
*
* @param sortedArray object array to search
* @param key value to search for
* @return the index of an element with a matching value, or a negative number
* which is the index of the next larger value (or just past the end
* of the array if the searched value is larger than all elements in
* the array) minus 1 (to ensure error returns are negative)
* @throws ClassCastException if key is not comparable to
* sortedArray's elements.
*/
public static int binarySearch(final Object[] sortedArray, final Object key) {
return binarySearch(sortedArray, key, Comparators.natural());
}
/**
* Perform a binary search on a sorted short array.
*
* @param sortedArray short array to search
* @param key value to search for
* @return the index of an element with a matching value, or a negative number
* which is the index of the next larger value (or just past the end
* of the array if the searched value is larger than all elements in
* the array) minus 1 (to ensure error returns are negative)
*/
public static int binarySearch(final short[] sortedArray, final short key) {
int low = 0;
int high = sortedArray.length - 1;
while (low <= high) {
final int mid = low + ((high - low) >> 1);
final short midVal = sortedArray[mid];
if (midVal < key) {
low = mid + 1;
} else if (midVal > key) {
high = mid - 1;
} else {
// key found
return mid;
}
}
// key not found.
return -low - 1;
}
/**
* Perform a binary search on a sorted object array, using a user-specified
* comparison function.
*
* @param sortedArray object array to search
* @param key value to search for
* @param comparator comparision function, null indicates
* natural ordering should be used.
* @return the index of an element with a matching value, or a negative number
* which is the index of the next larger value (or just past the end
* of the array if the searched value is larger than all elements in
* the array) minus 1 (to ensure error returns are negative)
* @throws ClassCastException if key and
* sortedArray's elements cannot be compared by
* comparator.
*/
public static int binarySearch(final T[] sortedArray, final T key,
Comparator super T> comparator) {
if (comparator == null) {
comparator = Comparators.natural();
}
int low = 0;
int high = sortedArray.length - 1;
while (low <= high) {
final int mid = low + ((high - low) >> 1);
final T midVal = sortedArray[mid];
final int compareResult = comparator.compare(midVal, key);
if (compareResult < 0) {
low = mid + 1;
} else if (compareResult > 0) {
high = mid - 1;
} else {
// key found
return mid;
}
}
// key not found.
return -low - 1;
}
public static boolean[] copyOf(boolean[] original, int newLength) {
checkArraySize(newLength);
return copyOfRange(original, 0, newLength);
}
public static byte[] copyOf(byte[] original, int newLength) {
checkArraySize(newLength);
return copyOfRange(original, 0, newLength);
}
public static char[] copyOf(char[] original, int newLength) {
checkArraySize(newLength);
return copyOfRange(original, 0, newLength);
}
public static double[] copyOf(double[] original, int newLength) {
checkArraySize(newLength);
return copyOfRange(original, 0, newLength);
}
public static float[] copyOf(float[] original, int newLength) {
checkArraySize(newLength);
return copyOfRange(original, 0, newLength);
}
public static int[] copyOf(int[] original, int newLength) {
checkArraySize(newLength);
return copyOfRange(original, 0, newLength);
}
public static long[] copyOf(long[] original, int newLength) {
checkArraySize(newLength);
return copyOfRange(original, 0, newLength);
}
public static short[] copyOf(short[] original, int newLength) {
checkArraySize(newLength);
return copyOfRange(original, 0, newLength);
}
public static T[] copyOf(T[] original, int newLength) {
checkArraySize(newLength);
return copyOfRange(original, 0, newLength);
}
public static boolean[] copyOfRange(boolean[] original, int from, int to) {
int newLength = getLengthFromRange(from, to);
boolean[] copy = new boolean[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
public static byte[] copyOfRange(byte[] original, int from, int to) {
int newLength = getLengthFromRange(from, to);
byte[] copy = new byte[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
public static char[] copyOfRange(char[] original, int from, int to) {
int newLength = getLengthFromRange(from, to);
char[] copy = new char[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
public static double[] copyOfRange(double[] original, int from, int to) {
int newLength = getLengthFromRange(from, to);
double[] copy = new double[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
public static float[] copyOfRange(float[] original, int from, int to) {
int newLength = getLengthFromRange(from, to);
float[] copy = new float[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
public static int[] copyOfRange(int[] original, int from, int to) {
int newLength = getLengthFromRange(from, to);
int[] copy = new int[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
public static long[] copyOfRange(long[] original, int from, int to) {
int newLength = getLengthFromRange(from, to);
long[] copy = new long[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
public static short[] copyOfRange(short[] original, int from, int to) {
int newLength = getLengthFromRange(from, to);
short[] copy = new short[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
public static T[] copyOfRange(T[] original, int from, int to) {
int newLength = getLengthFromRange(from, to);
T[] copy = Array.createFrom(original, newLength);
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
public static boolean deepEquals(Object[] a1, Object[] a2) {
if (a1 == a2) {
return true;
}
if (a1 == null || a2 == null) {
return false;
}
if (a1.length != a2.length) {
return false;
}
for (int i = 0, n = a1.length; i < n; ++i) {
if (!Objects.deepEquals(a1[i], a2[i])) {
return false;
}
}
return true;
}
public static int deepHashCode(Object[] a) {
if (a == null) {
return 0;
}
int hashCode = 1;
for (Object obj : a) {
int hash;
if (obj instanceof Object[]) {
hash = deepHashCode((Object[]) obj);
} else if (obj instanceof boolean[]) {
hash = hashCode((boolean[]) obj);
} else if (obj instanceof byte[]) {
hash = hashCode((byte[]) obj);
} else if (obj instanceof char[]) {
hash = hashCode((char[]) obj);
} else if (obj instanceof short[]) {
hash = hashCode((short[]) obj);
} else if (obj instanceof int[]) {
hash = hashCode((int[]) obj);
} else if (obj instanceof long[]) {
hash = hashCode((long[]) obj);
} else if (obj instanceof float[]) {
hash = hashCode((float[]) obj);
} else if (obj instanceof double[]) {
hash = hashCode((double[]) obj);
} else {
hash = Objects.hashCode(obj);
}
hashCode = 31 * hashCode + hash;
hashCode = ensureInt(hashCode); // make sure we don't overflow
}
return hashCode;
}
public static String deepToString(Object[] a) {
return deepToString(a, new HashSet