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

com.landawn.abacus.util.Primitives Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 2.1.12
Show newest version
/*
 * Copyright (c) 2018, Haiyang Li.
 * 
 * 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 com.landawn.abacus.util;

/**
 * The Class Primitives.
 */
public final class Primitives {

    /** The Constant PRIMITIVE_2_WRAPPER. */
    // ...
    static final BiMap, Class> PRIMITIVE_2_WRAPPER = new BiMap<>();

    static {
        PRIMITIVE_2_WRAPPER.put(boolean.class, Boolean.class);
        PRIMITIVE_2_WRAPPER.put(char.class, Character.class);
        PRIMITIVE_2_WRAPPER.put(byte.class, Byte.class);
        PRIMITIVE_2_WRAPPER.put(short.class, Short.class);
        PRIMITIVE_2_WRAPPER.put(int.class, Integer.class);
        PRIMITIVE_2_WRAPPER.put(long.class, Long.class);
        PRIMITIVE_2_WRAPPER.put(float.class, Float.class);
        PRIMITIVE_2_WRAPPER.put(double.class, Double.class);

        PRIMITIVE_2_WRAPPER.put(boolean[].class, Boolean[].class);
        PRIMITIVE_2_WRAPPER.put(char[].class, Character[].class);
        PRIMITIVE_2_WRAPPER.put(byte[].class, Byte[].class);
        PRIMITIVE_2_WRAPPER.put(short[].class, Short[].class);
        PRIMITIVE_2_WRAPPER.put(int[].class, Integer[].class);
        PRIMITIVE_2_WRAPPER.put(long[].class, Long[].class);
        PRIMITIVE_2_WRAPPER.put(float[].class, Float[].class);
        PRIMITIVE_2_WRAPPER.put(double[].class, Double[].class);
    }

    /**
     * Instantiates a new primitives.
     */
    private Primitives() {
        // utility class.
    }

    /**
     * Checks if is primitive type.
     *
     * @param cls
     * @return true, if is primitive type
     */
    public static boolean isPrimitiveType(final Class cls) {
        N.checkArgNotNull(cls, "cls");

        return N.typeOf(cls).isPrimitiveType();
    }

    /**
     * Checks if is wrapper type.
     *
     * @param cls
     * @return true, if is wrapper type
     */
    public static boolean isWrapperType(final Class cls) {
        N.checkArgNotNull(cls, "cls");

        return N.typeOf(cls).isPrimitiveWrapper();
    }

    /**
     * Checks if is primitive array type.
     *
     * @param cls
     * @return true, if is primitive array type
     */
    public static boolean isPrimitiveArrayType(final Class cls) {
        N.checkArgNotNull(cls, "cls");

        return N.typeOf(cls).isPrimitiveArray();
    }

    /**
     * Returns the corresponding wrapper type of {@code type} if it is a primitive type; otherwise
     * returns {@code type} itself. Idempotent.
     * 
     * 
     *     wrap(int.class) == Integer.class
     *     wrap(Integer.class) == Integer.class
     *     wrap(String.class) == String.class
     * 
* * @param cls * @return */ public static Class wrap(final Class cls) { N.checkArgNotNull(cls, "cls"); final Class wrapped = PRIMITIVE_2_WRAPPER.get(cls); return wrapped == null ? cls : wrapped; } /** * Returns the corresponding primitive type of {@code type} if it is a wrapper type; otherwise * returns {@code type} itself. Idempotent. * *
     *     unwrap(Integer.class) == int.class
     *     unwrap(int.class) == int.class
     *     unwrap(String.class) == String.class
     * 
* * @param cls * @return */ public static Class unwrap(final Class cls) { N.checkArgNotNull(cls, "cls"); Class unwrapped = PRIMITIVE_2_WRAPPER.getByValue(cls); return unwrapped == null ? cls : unwrapped; } /** *

* Converts an array of primitive booleans to objects. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code boolean} array * @return a {@code Boolean} array, {@code null} if null array input */ @SafeVarargs public static Boolean[] box(final boolean... a) { if (a == null) { return null; } return box(a, 0, a.length); } /** * * @param a * @param fromIndex * @param toIndex * @return */ public static Boolean[] box(final boolean[] a, final int fromIndex, final int toIndex) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_BOOLEAN_OBJECT_ARRAY; } final Boolean[] result = new Boolean[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = Boolean.valueOf(a[j]); } return result; } /** *

* Converts an array of primitive chars to objects. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code char} array * @return a {@code Character} array, {@code null} if null array input */ @SafeVarargs public static Character[] box(final char... a) { if (a == null) { return null; } return box(a, 0, a.length); } /** * * @param a * @param fromIndex * @param toIndex * @return */ public static Character[] box(final char[] a, final int fromIndex, final int toIndex) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_CHARACTER_OBJECT_ARRAY; } final Character[] result = new Character[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = Character.valueOf(a[j]); } return result; } /** *

* Converts an array of primitive bytes to objects. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code byte} array * @return a {@code Byte} array, {@code null} if null array input */ @SafeVarargs public static Byte[] box(final byte... a) { if (a == null) { return null; } return box(a, 0, a.length); } /** * * @param a * @param fromIndex * @param toIndex * @return */ public static Byte[] box(final byte[] a, final int fromIndex, final int toIndex) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_BYTE_OBJECT_ARRAY; } final Byte[] result = new Byte[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = Byte.valueOf(a[j]); } return result; } /** *

* Converts an array of primitive shorts to objects. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code short} array * @return a {@code Short} array, {@code null} if null array input */ @SafeVarargs public static Short[] box(final short... a) { if (a == null) { return null; } return box(a, 0, a.length); } /** * * @param a * @param fromIndex * @param toIndex * @return */ public static Short[] box(final short[] a, final int fromIndex, final int toIndex) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_SHORT_OBJECT_ARRAY; } final Short[] result = new Short[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = Short.valueOf(a[j]); } return result; } /** *

* Converts an array of primitive ints to objects. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * an {@code int} array * @return an {@code Integer} array, {@code null} if null array input */ @SafeVarargs public static Integer[] box(final int... a) { if (a == null) { return null; } return box(a, 0, a.length); } /** * * @param a * @param fromIndex * @param toIndex * @return */ public static Integer[] box(final int[] a, final int fromIndex, final int toIndex) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_INTEGER_OBJECT_ARRAY; } final Integer[] result = new Integer[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = Integer.valueOf(a[j]); } return result; } /** *

* Converts an array of primitive longs to objects. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code long} array * @return a {@code Long} array, {@code null} if null array input */ @SafeVarargs public static Long[] box(final long... a) { if (a == null) { return null; } return box(a, 0, a.length); } /** * * @param a * @param fromIndex * @param toIndex * @return */ public static Long[] box(final long[] a, final int fromIndex, final int toIndex) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_LONG_OBJECT_ARRAY; } final Long[] result = new Long[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = Long.valueOf(a[j]); } return result; } /** *

* Converts an array of primitive floats to objects. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code float} array * @return a {@code Float} array, {@code null} if null array input */ @SafeVarargs public static Float[] box(final float... a) { if (a == null) { return null; } return box(a, 0, a.length); } /** * * @param a * @param fromIndex * @param toIndex * @return */ public static Float[] box(final float[] a, final int fromIndex, final int toIndex) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_FLOAT_OBJECT_ARRAY; } final Float[] result = new Float[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = Float.valueOf(a[j]); } return result; } /** *

* Converts an array of primitive doubles to objects. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code double} array * @return a {@code Double} array, {@code null} if null array input */ @SafeVarargs public static Double[] box(final double... a) { if (a == null) { return null; } return box(a, 0, a.length); } /** * * @param a * @param fromIndex * @param toIndex * @return */ public static Double[] box(final double[] a, final int fromIndex, final int toIndex) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_DOUBLE_OBJECT_ARRAY; } final Double[] result = new Double[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = Double.valueOf(a[j]); } return result; } /** * * @param a * @return */ public static Boolean[][] box(boolean[][] a) { if (a == null) { return null; } final Boolean[][] result = new Boolean[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Character[][] box(char[][] a) { if (a == null) { return null; } final Character[][] result = new Character[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Byte[][] box(byte[][] a) { if (a == null) { return null; } final Byte[][] result = new Byte[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Short[][] box(short[][] a) { if (a == null) { return null; } final Short[][] result = new Short[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Integer[][] box(int[][] a) { if (a == null) { return null; } final Integer[][] result = new Integer[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Long[][] box(long[][] a) { if (a == null) { return null; } final Long[][] result = new Long[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Float[][] box(float[][] a) { if (a == null) { return null; } final Float[][] result = new Float[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Double[][] box(double[][] a) { if (a == null) { return null; } final Double[][] result = new Double[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Boolean[][][] box(boolean[][][] a) { if (a == null) { return null; } final Boolean[][][] result = new Boolean[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Character[][][] box(char[][][] a) { if (a == null) { return null; } final Character[][][] result = new Character[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Byte[][][] box(byte[][][] a) { if (a == null) { return null; } final Byte[][][] result = new Byte[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Short[][][] box(short[][][] a) { if (a == null) { return null; } final Short[][][] result = new Short[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Integer[][][] box(int[][][] a) { if (a == null) { return null; } final Integer[][][] result = new Integer[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Long[][][] box(long[][][] a) { if (a == null) { return null; } final Long[][][] result = new Long[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Float[][][] box(float[][][] a) { if (a == null) { return null; } final Float[][][] result = new Float[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } /** * * @param a * @return */ public static Double[][][] box(double[][][] a) { if (a == null) { return null; } final Double[][][] result = new Double[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = box(a[i]); } return result; } // Boolean array converters // ---------------------------------------------------------------------- /** *

* Converts an array of object Booleans to primitives. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Boolean} array, may be {@code null} * @return a {@code boolean} array, {@code null} if null array input */ @SafeVarargs public static boolean[] unbox(final Boolean... a) { return unbox(a, false); } /** *

* Converts an array of object Booleans to primitives handling {@code null}. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Boolean} array, may be {@code null} * @param valueForNull * the value to insert if {@code null} found * @return a {@code boolean} array, {@code null} if null array input */ public static boolean[] unbox(final Boolean[] a, final boolean valueForNull) { if (a == null) { return null; } return unbox(a, 0, a.length, valueForNull); } /** * * @param a * @param fromIndex * @param toIndex * @param valueForNull * @return */ public static boolean[] unbox(final Boolean[] a, final int fromIndex, final int toIndex, final boolean valueForNull) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_BOOLEAN_ARRAY; } final boolean[] result = new boolean[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = (a[j] == null ? valueForNull : a[j].booleanValue()); } return result; } // Character array converters // ---------------------------------------------------------------------- /** *

* Converts an array of object Characters to primitives. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Character} array, may be {@code null} * @return a {@code char} array, {@code null} if null array input */ @SafeVarargs public static char[] unbox(final Character... a) { return unbox(a, (char) 0); } /** *

* Converts an array of object Character to primitives handling {@code null} * . *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Character} array, may be {@code null} * @param valueForNull * the value to insert if {@code null} found * @return a {@code char} array, {@code null} if null array input */ public static char[] unbox(final Character[] a, final char valueForNull) { if (a == null) { return null; } return unbox(a, 0, a.length, valueForNull); } /** * * @param a * @param fromIndex * @param toIndex * @param valueForNull * @return */ public static char[] unbox(final Character[] a, final int fromIndex, final int toIndex, final char valueForNull) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_CHAR_ARRAY; } final char[] result = new char[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = (a[j] == null ? valueForNull : a[j].charValue()); } return result; } // Byte array converters // ---------------------------------------------------------------------- /** *

* Converts an array of object Bytes to primitives. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Byte} array, may be {@code null} * @return a {@code byte} array, {@code null} if null array input */ @SafeVarargs public static byte[] unbox(final Byte... a) { return unbox(a, (byte) 0); } /** *

* Converts an array of object Bytes to primitives handling {@code null}. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Byte} array, may be {@code null} * @param valueForNull * the value to insert if {@code null} found * @return a {@code byte} array, {@code null} if null array input */ public static byte[] unbox(final Byte[] a, final byte valueForNull) { if (a == null) { return null; } return unbox(a, 0, a.length, valueForNull); } /** * * @param a * @param fromIndex * @param toIndex * @param valueForNull * @return */ public static byte[] unbox(final Byte[] a, final int fromIndex, final int toIndex, final byte valueForNull) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_BYTE_ARRAY; } final byte[] result = new byte[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = (a[j] == null ? valueForNull : a[j].byteValue()); } return result; } // Short array converters // ---------------------------------------------------------------------- /** *

* Converts an array of object Shorts to primitives. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Short} array, may be {@code null} * @return a {@code byte} array, {@code null} if null array input */ @SafeVarargs public static short[] unbox(final Short... a) { return unbox(a, (short) 0); } /** *

* Converts an array of object Short to primitives handling {@code null}. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Short} array, may be {@code null} * @param valueForNull * the value to insert if {@code null} found * @return a {@code byte} array, {@code null} if null array input */ public static short[] unbox(final Short[] a, final short valueForNull) { if (a == null) { return null; } return unbox(a, 0, a.length, valueForNull); } /** * * @param a * @param fromIndex * @param toIndex * @param valueForNull * @return */ public static short[] unbox(final Short[] a, final int fromIndex, final int toIndex, final short valueForNull) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_SHORT_ARRAY; } final short[] result = new short[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = (a[j] == null ? valueForNull : a[j].shortValue()); } return result; } // Int array converters // ---------------------------------------------------------------------- /** *

* Converts an array of object Integers to primitives. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Integer} array, may be {@code null} * @return an {@code int} array, {@code null} if null array input */ @SafeVarargs public static int[] unbox(final Integer... a) { return unbox(a, 0); } /** *

* Converts an array of object Integer to primitives handling {@code null}. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Integer} array, may be {@code null} * @param valueForNull * the value to insert if {@code null} found * @return an {@code int} array, {@code null} if null array input */ public static int[] unbox(final Integer[] a, final int valueForNull) { if (a == null) { return null; } return unbox(a, 0, a.length, valueForNull); } /** * * @param a * @param fromIndex * @param toIndex * @param valueForNull * @return */ public static int[] unbox(final Integer[] a, final int fromIndex, final int toIndex, final int valueForNull) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_INT_ARRAY; } final int[] result = new int[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = (a[j] == null ? valueForNull : a[j].intValue()); } return result; } // Long array converters // ---------------------------------------------------------------------- /** *

* Converts an array of object Longs to primitives. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Long} array, may be {@code null} * @return a {@code long} array, {@code null} if null array input */ @SafeVarargs public static long[] unbox(final Long... a) { return unbox(a, 0L); } /** *

* Converts an array of object Long to primitives handling {@code null}. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Long} array, may be {@code null} * @param valueForNull * the value to insert if {@code null} found * @return a {@code long} array, {@code null} if null array input */ public static long[] unbox(final Long[] a, final long valueForNull) { if (a == null) { return null; } return unbox(a, 0, a.length, valueForNull); } /** * * @param a * @param fromIndex * @param toIndex * @param valueForNull * @return */ public static long[] unbox(final Long[] a, final int fromIndex, final int toIndex, final long valueForNull) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_LONG_ARRAY; } final long[] result = new long[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = (a[j] == null ? valueForNull : a[j].longValue()); } return result; } // Float array converters // ---------------------------------------------------------------------- /** *

* Converts an array of object Floats to primitives. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Float} array, may be {@code null} * @return a {@code float} array, {@code null} if null array input */ @SafeVarargs public static float[] unbox(final Float... a) { return unbox(a, 0f); } /** *

* Converts an array of object Floats to primitives handling {@code null}. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Float} array, may be {@code null} * @param valueForNull * the value to insert if {@code null} found * @return a {@code float} array, {@code null} if null array input */ public static float[] unbox(final Float[] a, final float valueForNull) { if (a == null) { return null; } return unbox(a, 0, a.length, valueForNull); } /** * * @param a * @param fromIndex * @param toIndex * @param valueForNull * @return */ public static float[] unbox(final Float[] a, final int fromIndex, final int toIndex, final float valueForNull) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_FLOAT_ARRAY; } final float[] result = new float[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = (a[j] == null ? valueForNull : a[j].floatValue()); } return result; } // Double array converters // ---------------------------------------------------------------------- /** *

* Converts an array of object Doubles to primitives. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Double} array, may be {@code null} * @return a {@code double} array, {@code null} if null array input */ @SafeVarargs public static double[] unbox(final Double... a) { return unbox(a, 0d); } /** *

* Converts an array of object Doubles to primitives handling {@code null}. *

* *

* This method returns {@code null} for a {@code null} input array. *

* * @param a * a {@code Double} array, may be {@code null} * @param valueForNull * the value to insert if {@code null} found * @return a {@code double} array, {@code null} if null array input */ public static double[] unbox(final Double[] a, final double valueForNull) { if (a == null) { return null; } return unbox(a, 0, a.length, valueForNull); } /** * * @param a * @param fromIndex * @param toIndex * @param valueForNull * @return */ public static double[] unbox(final Double[] a, final int fromIndex, final int toIndex, final double valueForNull) { if (a == null) { return null; } else if (toIndex - fromIndex == 0) { return N.EMPTY_DOUBLE_ARRAY; } final double[] result = new double[toIndex - fromIndex]; for (int i = 0, j = fromIndex; j < toIndex; i++, j++) { result[i] = (a[j] == null ? valueForNull : a[j].doubleValue()); } return result; } /** * * @param a * @return */ public static boolean[][] unbox(Boolean[][] a) { return unbox(a, false); } /** * * @param a * @param valueForNull * @return */ public static boolean[][] unbox(Boolean[][] a, boolean valueForNull) { if (a == null) { return null; } final boolean[][] result = new boolean[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static char[][] unbox(Character[][] a) { return unbox(a, (char) 0); } /** * * @param a * @param valueForNull * @return */ public static char[][] unbox(Character[][] a, char valueForNull) { if (a == null) { return null; } final char[][] result = new char[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static byte[][] unbox(Byte[][] a) { return unbox(a, (byte) 0); } /** * * @param a * @param valueForNull * @return */ public static byte[][] unbox(Byte[][] a, byte valueForNull) { if (a == null) { return null; } final byte[][] result = new byte[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static short[][] unbox(Short[][] a) { return unbox(a, (short) 0); } /** * * @param a * @param valueForNull * @return */ public static short[][] unbox(Short[][] a, short valueForNull) { if (a == null) { return null; } final short[][] result = new short[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static int[][] unbox(Integer[][] a) { return unbox(a, 0); } /** * * @param a * @param valueForNull * @return */ public static int[][] unbox(Integer[][] a, int valueForNull) { if (a == null) { return null; } final int[][] result = new int[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static long[][] unbox(Long[][] a) { return unbox(a, 0); } /** * * @param a * @param valueForNull * @return */ public static long[][] unbox(Long[][] a, long valueForNull) { if (a == null) { return null; } final long[][] result = new long[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static float[][] unbox(Float[][] a) { return unbox(a, 0); } /** * * @param a * @param valueForNull * @return */ public static float[][] unbox(Float[][] a, float valueForNull) { if (a == null) { return null; } final float[][] result = new float[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static double[][] unbox(Double[][] a) { return unbox(a, 0); } /** * * @param a * @param valueForNull * @return */ public static double[][] unbox(Double[][] a, double valueForNull) { if (a == null) { return null; } final double[][] result = new double[a.length][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static boolean[][][] unbox(Boolean[][][] a) { return unbox(a, false); } /** * * @param a * @param valueForNull * @return */ public static boolean[][][] unbox(Boolean[][][] a, boolean valueForNull) { if (a == null) { return null; } final boolean[][][] result = new boolean[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static char[][][] unbox(Character[][][] a) { return unbox(a, (char) 0); } /** * * @param a * @param valueForNull * @return */ public static char[][][] unbox(Character[][][] a, char valueForNull) { if (a == null) { return null; } final char[][][] result = new char[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static byte[][][] unbox(Byte[][][] a) { return unbox(a, (byte) 0); } /** * * @param a * @param valueForNull * @return */ public static byte[][][] unbox(Byte[][][] a, byte valueForNull) { if (a == null) { return null; } final byte[][][] result = new byte[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static short[][][] unbox(Short[][][] a) { return unbox(a, (short) 0); } /** * * @param a * @param valueForNull * @return */ public static short[][][] unbox(Short[][][] a, short valueForNull) { if (a == null) { return null; } final short[][][] result = new short[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static int[][][] unbox(Integer[][][] a) { return unbox(a, 0); } /** * * @param a * @param valueForNull * @return */ public static int[][][] unbox(Integer[][][] a, int valueForNull) { if (a == null) { return null; } final int[][][] result = new int[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static long[][][] unbox(Long[][][] a) { return unbox(a, 0); } /** * * @param a * @param valueForNull * @return */ public static long[][][] unbox(Long[][][] a, long valueForNull) { if (a == null) { return null; } final long[][][] result = new long[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static float[][][] unbox(Float[][][] a) { return unbox(a, 0); } /** * * @param a * @param valueForNull * @return */ public static float[][][] unbox(Float[][][] a, float valueForNull) { if (a == null) { return null; } final float[][][] result = new float[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } /** * * @param a * @return */ public static double[][][] unbox(Double[][][] a) { return unbox(a, 0); } /** * * @param a * @param valueForNull * @return */ public static double[][][] unbox(Double[][][] a, double valueForNull) { if (a == null) { return null; } final double[][][] result = new double[a.length][][]; for (int i = 0, len = a.length; i < len; i++) { result[i] = unbox(a[i], valueForNull); } return result; } // public static boolean unboxOrDefault(Boolean b) { // if (b == null) { // return false; // } // // return b.booleanValue(); // } // // public static boolean unboxOrDefault(Boolean b, boolean defaultForNull) { // if (b == null) { // return defaultForNull; // } // // return b.booleanValue(); // } // // public static char unboxOrDefault(Character c) { // if (c == null) { // return N.CHAR_0; // } // // return c.charValue(); // } // // public static char unboxOrDefault(Character c, char defaultForNull) { // if (c == null) { // return defaultForNull; // } // // return c.charValue(); // } // // public static byte unboxOrDefault(Byte b) { // if (b == null) { // return (byte) 0; // } // // return b.byteValue(); // } // // public static byte unboxOrDefault(Byte b, byte defaultForNull) { // if (b == null) { // return defaultForNull; // } // // return b.byteValue(); // } // // public static short unboxOrDefault(Short b) { // if (b == null) { // return (short) 0; // } // // return b.shortValue(); // } // // public static short unboxOrDefault(Short b, short defaultForNull) { // if (b == null) { // return defaultForNull; // } // // return b.shortValue(); // } // // public static int unboxOrDefault(Integer b) { // if (b == null) { // return 0; // } // // return b.intValue(); // } // // public static int unboxOrDefault(Integer b, int defaultForNull) { // if (b == null) { // return defaultForNull; // } // // return b.intValue(); // } // // public static long unboxOrDefault(Long b) { // if (b == null) { // return 0; // } // // return b.longValue(); // } // // public static long unboxOrDefault(Long b, long defaultForNull) { // if (b == null) { // return defaultForNull; // } // // return b.longValue(); // } // // public static float unboxOrDefault(Float b) { // if (b == null) { // return 0; // } // // return b.floatValue(); // } // // public static float unboxOrDefault(Float b, float defaultForNull) { // if (b == null) { // return defaultForNull; // } // // return b.floatValue(); // } // // public static double unboxOrDefault(Double b) { // if (b == null) { // return 0; // } // // return b.doubleValue(); // } // // public static double unboxOrDefault(Double b, double defaultForNull) { // if (b == null) { // return defaultForNull; // } // // return b.doubleValue(); // } // // public static boolean unboxOrGet(Boolean b, BooleanSupplier supplierForNull) { // if (b == null) { // return supplierForNull.getAsBoolean(); // } // // return b.booleanValue(); // } // // public static char unboxOrGet(Character b, CharSupplier supplierForNull) { // if (b == null) { // return supplierForNull.getAsChar(); // } // // return b.charValue(); // } // // public static byte unboxOrGet(Byte b, ByteSupplier supplierForNull) { // if (b == null) { // return supplierForNull.getAsByte(); // } // // return b.byteValue(); // } // // public static short unboxOrGet(Short b, ShortSupplier supplierForNull) { // if (b == null) { // return supplierForNull.getAsShort(); // } // // return b.shortValue(); // } // // public static int unboxOrGet(Integer b, IntSupplier supplierForNull) { // if (b == null) { // return supplierForNull.getAsInt(); // } // // return b.intValue(); // } // // public static long unboxOrGet(Long b, LongSupplier supplierForNull) { // if (b == null) { // return supplierForNull.getAsLong(); // } // // return b.longValue(); // } // // public static float unboxOrGet(Float b, FloatSupplier supplierForNull) { // if (b == null) { // return supplierForNull.getAsFloat(); // } // // return b.floatValue(); // } // // public static double unboxOrGet(Double b, DoubleSupplier supplierForNull) { // if (b == null) { // return supplierForNull.getAsDouble(); // } // // return b.doubleValue(); // } /** * The Class Booleans. */ // TODO public static final class Booleans { /** * Instantiates a new booleans. */ private Booleans() { // utility class. } /** * Inverts the element from {@code fromIndex} to {@code toIndex}: set it to {@code true} if it's {@code false}, or set it to {@code false} if it's {@code true}. * * @param a */ public static void invert(final boolean[] a) { if (N.isNullOrEmpty(a)) { return; } invert(a, 0, a.length); } /** * Inverts the element from {@code fromIndex} to {@code toIndex}: set it to {@code true} if it's {@code false}, or set it to {@code false} if it's {@code true}. * * @param a * @param fromIndex * @param toIndex */ public static void invert(final boolean[] a, final int fromIndex, final int toIndex) { N.checkFromToIndex(fromIndex, toIndex, N.len(a)); if (fromIndex == toIndex) { return; } for (int i = fromIndex; i < toIndex; i++) { a[i] = !a[i]; } } } /** * The Class Chars. */ // TODO public static final class Chars { /** * Instantiates a new chars. */ private Chars() { // utility class. } } /** * The Class Bytes. */ // TODO public static final class Bytes { /** * Instantiates a new bytes. */ private Bytes() { // utility class. } } /** * The Class Shorts. */ // TODO public static final class Shorts { /** * Instantiates a new shorts. */ private Shorts() { // utility class. } } /** * The Class Ints. */ // TODO public static final class Ints { /** * Instantiates a new ints. */ private Ints() { // utility class. } } /** * The Class Longs. */ // TODO public static final class Longs { /** * Instantiates a new longs. */ private Longs() { // utility class. } } /** * The Class Floats. */ // TODO public static final class Floats { /** * Instantiates a new floats. */ private Floats() { // utility class. } } /** * The Class Doubles. */ // TODO public static final class Doubles { /** * Instantiates a new doubles. */ private Doubles() { // utility class. } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy