Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.Collection; import java.util.Map; import org.json.JSONArray; public class Main { /** * Checks if the given string is empty. * * @param s * string * @return <code>true</code> if the given reference is <code>null</code> or string is empty */ public static boolean isEmpty(final String s) { return length(s) == 0; } /** * Checks if the given array is empty. * * @param array * array to check * @return <code>true</code> if the given reference is <code>null</code> or array is empty */ public static boolean isEmpty(final Object[] array) { return length(array) == 0; } /** * Checks if the given array is empty. * * @param array * array to check * @return <code>true</code> if the given reference is <code>null</code> or array is empty */ public static boolean isEmpty(final JSONArray array) { return length(array) == 0; } /** * Checks if the given array is empty. * * @param array * array to check * @return <code>true</code> if the given reference is <code>null</code> or array is empty */ public static boolean isEmpty(final boolean[] array) { return length(array) == 0; } /** * Checks if the given array is empty. * * @param array * array to check * @return <code>true</code> if the given reference is <code>null</code> or array is empty */ public static boolean isEmpty(final byte[] array) { return length(array) == 0; } /** * Checks if the given array is empty. * * @param array * array to check * @return <code>true</code> if the given reference is <code>null</code> or array is empty */ public static boolean isEmpty(final short[] array) { return length(array) == 0; } /** * Checks if the given array is empty. * * @param array * array to check * @return <code>true</code> if the given reference is <code>null</code> or array is empty */ public static boolean isEmpty(final int[] array) { return length(array) == 0; } /** * Checks if the given array is empty. * * @param array * array to check * @return <code>true</code> if the given reference is <code>null</code> or array is empty */ public static boolean isEmpty(final long[] array) { return length(array) == 0; } /** * Checks if the given array is empty. * * @param array * array to check * @return <code>true</code> if the given reference is <code>null</code> or array is empty */ public static boolean isEmpty(final float[] array) { return length(array) == 0; } /** * Checks if the given array is empty. * * @param array * array to check * @return <code>true</code> if the given reference is <code>null</code> or array is empty */ public static boolean isEmpty(final double[] array) { return length(array) == 0; } /** * Checks if the given collection is empty. * * @param c * collection to check * @return <code>true</code> if the given reference is <code>null</code> or collection is empty */ public static boolean isEmpty(final Collection<?> c) { return c == null || c.isEmpty(); } /** * Checks if the given map is empty. * * @param map * map to check * @return <code>true</code> if the given reference is <code>null</code> or map is empty */ public static boolean isEmpty(final Map<?, ?> map) { return map == null || map.isEmpty(); } /** * Safely calculates a string length. * * @param s * string * @return real string length or <code>0</code> if reference is <code>null</code> */ public static int length(final String s) { return s != null ? s.length() : 0; } /** * Safely calculates an array length. * * @param arr * array * @return real array length or <code>0</code> if reference is <code>null</code> */ public static int length(final Object[] arr) { return arr != null ? arr.length : 0; } /** * Safely calculates an array length. * * @param arr * array * @return real array length or <code>0</code> if reference is <code>null</code> */ public static int length(final JSONArray arr) { return arr != null ? arr.length() : 0; } /** * Safely calculates an array length. * * @param arr * array * @return real array length or <code>0</code> if reference is <code>null</code> */ public static int length(final boolean[] arr) { return arr != null ? arr.length : 0; } /** * Safely calculates a collection length. * * @param c * collection to check * @return real collection length or <code>0</code> if reference is <code>null</code> */ public static int length(final Collection<?> c) { return c != null ? c.size() : 0; } /** * Safely calculates a map length. * * @param map * map to check * @return real map length or <code>0</code> if reference is <code>null</code> */ public static int length(final Map<?, ?> map) { return map != null ? map.size() : 0; } /** * Safely calculates an array length. * * @param arr * array * @return real array length or <code>0</code> if reference is <code>null</code> */ public static int length(final byte[] arr) { return arr != null ? arr.length : 0; } /** * Safely calculates an array length. * * @param arr * array * @return real array length or <code>0</code> if reference is <code>null</code> */ public static int length(final short[] arr) { return arr != null ? arr.length : 0; } /** * Safely calculates an array length. * * @param arr * array * @return real array length or <code>0</code> if reference is <code>null</code> */ public static int length(final int[] arr) { return arr != null ? arr.length : 0; } /** * Safely calculates an array length. * * @param arr * array * @return real array length or <code>0</code> if reference is <code>null</code> */ public static int length(final long[] arr) { return arr != null ? arr.length : 0; } /** * Safely calculates an array length. * * @param arr * array * @return real array length or <code>0</code> if reference is <code>null</code> */ public static int length(final float[] arr) { return arr != null ? arr.length : 0; } /** * Safely calculates an array length. * * @param arr * array * @return real array length or <code>0</code> if reference is <code>null</code> */ public static int length(final double[] arr) { return arr != null ? arr.length : 0; } }