8004201: Add static utility methods to primitives to be used for redution operations.
authormduigou
Fri, 25 Jan 2013 16:13:39 -0800
changeset 15311 be0ff4a719bf
parent 15310 77bdd8c8467e
child 15312 4b57f9da8192
8004201: Add static utility methods to primitives to be used for redution operations. Reviewed-by: darcy, mduigou, briangoetz, dholmes Contributed-by: akhil.arora@oracle.com
jdk/src/share/classes/java/lang/Boolean.java
jdk/src/share/classes/java/lang/Double.java
jdk/src/share/classes/java/lang/Float.java
jdk/src/share/classes/java/lang/Integer.java
jdk/src/share/classes/java/lang/Long.java
jdk/test/java/lang/PrimitiveSumMinMaxTest.java
--- a/jdk/src/share/classes/java/lang/Boolean.java	Fri Jan 25 16:13:32 2013 -0800
+++ b/jdk/src/share/classes/java/lang/Boolean.java	Fri Jan 25 16:13:39 2013 -0800
@@ -290,4 +290,46 @@
     public static int compare(boolean x, boolean y) {
         return (x == y) ? 0 : (x ? 1 : -1);
     }
+
+    /**
+     * Returns the result of applying the logical AND operator to the
+     * specified {@code boolean} operands.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the logical AND of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static boolean logicalAnd(boolean a, boolean b) {
+        return a && b;
+    }
+
+    /**
+     * Returns the result of applying the logical OR operator to the
+     * specified {@code boolean} operands.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the logical OR of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static boolean logicalOr(boolean a, boolean b) {
+        return a || b;
+    }
+
+    /**
+     * Returns the result of applying the logical XOR operator to the
+     * specified {@code boolean} operands.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return  the logical XOR of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static boolean logicalXor(boolean a, boolean b) {
+        return a ^ b;
+    }
 }
--- a/jdk/src/share/classes/java/lang/Double.java	Fri Jan 25 16:13:32 2013 -0800
+++ b/jdk/src/share/classes/java/lang/Double.java	Fri Jan 25 16:13:39 2013 -0800
@@ -1021,6 +1021,48 @@
                  1));                          // (0.0, -0.0) or (NaN, !NaN)
     }
 
+    /**
+     * Adds two {@code double} values together as per the + operator.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the sum of {@code a} and {@code b}
+     * @jls 4.2.4 Floating-Point Operations
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static double sum(double a, double b) {
+        return a + b;
+    }
+
+    /**
+     * Returns the greater of two {@code double} values
+     * as if by calling {@link Math#max(double, double) Math.max}.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the greater of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static double max(double a, double b) {
+        return Math.max(a, b);
+    }
+
+    /**
+     * Returns the smaller of two {@code double} values
+     * as if by calling {@link Math#min(double, double) Math.min}.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the smaller of {@code a} and {@code b}.
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static double min(double a, double b) {
+        return Math.min(a, b);
+    }
+
     /** use serialVersionUID from JDK 1.0.2 for interoperability */
     private static final long serialVersionUID = -9172774392245257468L;
 }
--- a/jdk/src/share/classes/java/lang/Float.java	Fri Jan 25 16:13:32 2013 -0800
+++ b/jdk/src/share/classes/java/lang/Float.java	Fri Jan 25 16:13:39 2013 -0800
@@ -926,6 +926,48 @@
                  1));                          // (0.0, -0.0) or (NaN, !NaN)
     }
 
+    /**
+     * Adds two {@code float} values together as per the + operator.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the sum of {@code a} and {@code b}
+     * @jls 4.2.4 Floating-Point Operations
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static float sum(float a, float b) {
+        return a + b;
+    }
+
+    /**
+     * Returns the greater of two {@code float} values
+     * as if by calling {@link Math#max(float, float) Math.max}.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the greater of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static float max(float a, float b) {
+        return Math.max(a, b);
+    }
+
+    /**
+     * Returns the smaller of two {@code float} values
+     * as if by calling {@link Math#min(float, float) Math.min}.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the smaller of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static float min(float a, float b) {
+        return Math.min(a, b);
+    }
+
     /** use serialVersionUID from JDK 1.0.2 for interoperability */
     private static final long serialVersionUID = -2671257302660747028L;
 }
--- a/jdk/src/share/classes/java/lang/Integer.java	Fri Jan 25 16:13:32 2013 -0800
+++ b/jdk/src/share/classes/java/lang/Integer.java	Fri Jan 25 16:13:39 2013 -0800
@@ -1513,6 +1513,47 @@
                ((i << 24));
     }
 
+    /**
+     * Adds two integers together as per the + operator.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the sum of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static int sum(int a, int b) {
+        return a + b;
+    }
+
+    /**
+     * Returns the greater of two {@code int} values
+     * as if by calling {@link Math#max(int, int) Math.max}.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the greater of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static int max(int a, int b) {
+        return Math.max(a, b);
+    }
+
+    /**
+     * Returns the smaller of two {@code int} values
+     * as if by calling {@link Math#min(int, int) Math.min}.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the smaller of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static int min(int a, int b) {
+        return Math.min(a, b);
+    }
+
     /** use serialVersionUID from JDK 1.0.2 for interoperability */
     @Native private static final long serialVersionUID = 1360826667806852920L;
 }
--- a/jdk/src/share/classes/java/lang/Long.java	Fri Jan 25 16:13:32 2013 -0800
+++ b/jdk/src/share/classes/java/lang/Long.java	Fri Jan 25 16:13:39 2013 -0800
@@ -1540,6 +1540,47 @@
             ((i >>> 16) & 0xffff0000L) | (i >>> 48);
     }
 
+    /**
+     * Adds two {@code long} values together as per the + operator.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the sum of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static long sum(long a, long b) {
+        return a + b;
+    }
+
+    /**
+     * Returns the greater of two {@code long} values
+     * as if by calling {@link Math#max(long, long) Math.max}.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the greater of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static long max(long a, long b) {
+        return Math.max(a, b);
+    }
+
+    /**
+     * Returns the smaller of two {@code long} values
+     * as if by calling {@link Math#min(long, long) Math.min}.
+     *
+     * @param a the first operand
+     * @param b the second operand
+     * @return the smaller of {@code a} and {@code b}
+     * @see java.util.function.BinaryOperator
+     * @since 1.8
+     */
+    public static long min(long a, long b) {
+        return Math.min(a, b);
+    }
+
     /** use serialVersionUID from JDK 1.0.2 for interoperability */
     @Native private static final long serialVersionUID = 4290774380558885855L;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/PrimitiveSumMinMaxTest.java	Fri Jan 25 16:13:39 2013 -0800
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import org.testng.annotations.Test;
+
+import java.util.Comparator;
+import java.util.function.BinaryOperator;
+import java.util.function.IntBinaryOperator;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+import java.util.function.DoubleBinaryOperator;
+import java.util.function.LongBinaryOperator;
+
+/**
+ * @test
+ * @run testng PrimitiveSumMinMaxTest
+ * @summary test conversion of primitive wrapper sum, min, max, and compareTo methods to functional interfaces
+ *
+ * @author Brian Goetz
+ */
+@Test
+public class PrimitiveSumMinMaxTest {
+
+    public void testBooleanMethods() {
+        BinaryOperator<Boolean> and = Boolean::logicalAnd;
+        BinaryOperator<Boolean> or = Boolean::logicalOr;
+        BinaryOperator<Boolean> xor = Boolean::logicalXor;
+        Comparator<Boolean> cmp = Boolean::compare;
+
+        assertTrue(and.operate(true, true));
+        assertFalse(and.operate(true, false));
+        assertFalse(and.operate(false, true));
+        assertFalse(and.operate(false, false));
+
+        assertTrue(or.operate(true, true));
+        assertTrue(or.operate(true, false));
+        assertTrue(or.operate(false, true));
+        assertFalse(or.operate(false, false));
+
+        assertFalse(xor.operate(true, true));
+        assertTrue(xor.operate(true, false));
+        assertTrue(xor.operate(false, true));
+        assertFalse(xor.operate(false, false));
+
+        assertEquals(Boolean.TRUE.compareTo(Boolean.TRUE), cmp.compare(true, true));
+        assertEquals(Boolean.TRUE.compareTo(Boolean.FALSE), cmp.compare(true, false));
+        assertEquals(Boolean.FALSE.compareTo(Boolean.TRUE), cmp.compare(false, true));
+        assertEquals(Boolean.FALSE.compareTo(Boolean.FALSE), cmp.compare(false, false));
+    };
+
+    public void testIntMethods() {
+        BinaryOperator<Integer> sum1 = Integer::sum;
+        IntBinaryOperator sum2 = Integer::sum;
+        BinaryOperator<Integer> max1 = Integer::max;
+        IntBinaryOperator max2 = Integer::max;
+        BinaryOperator<Integer> min1 = Integer::min;
+        IntBinaryOperator min2 = Integer::min;
+        Comparator<Integer> cmp = Integer::compare;
+
+        int[] numbers = { -1, 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE };
+        for (int i : numbers) {
+            for (int j : numbers) {
+                assertEquals(i+j, (int) sum1.operate(i, j));
+                assertEquals(i+j, sum2.operateAsInt(i, j));
+                assertEquals(Math.max(i,j), (int) max1.operate(i, j));
+                assertEquals(Math.max(i,j), max2.operateAsInt(i, j));
+                assertEquals(Math.min(i,j), (int) min1.operate(i, j));
+                assertEquals(Math.min(i,j), min2.operateAsInt(i, j));
+                assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j));
+            }
+        }
+    }
+
+    public void testLongMethods() {
+        BinaryOperator<Long> sum1 = Long::sum;
+        LongBinaryOperator sum2 = Long::sum;
+        BinaryOperator<Long> max1 = Long::max;
+        LongBinaryOperator max2 = Long::max;
+        BinaryOperator<Long> min1 = Long::min;
+        LongBinaryOperator min2 = Long::min;
+        Comparator<Long> cmp = Long::compare;
+
+        long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE };
+        for (long i : numbers) {
+            for (long j : numbers) {
+                assertEquals(i+j, (long) sum1.operate(i, j));
+                assertEquals(i+j, sum2.operateAsLong(i, j));
+                assertEquals(Math.max(i,j), (long) max1.operate(i, j));
+                assertEquals(Math.max(i,j), max2.operateAsLong(i, j));
+                assertEquals(Math.min(i,j), (long) min1.operate(i, j));
+                assertEquals(Math.min(i,j), min2.operateAsLong(i, j));
+                assertEquals(((Long) i).compareTo(j), cmp.compare(i, j));
+            }
+        }
+    }
+
+    public void testFloatMethods() {
+        BinaryOperator<Float> sum1 = Float::sum;
+        BinaryOperator<Float> max1 = Float::max;
+        BinaryOperator<Float> min1 = Float::min;
+        Comparator<Float> cmp = Float::compare;
+
+        float[] numbers = { -1, 0, 1, 100, Float.MAX_VALUE, Float.MIN_VALUE };
+        for (float i : numbers) {
+            for (float j : numbers) {
+                assertEquals(i+j, (float) sum1.operate(i, j));
+                assertEquals(Math.max(i,j), (float) max1.operate(i, j));
+                assertEquals(Math.min(i,j), (float) min1.operate(i, j));
+                assertEquals(((Float) i).compareTo(j), cmp.compare(i, j));
+            }
+        }
+    }
+
+    public void testDoubleMethods() {
+        BinaryOperator<Double> sum1 = Double::sum;
+        DoubleBinaryOperator sum2 = Double::sum;
+        BinaryOperator<Double> max1 = Double::max;
+        DoubleBinaryOperator max2 = Double::max;
+        BinaryOperator<Double> min1 = Double::min;
+        DoubleBinaryOperator min2 = Double::min;
+        Comparator<Double> cmp = Double::compare;
+
+        double[] numbers = { -1, 0, 1, 100, Double.MAX_VALUE, Double.MIN_VALUE };
+        for (double i : numbers) {
+            for (double j : numbers) {
+                assertEquals(i+j, (double) sum1.operate(i, j));
+                assertEquals(i+j, sum2.operateAsDouble(i, j));
+                assertEquals(Math.max(i,j), (double) max1.operate(i, j));
+                assertEquals(Math.max(i,j), max2.operateAsDouble(i, j));
+                assertEquals(Math.min(i,j), (double) min1.operate(i, j));
+                assertEquals(Math.min(i,j), min2.operateAsDouble(i, j));
+                assertEquals(((Double) i).compareTo(j), cmp.compare(i, j));
+            }
+        }
+    }
+
+}