Java tutorial
//package com.java2s; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ public class Main { /** * An empty immutable {@code long} array. */ public static final long[] EMPTY_LONG_ARRAY = new long[0]; /** * An empty immutable {@code int} array. */ public static final int[] EMPTY_INT_ARRAY = new int[0]; /** * An empty immutable {@code short} array. */ public static final short[] EMPTY_SHORT_ARRAY = new short[0]; /** * An empty immutable {@code byte} array. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; /** * An empty immutable {@code double} array. */ public static final double[] EMPTY_DOUBLE_ARRAY = new double[0]; /** * An empty immutable {@code float} array. */ public static final float[] EMPTY_FLOAT_ARRAY = new float[0]; /** * An empty immutable {@code boolean} array. */ public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0]; /** * An empty immutable {@code char} array. */ public static final char[] EMPTY_CHAR_ARRAY = new char[0]; /** * <p>Converts an array of object Characters to primitives.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code Character} array, may be {@code null} * @return a {@code char} array, {@code null} if null array input * @throws NullPointerException if array content is {@code null} */ public static char[] toPrimitive(Character[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_CHAR_ARRAY; } final char[] result = new char[array.length]; for (int i = 0; i < array.length; i++) { result[i] = array[i].charValue(); } return result; } /** * <p>Converts an array of object Character to primitives handling {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array 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[] toPrimitive(Character[] array, char valueForNull) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_CHAR_ARRAY; } final char[] result = new char[array.length]; for (int i = 0; i < array.length; i++) { Character b = array[i]; result[i] = (b == null ? valueForNull : b.charValue()); } return result; } /** * <p>Converts an array of object Longs to primitives.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code Long} array, may be {@code null} * @return a {@code long} array, {@code null} if null array input * @throws NullPointerException if array content is {@code null} */ public static long[] toPrimitive(Long[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_LONG_ARRAY; } final long[] result = new long[array.length]; for (int i = 0; i < array.length; i++) { result[i] = array[i].longValue(); } return result; } /** * <p>Converts an array of object Long to primitives handling {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array 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[] toPrimitive(Long[] array, long valueForNull) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_LONG_ARRAY; } final long[] result = new long[array.length]; for (int i = 0; i < array.length; i++) { Long b = array[i]; result[i] = (b == null ? valueForNull : b.longValue()); } return result; } /** * <p>Converts an array of object Integers to primitives.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code Integer} array, may be {@code null} * @return an {@code int} array, {@code null} if null array input * @throws NullPointerException if array content is {@code null} */ public static int[] toPrimitive(Integer[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_INT_ARRAY; } final int[] result = new int[array.length]; for (int i = 0; i < array.length; i++) { result[i] = array[i].intValue(); } return result; } /** * <p>Converts an array of object Integer to primitives handling {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array 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[] toPrimitive(Integer[] array, int valueForNull) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_INT_ARRAY; } final int[] result = new int[array.length]; for (int i = 0; i < array.length; i++) { Integer b = array[i]; result[i] = (b == null ? valueForNull : b.intValue()); } return result; } /** * <p>Converts an array of object Shorts to primitives.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code Short} array, may be {@code null} * @return a {@code byte} array, {@code null} if null array input * @throws NullPointerException if array content is {@code null} */ public static short[] toPrimitive(Short[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_SHORT_ARRAY; } final short[] result = new short[array.length]; for (int i = 0; i < array.length; i++) { result[i] = array[i].shortValue(); } return result; } /** * <p>Converts an array of object Short to primitives handling {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array 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[] toPrimitive(Short[] array, short valueForNull) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_SHORT_ARRAY; } final short[] result = new short[array.length]; for (int i = 0; i < array.length; i++) { Short b = array[i]; result[i] = (b == null ? valueForNull : b.shortValue()); } return result; } /** * <p>Converts an array of object Bytes to primitives.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code Byte} array, may be {@code null} * @return a {@code byte} array, {@code null} if null array input * @throws NullPointerException if array content is {@code null} */ public static byte[] toPrimitive(Byte[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_BYTE_ARRAY; } final byte[] result = new byte[array.length]; for (int i = 0; i < array.length; i++) { result[i] = array[i].byteValue(); } return result; } /** * <p>Converts an array of object Bytes to primitives handling {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array 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[] toPrimitive(Byte[] array, byte valueForNull) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_BYTE_ARRAY; } final byte[] result = new byte[array.length]; for (int i = 0; i < array.length; i++) { Byte b = array[i]; result[i] = (b == null ? valueForNull : b.byteValue()); } return result; } /** * <p>Converts an array of object Doubles to primitives.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code Double} array, may be {@code null} * @return a {@code double} array, {@code null} if null array input * @throws NullPointerException if array content is {@code null} */ public static double[] toPrimitive(Double[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_DOUBLE_ARRAY; } final double[] result = new double[array.length]; for (int i = 0; i < array.length; i++) { result[i] = array[i].doubleValue(); } return result; } /** * <p>Converts an array of object Doubles to primitives handling {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array 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[] toPrimitive(Double[] array, double valueForNull) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_DOUBLE_ARRAY; } final double[] result = new double[array.length]; for (int i = 0; i < array.length; i++) { Double b = array[i]; result[i] = (b == null ? valueForNull : b.doubleValue()); } return result; } /** * <p>Converts an array of object Floats to primitives.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code Float} array, may be {@code null} * @return a {@code float} array, {@code null} if null array input * @throws NullPointerException if array content is {@code null} */ public static float[] toPrimitive(Float[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_FLOAT_ARRAY; } final float[] result = new float[array.length]; for (int i = 0; i < array.length; i++) { result[i] = array[i].floatValue(); } return result; } /** * <p>Converts an array of object Floats to primitives handling {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array 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[] toPrimitive(Float[] array, float valueForNull) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_FLOAT_ARRAY; } final float[] result = new float[array.length]; for (int i = 0; i < array.length; i++) { Float b = array[i]; result[i] = (b == null ? valueForNull : b.floatValue()); } return result; } /** * <p>Converts an array of object Booleans to primitives.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code Boolean} array, may be {@code null} * @return a {@code boolean} array, {@code null} if null array input * @throws NullPointerException if array content is {@code null} */ public static boolean[] toPrimitive(Boolean[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_BOOLEAN_ARRAY; } final boolean[] result = new boolean[array.length]; for (int i = 0; i < array.length; i++) { result[i] = array[i].booleanValue(); } return result; } /** * <p>Converts an array of object Booleans to primitives handling {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array 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[] toPrimitive(Boolean[] array, boolean valueForNull) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_BOOLEAN_ARRAY; } final boolean[] result = new boolean[array.length]; for (int i = 0; i < array.length; i++) { Boolean b = array[i]; result[i] = (b == null ? valueForNull : b.booleanValue()); } return result; } }