changeset 6416:1257aeafc7f5

Static methods on Integer suitable for use as method refs (Integer::sum, Integer::min, Integer::max)
author briangoetz
date Thu, 15 Nov 2012 17:54:00 -0500
parents ab258565c0c9
children dfed78f69c8b
files src/share/classes/java/lang/Integer.java test-ng/tests/org/openjdk/tests/java/lang/PrimitiveSumMinMaxTest.java
diffstat 2 files changed, 106 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/lang/Integer.java	Thu Nov 15 17:19:42 2012 -0500
+++ b/src/share/classes/java/lang/Integer.java	Thu Nov 15 17:54:00 2012 -0500
@@ -25,8 +25,6 @@
 
 package java.lang;
 
-import java.util.Properties;
-
 /**
  * The {@code Integer} class wraps a value of the primitive type
  * {@code int} in an object. An object of type {@code Integer}
@@ -1491,6 +1489,48 @@
                ((i << 24));
     }
 
+    /**
+     * Add two integers together as per the + operator.
+     * Suitable for conversion as a method reference to functional interfaces such
+     * as {@code BinaryOperator&lt;Integer&gt;} 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&lt;Integer&gt;} 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 (a >= b) ? a : b;
+    }
+
+    /**
+     * Returns the lesser of two {@code int} values.
+     * Suitable for conversion as a method reference to functional interfaces such
+     * as {@code BinaryOperator&lt;Integer&gt;} 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 (a <= b) ? a : b;
+    }
+
     /** use serialVersionUID from JDK 1.0.2 for interoperability */
     private static final long serialVersionUID = 1360826667806852920L;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-ng/tests/org/openjdk/tests/java/lang/PrimitiveSumMinMaxTest.java	Thu Nov 15 17:54:00 2012 -0500
@@ -0,0 +1,64 @@
+/*
+ * 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.
+ */
+package org.openjdk.tests.java.lang;
+
+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;
+
+/**
+ * PrimitiveSumMinMaxTest -- test conversion of primitive wrapper sum, min, max, and compareTo methods to functional interfaces
+ *
+ * @author Brian Goetz
+ */
+@Test
+public class PrimitiveSumMinMaxTest {
+    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.eval(i, j));
+                assertEquals(Math.max(i,j), (int) max1.operate(i, j));
+                assertEquals(Math.max(i,j), max2.eval(i, j));
+                assertEquals(Math.min(i,j), (int) min1.operate(i, j));
+                assertEquals(Math.min(i,j), min2.eval(i, j));
+                assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j));
+            }
+        }
+    }
+}