All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java8.lang.Integers Maven / Gradle / Ivy

Go to download

streamsupport is a backport of the Java 8 java.util.function (functional interfaces) and java.util.stream (streams) API for Java 6 / 7 and Android developers

There is a newer version: 1.7.4
Show newest version
/*
 * Copyright (c) 1994, 2013, 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 java8.lang;

/**
 * A place for static default implementations of the new Java 8
 * static methods in the {@link Integer} class. 
 */
public final class Integers {
    /**
     * Returns a hash code for a {@code int} value; compatible with
     * {@code Integer.hashCode()}.
     *
     * @param value the value to hash
     * @return a hash code value for a {@code int} value.
     * @since 1.8
     */
    public static int hashCode(int value) {
        return value;
    }

    /**
     * Converts the argument to a {@code long} by an unsigned
     * conversion.  In an unsigned conversion to a {@code long}, the
     * high-order 32 bits of the {@code long} are zero and the
     * low-order 32 bits are equal to the bits of the integer
     * argument.
     *
     * Consequently, zero and positive {@code int} values are mapped
     * to a numerically equal {@code long} value and negative {@code
     * int} values are mapped to a {@code long} value equal to the
     * input plus 232.
     *
     * @param  x the value to convert to an unsigned {@code long}
     * @return the argument converted to {@code long} by an unsigned
     *         conversion
     * @since 1.8
     */
    public static long toUnsignedLong(int x) {
        return ((long) x) & 0xffffffffL;
    }

    /**
     * Returns the unsigned quotient of dividing the first argument by
     * the second where each argument and the result is interpreted as
     * an unsigned value.
     *
     * 

Note that in two's complement arithmetic, the three other * basic arithmetic operations of add, subtract, and multiply are * bit-wise identical if the two operands are regarded as both * being signed or both being unsigned. Therefore separate {@code * addUnsigned}, etc. methods are not provided. * * @param dividend the value to be divided * @param divisor the value doing the dividing * @return the unsigned quotient of the first argument divided by * the second argument * @see #remainderUnsigned * @since 1.8 */ public static int divideUnsigned(int dividend, int divisor) { // In lieu of tricky code, for now just use long arithmetic. return (int) (toUnsignedLong(dividend) / toUnsignedLong(divisor)); } /** * Returns the unsigned remainder from dividing the first argument * by the second where each argument and the result is interpreted * as an unsigned value. * * @param dividend the value to be divided * @param divisor the value doing the dividing * @return the unsigned remainder of the first argument divided by * the second argument * @see #divideUnsigned * @since 1.8 */ public static int remainderUnsigned(int dividend, int divisor) { // In lieu of tricky code, for now just use long arithmetic. return (int) (toUnsignedLong(dividend) % toUnsignedLong(divisor)); } /** * Compares two {@code int} values numerically. * The value returned is identical to what would be returned by: *

     *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
     * 
* * @param x the first {@code int} to compare * @param y the second {@code int} to compare * @return the value {@code 0} if {@code x == y}; * a value less than {@code 0} if {@code x < y}; and * a value greater than {@code 0} if {@code x > y} * @since 1.7 */ public static int compare(int x, int y) { return (x < y) ? -1 : ((x == y) ? 0 : 1); } /** * Compares two {@code int} values numerically treating the values * as unsigned. * * @param x the first {@code int} to compare * @param y the second {@code int} to compare * @return the value {@code 0} if {@code x == y}; a value less * than {@code 0} if {@code x < y} as unsigned values; and * a value greater than {@code 0} if {@code x > y} as * unsigned values * @since 1.8 */ public static int compareUnsigned(int x, int y) { return compare(x + Integer.MIN_VALUE, y + Integer.MIN_VALUE); } /** * 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 java8.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 java8.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 java8.util.function.BinaryOperator * @since 1.8 */ public static int min(int a, int b) { return Math.min(a, b); } private Integers() { } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy