--- old/src/share/classes/java/lang/Boolean.java 2012-12-05 13:03:56.987420813 -0800 +++ new/src/share/classes/java/lang/Boolean.java 2012-12-05 13:03:56.819420821 -0800 @@ -290,4 +290,49 @@ 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} parameters. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Boolean>}. + * + * @param a a boolean argument. + * @param b another boolean argument. + * @return the logical AND of {@code a} and {@code b}. + * @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} parameters. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Boolean>}. + * + * @param a a boolean argument. + * @param b another boolean argument. + * @return the logical OR of {@code a} and {@code b}. + * @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} parameters. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Boolean>}. + * + * @param a a boolean argument. + * @param b another boolean argument. + * @return the logical XOR of {@code a} and {@code b}. + * @since 1.8 + */ + public static boolean logicalXor(boolean a, boolean b) { + return a ^ b; + } } --- old/src/share/classes/java/lang/Byte.java 2012-12-05 13:03:57.767420775 -0800 +++ new/src/share/classes/java/lang/Byte.java 2012-12-05 13:03:57.599420783 -0800 @@ -515,6 +515,48 @@ */ public static final int BYTES = SIZE / Byte.SIZE; + /** + * Adds two {@code byte}s together as per the + operator. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Byte>}. + * + * @param a an argument. + * @param b another argument. + * @return the sum of {@code a} and {@code b}. + * @since 1.8 + */ + public static byte sum(byte a, byte b) { + return (byte) (a + b); + } + + /** + * Returns the greater of two {@code byte} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Byte>}. + * + * @param a an argument. + * @param b another argument. + * @return the larger of {@code a} and {@code b}. + * @since 1.8 + */ + public static byte max(byte a, byte b) { + return (a >= b) ? a : b; + } + + /** + * Returns the lesser of two {@code byte} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Byte>}. + * + * @param a an argument. + * @param b another argument. + * @return the lesser of {@code a} and {@code b}. + * @since 1.8 + */ + public static byte min(byte a, byte b) { + return (a <= b) ? a : b; + } + /** use serialVersionUID from JDK 1.1. for interoperability */ private static final long serialVersionUID = -7183698231559129828L; } --- old/src/share/classes/java/lang/Double.java 2012-12-05 13:03:58.543420738 -0800 +++ new/src/share/classes/java/lang/Double.java 2012-12-05 13:03:58.375420746 -0800 @@ -1021,6 +1021,48 @@ 1)); // (0.0, -0.0) or (NaN, !NaN) } + /** + * Adds two {@code double} values together as per the + operator. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Double>} or {@code DoubleBinaryOperator}. + * + * @param a an argument. + * @param b another argument. + * @return the sum of {@code a} and {@code b}. + * @since 1.8 + */ + public static double sum(double a, double b) { + return a + b; + } + + /** + * Returns the greater of two {@code double} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Double>} or {@code DoubleBinaryOperator}. + * + * @param a an argument. + * @param b another argument. + * @return the larger of {@code a} and {@code b}. + * @since 1.8 + */ + public static double max(double a, double b) { + return Math.max(a, b); + } + + /** + * Returns the lesser of two {@code double} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Double>} or {@code DoubleBinaryOperator}. + * + * @param a an argument. + * @param b another argument. + * @return the lesser of {@code a} and {@code b}. + * @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; } --- old/src/share/classes/java/lang/Float.java 2012-12-05 13:03:59.327420700 -0800 +++ new/src/share/classes/java/lang/Float.java 2012-12-05 13:03:59.163420708 -0800 @@ -926,6 +926,48 @@ 1)); // (0.0, -0.0) or (NaN, !NaN) } + /** + * Adds two {@code float} values together as per the + operator. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Float>}. + * + * @param a an argument. + * @param b another argument. + * @return the sum of {@code a} and {@code b}. + * @since 1.8 + */ + public static float sum(float a, float b) { + return a + b; + } + + /** + * Returns the greater of two {@code float} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Float>}. + * + * @param a an argument. + * @param b another argument. + * @return the larger of {@code a} and {@code b}. + * @since 1.8 + */ + public static float max(float a, float b) { + return Math.max(a, b); + } + + /** + * Returns the lesser of two {@code float} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Float>}. + * + * @param a an argument. + * @param b another argument. + * @return the lesser of {@code a} and {@code b}. + * @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; } --- old/src/share/classes/java/lang/Integer.java 2012-12-05 13:04:00.251420656 -0800 +++ new/src/share/classes/java/lang/Integer.java 2012-12-05 13:04:00.035420666 -0800 @@ -1512,6 +1512,48 @@ ((i << 24)); } + /** + * Adds two integers together as per the + operator. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Integer>} or {@code IntBinaryOperator}. + * + * @param a an argument. + * @param b another argument. + * @return the sum of {@code a} and {@code b}. + * @since 1.8 + */ + public static int sum(int a, int b) { + return a + b; + } + + /** + * Returns the greater of two {@code int} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Integer>} or {@code IntBinaryOperator}. + * + * @param a an argument. + * @param b another argument. + * @return the larger of {@code a} and {@code b}. + * @since 1.8 + */ + public static int max(int a, int b) { + return Math.max(a, b); + } + + /** + * Returns the lesser of two {@code int} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Integer>} or {@code IntBinaryOperator}. + * + * @param a an argument. + * @param b another argument. + * @return the lesser of {@code a} and {@code b}. + * @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 */ private static final long serialVersionUID = 1360826667806852920L; } --- old/src/share/classes/java/lang/Long.java 2012-12-05 13:04:01.267420607 -0800 +++ new/src/share/classes/java/lang/Long.java 2012-12-05 13:04:01.095420615 -0800 @@ -1539,6 +1539,48 @@ ((i >>> 16) & 0xffff0000L) | (i >>> 48); } + /** + * Adds two {@code long} values together as per the + operator. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Long>} or {@code LongBinaryOperator}. + * + * @param a an argument. + * @param b another argument. + * @return the sum of {@code a} and {@code b}. + * @since 1.8 + */ + public static long sum(long a, long b) { + return a + b; + } + + /** + * Returns the greater of two {@code long} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Long>} or {@code LongBinaryOperator}. + * + * @param a an argument. + * @param b another argument. + * @return the larger of {@code a} and {@code b}. + * @since 1.8 + */ + public static long max(long a, long b) { + return Math.max(a, b); + } + + /** + * Returns the lesser of two {@code long} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Long>} or {@code LongBinaryOperator}. + * + * @param a an argument. + * @param b another argument. + * @return the lesser of {@code a} and {@code b}. + * @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 */ private static final long serialVersionUID = 4290774380558885855L; } --- old/src/share/classes/java/lang/Short.java 2012-12-05 13:04:02.051420569 -0800 +++ new/src/share/classes/java/lang/Short.java 2012-12-05 13:04:01.883420577 -0800 @@ -531,6 +531,48 @@ return ((long) x) & 0xffffL; } + /** + * Adds two {@code short} values together as per the + operator. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Short>}. + * + * @param a an argument. + * @param b another argument. + * @return the sum of {@code a} and {@code b}. + * @since 1.8 + */ + public static short sum(short a, short b) { + return (short) (a + b); + } + + /** + * Returns the greater of two {@code short} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Short>}. + * + * @param a an argument. + * @param b another argument. + * @return the larger of {@code a} and {@code b}. + * @since 1.8 + */ + public static short max(short a, short b) { + return (a >= b) ? a : b; + } + + /** + * Returns the lesser of two {@code short} values. + * Suitable for conversion as a method reference to functional interfaces such + * as {@code BinaryOperator<Short>}. + * + * @param a an argument. + * @param b another argument. + * @return the lesser of {@code a} and {@code b}. + * @since 1.8 + */ + public static short min(short a, short b) { + return (a <= b) ? a : b; + } + /** use serialVersionUID from JDK 1.1. for interoperability */ private static final long serialVersionUID = 7515723908773894738L; } --- /dev/null 2012-12-05 10:40:46.603834579 -0800 +++ new/test/java/lang/PrimitiveSumMinMaxTest.java 2012-12-05 13:04:02.651420540 -0800 @@ -0,0 +1,196 @@ +/* + * 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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 and = Boolean::logicalAnd; + BinaryOperator or = Boolean::logicalOr; + BinaryOperator xor = Boolean::logicalXor; + Comparator 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 testByteMethods() { + BinaryOperator sum1 = Byte::sum; + BinaryOperator max1 = Byte::max; + BinaryOperator min1 = Byte::min; + Comparator cmp = Byte::compare; + + byte[] numbers = { -1, 0, 1, 100, Byte.MAX_VALUE, Byte.MIN_VALUE }; + for (byte i : numbers) { + for (byte j : numbers) { + assertEquals((byte) (i+j), (byte) sum1.operate(i, j)); + assertEquals(Math.max(i,j), (byte) max1.operate(i, j)); + assertEquals(Math.min(i,j), (byte) min1.operate(i, j)); + assertEquals(((Byte) i).compareTo(j), cmp.compare(i, j)); + } + } + } + + public void testShortMethods() { + BinaryOperator sum1 = Short::sum; + BinaryOperator max1 = Short::max; + BinaryOperator min1 = Short::min; + Comparator cmp = Short::compare; + + short[] numbers = { -1, 0, 1, 100, Short.MAX_VALUE, Short.MIN_VALUE }; + for (short i : numbers) { + for (short j : numbers) { + assertEquals((short) (i+j), (short) sum1.operate(i, j)); + assertEquals(Math.max(i,j), (short) max1.operate(i, j)); + assertEquals(Math.min(i,j), (short) min1.operate(i, j)); + assertEquals(((Short) i).compareTo(j), cmp.compare(i, j)); + } + } + } + + public void testIntMethods() { + BinaryOperator sum1 = Integer::sum; + IntBinaryOperator sum2 = Integer::sum; + BinaryOperator max1 = Integer::max; + IntBinaryOperator max2 = Integer::max; + BinaryOperator min1 = Integer::min; + IntBinaryOperator min2 = Integer::min; + Comparator 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 sum1 = Long::sum; + LongBinaryOperator sum2 = Long::sum; + BinaryOperator max1 = Long::max; + LongBinaryOperator max2 = Long::max; + BinaryOperator min1 = Long::min; + LongBinaryOperator min2 = Long::min; + Comparator 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 sum1 = Float::sum; + BinaryOperator max1 = Float::max; + BinaryOperator min1 = Float::min; + Comparator 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 sum1 = Double::sum; + DoubleBinaryOperator sum2 = Double::sum; + BinaryOperator max1 = Double::max; + DoubleBinaryOperator max2 = Double::max; + BinaryOperator min1 = Double::min; + DoubleBinaryOperator min2 = Double::min; + Comparator 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)); + } + } + } + +}