Simplify Array sort comparators.
Change-Id: I418b58d9767b0e84527bf17021943deb81f8f7a2
Review-Link: https://gwt-review.googlesource.com/#/c/18101/
diff --git a/user/super/com/google/gwt/emul/java/util/Arrays.java b/user/super/com/google/gwt/emul/java/util/Arrays.java
index cc08e51..a9dd2a4 100644
--- a/user/super/com/google/gwt/emul/java/util/Arrays.java
+++ b/user/super/com/google/gwt/emul/java/util/Arrays.java
@@ -41,8 +41,7 @@
import java.util.stream.StreamSupport;
import javaemul.internal.ArrayHelper;
-import javaemul.internal.DoubleCompareHolder;
-import javaemul.internal.LongCompareHolder;
+import jsinterop.annotations.JsFunction;
/**
* Utility methods related to native arrays.
@@ -1280,21 +1279,21 @@
}
public static void sort(double[] array) {
- nativeSort(array, DoubleCompareHolder.getDoubleComparator());
+ nativeSort(array, getDoubleComparator());
}
public static void sort(double[] array, int fromIndex, int toIndex) {
checkCriticalArrayBounds(fromIndex, toIndex, array.length);
- nativeSort(array, fromIndex, toIndex, DoubleCompareHolder.getDoubleComparator());
+ nativeSort(array, fromIndex, toIndex, getDoubleComparator());
}
public static void sort(float[] array) {
- nativeSort(array, DoubleCompareHolder.getDoubleComparator());
+ nativeSort(array, getDoubleComparator());
}
public static void sort(float[] array, int fromIndex, int toIndex) {
checkCriticalArrayBounds(fromIndex, toIndex, array.length);
- nativeSort(array, fromIndex, toIndex, DoubleCompareHolder.getDoubleComparator());
+ nativeSort(array, fromIndex, toIndex, getDoubleComparator());
}
public static void sort(int[] array) {
@@ -1307,12 +1306,12 @@
}
public static void sort(long[] array) {
- nativeSort(array, LongCompareHolder.getLongComparator());
+ nativeSort(array, getLongComparator());
}
public static void sort(long[] array, int fromIndex, int toIndex) {
checkCriticalArrayBounds(fromIndex, toIndex, array.length);
- nativeSort(array, fromIndex, toIndex, LongCompareHolder.getLongComparator());
+ nativeSort(array, fromIndex, toIndex, getLongComparator());
}
public static void sort(Object[] array) {
@@ -1753,19 +1752,42 @@
/**
* Sort an entire array of number primitives of integral type.
*/
- private static native void nativeIntegerSort(Object array) /*-{
- array.sort(function(a, b) {
- return a - b;
- });
- }-*/;
+ private static void nativeIntegerSort(Object array) {
+ nativeSort(array, getIntComparator());
+ }
/**
* Sort a subset of an array of primitives of integral type.
*/
private static void nativeIntegerSort(Object array, int fromIndex, int toIndex) {
- Object temp = ArrayHelper.unsafeClone(array, fromIndex, toIndex);
- nativeIntegerSort(temp);
- ArrayHelper.copy(temp, 0, array, fromIndex, toIndex - fromIndex);
+ nativeSort(array, fromIndex, toIndex, getIntComparator());
+ }
+
+ @JsFunction
+ private interface CompareDoubleFunction {
+ int compare(double d1, double d2);
+ }
+
+ private static CompareDoubleFunction getDoubleComparator() {
+ return Double::compare;
+ }
+
+ @JsFunction
+ private interface CompareLongFunction {
+ int compare(long d1, long d2);
+ }
+
+ private static CompareLongFunction getLongComparator() {
+ return Long::compare;
+ }
+
+ @JsFunction
+ private interface CompareIntFunction {
+ int compare(int d1, int d2);
+ }
+
+ private static CompareIntFunction getIntComparator() {
+ return (a, b) -> a - b;
}
private Arrays() { }
diff --git a/user/super/com/google/gwt/emul/javaemul/internal/DoubleCompareHolder.java b/user/super/com/google/gwt/emul/javaemul/internal/DoubleCompareHolder.java
deleted file mode 100644
index 1fe9d58..0000000
--- a/user/super/com/google/gwt/emul/javaemul/internal/DoubleCompareHolder.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright 2017 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 javaemul.internal;
-
-/**
- * A helper class for double comparison.
- */
-public class DoubleCompareHolder {
- // TODO(dankurka): replace this with @JsFunction once its available in GWT.
- public static native Object getDoubleComparator() /*-{
- return @java.lang.Double::compare(*);
- }-*/;
-}
-
diff --git a/user/super/com/google/gwt/emul/javaemul/internal/LongCompareHolder.java b/user/super/com/google/gwt/emul/javaemul/internal/LongCompareHolder.java
deleted file mode 100644
index 5a75c7c..0000000
--- a/user/super/com/google/gwt/emul/javaemul/internal/LongCompareHolder.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright 2015 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 javaemul.internal;
-
-/**
- * A helper class for long comparison.
- */
-public class LongCompareHolder {
- // TODO(dankurka): replace this with @JsFunction once its available in GWT.
- public static native Object getLongComparator() /*-{
- return @com.google.gwt.lang.LongLib::compare(*);
- }-*/;
-}
-