Merge pull request #163 from soc/topic/java-util-objects

Implement java.util.Objects
This commit is contained in:
Joshua Warner 2014-01-30 15:43:50 -08:00
commit 9f505f8522
2 changed files with 75 additions and 2 deletions

View File

@ -144,6 +144,7 @@ public class Arrays {
public static void sort(Object[] array) {
sort(array, new Comparator() {
@Override
public int compare(Object a, Object b) {
return ((Comparable) a).compareTo(b);
}
@ -306,14 +307,17 @@ public class Arrays {
public static <T> List<T> asList(final T ... array) {
return new AbstractList<T>() {
@Override
public int size() {
return array.length;
}
@Override
public void add(int index, T element) {
throw new UnsupportedOperationException();
}
@Override
public int indexOf(Object element) {
for (int i = 0; i < array.length; ++i) {
if (equal(element, array[i])) {
@ -323,6 +327,7 @@ public class Arrays {
return -1;
}
@Override
public int lastIndexOf(Object element) {
for (int i = array.length - 1; i >= 0; --i) {
if (equal(element, array[i])) {
@ -331,19 +336,23 @@ public class Arrays {
}
return -1;
}
@Override
public T get(int index) {
return array[index];
}
@Override
public T set(int index, T value) {
throw new UnsupportedOperationException();
}
@Override
public T remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public ListIterator<T> listIterator(int index) {
return new Collections.ArrayListIterator(this, index);
}

View File

@ -0,0 +1,64 @@
package java.util;
public final class Objects {
private Objects() {
throw new AssertionError("Instantiating java.long.Objetcs is not allowed!");
}
public static <T> int compare(final T x, final T y, final Comparator<? super T> comparator) {
if (x == y)
return 0;
else
return comparator.compare(x, y);
}
public static boolean deepEquals(final Object x, final Object y) {
if (x == y)
return true;
if (x == null || y == null)
return false;
throw new UnsupportedOperationException("deepEquals is not implemented yet.");
}
public static boolean equals(final Object x, final Object y) {
if (x == y)
return true;
if (x != null)
return x.equals(y);
return false;
}
public static int hash(final Object... values) {
return Arrays.hashCode(values);
}
public static int hashCode(final Object value) {
if (value == null)
return 0;
return value.hashCode();
}
public static <T> T requireNonNull(final T value) {
if (value == null)
throw new NullPointerException();
else
return value;
}
public static <T> T requireNonNull(final T value, String message) {
if (value == null)
throw new NullPointerException(message);
else
return value;
}
public static String toString(final Object value) {
return String.valueOf(value);
}
public static String toString(final Object value, final String defaultValue) {
if (value == null)
return defaultValue;
return value.toString();
}
}