/*
* Copyright 2016 NAVER Corp.
*
* 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.navercorp.pinpoint.common.util;
/**
* @author Woonduk Kang(emeroad)
*/
public final class ArrayUtils {
private static final int DEFAULT_ABBREVIATE_MAX_WIDTH = 32;
private ArrayUtils() {
}
public static String abbreviate(final byte[] bytes) {
return abbreviate(bytes, DEFAULT_ABBREVIATE_MAX_WIDTH);
}
public static String abbreviate(final byte[] bytes, final int maxWidth) {
if (bytes == null) {
return "null";
}
if (maxWidth < 0) {
throw new IllegalArgumentException("negative maxWidth:" + maxWidth);
}
// TODO handle negative limit
// Last valid index is length - 1
int bytesMaxLength = bytes.length - 1;
final int maxLimit = maxWidth - 1;
if (bytesMaxLength > maxLimit) {
bytesMaxLength = maxLimit;
}
if (bytesMaxLength == -1) {
if (bytes.length == 0) {
return "[]";
} else {
return "[...(" + bytes.length + ")]";
}
}
final StringBuilder sb = new StringBuilder();
sb.append('[');
for (int i = 0; ; i++) {
sb.append(bytes[i]);
if (i == bytesMaxLength) {
if ((bytes.length - 1) <= maxLimit) {
return sb.append(']').toString();
} else {
sb.append(", ...(");
sb.append(bytes.length - (i+1));
sb.append(")]");
return sb.toString();
}
}
sb.append(", ");
}
}
// copy apache commons lang3 ----------------------------------------------------------------------
/**
* <p>Checks if an array of Objects is empty or {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is empty or {@code null}
* @since 2.1
*/
public static <T> boolean isEmpty(final T[] array) {
return array == null || array.length == 0;
}
/**
* <p>Checks if an array of primitive longs is empty or {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is empty or {@code null}
* @since 2.1
*/
public static boolean isEmpty(final long[] array) {
return array == null || array.length == 0;
}
/**
* <p>Checks if an array of primitive ints is empty or {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is empty or {@code null}
* @since 2.1
*/
public static boolean isEmpty(final int[] array) {
return array == null || array.length == 0;
}
/**
* <p>Checks if an array of primitive shorts is empty or {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is empty or {@code null}
* @since 2.1
*/
public static boolean isEmpty(final short[] array) {
return array == null || array.length == 0;
}
/**
* <p>Checks if an array of primitive chars is empty or {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is empty or {@code null}
* @since 2.1
*/
public static boolean isEmpty(final char[] array) {
return array == null || array.length == 0;
}
/**
* <p>Checks if an array of primitive bytes is empty or {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is empty or {@code null}
* @since 2.1
*/
public static boolean isEmpty(final byte[] array) {
return array == null || array.length == 0;
}
/**
* <p>Checks if an array of primitive doubles is empty or {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is empty or {@code null}
* @since 2.1
*/
public static boolean isEmpty(final double[] array) {
return array == null || array.length == 0;
}
/**
* <p>Checks if an array of primitive floats is empty or {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is empty or {@code null}
* @since 2.1
*/
public static boolean isEmpty(final float[] array) {
return array == null || array.length == 0;
}
/**
* <p>Checks if an array of primitive booleans is empty or {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is empty or {@code null}
* @since 2.1
*/
public static boolean isEmpty(final boolean[] array) {
return array == null || array.length == 0;
}
// copy apache commons lang3 ----------------------------------------------------------------------
/**
* <p>Checks if an array of Objects is not empty and not {@code null}.
*
* @param <T> the component type of the array
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static <T> boolean isNotEmpty(final T[] array) {
return array != null && array.length != 0;
}
/**
* <p>Checks if an array of primitive longs is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final long[] array) {
return array != null && array.length != 0;
}
/**
* <p>Checks if an array of primitive ints is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final int[] array) {
return array != null && array.length != 0;
}
/**
* <p>Checks if an array of primitive shorts is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final short[] array) {
return array != null && array.length != 0;
}
/**
* <p>Checks if an array of primitive chars is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final char[] array) {
return array != null && array.length != 0;
}
/**
* <p>Checks if an array of primitive bytes is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final byte[] array) {
return array != null && array.length != 0;
}
/**
* <p>Checks if an array of primitive doubles is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final double[] array) {
return array != null && array.length != 0;
}
/**
* <p>Checks if an array of primitive floats is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final float[] array) {
return array != null && array.length != 0;
}
/**
* <p>Checks if an array of primitive booleans is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final boolean[] array) {
return array != null && array.length != 0;
}
//---------------------------------------------------------------------------
// Enhancement type safety & Remove native call ---------------------------------------------------------------------------
/**
* <p>Returns the length of the specified array.
* This method can deal with {@code Object} arrays and with primitive arrays.
*
* <p>If the input array is {@code null}, {@code 0} is returned.
*
* <pre>
*
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or {@code 0} if the array is {@code null}
* @throws IllegalArgumentException if the object argument is not an array.
* @since 2.1
*/
public static int getLength(final long[] array) {
if (array == null) {
return 0;
}
return array.length;
}
/**
* <p>Returns the length of the specified array.
* This method can deal with {@code Object} arrays and with primitive arrays.
*
* <p>If the input array is {@code null}, {@code 0} is returned.
*
* <pre>
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or {@code 0} if the array is {@code null}
* @throws IllegalArgumentException if the object argument is not an array.
* @since 2.1
*/
public static int getLength(final int[] array) {
if (array == null) {
return 0;
}
return array.length;
}
/**
* <p>Returns the length of the specified array.
* This method can deal with {@code Object} arrays and with primitive arrays.
*
* <p>If the input array is {@code null}, {@code 0} is returned.
*
* <pre>
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or {@code 0} if the array is {@code null}
* @throws IllegalArgumentException if the object argument is not an array.
* @since 2.1
*/
public static int getLength(final short[] array) {
if (array == null) {
return 0;
}
return array.length;
}
/**
* <p>Returns the length of the specified array.
* This method can deal with {@code Object} arrays and with primitive arrays.
*
* <p>If the input array is {@code null}, {@code 0} is returned.
*
* <pre>
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or {@code 0} if the array is {@code null}
* @throws IllegalArgumentException if the object argument is not an array.
* @since 2.1
*/
public static int getLength(final char[] array) {
if (array == null) {
return 0;
}
return array.length;
}
/**
* <p>Returns the length of the specified array.
* This method can deal with {@code Object} arrays and with primitive arrays.
*
* <p>If the input array is {@code null}, {@code 0} is returned.
*
* <pre>
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or {@code 0} if the array is {@code null}
* @throws IllegalArgumentException if the object argument is not an array.
* @since 2.1
*/
public static int getLength(final byte[] array) {
if (array == null) {
return 0;
}
return array.length;
}
/**
* <p>Returns the length of the specified array.
* This method can deal with {@code Object} arrays and with primitive arrays.
*
* <p>If the input array is {@code null}, {@code 0} is returned.
*
* <pre>
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or {@code 0} if the array is {@code null}
* @throws IllegalArgumentException if the object argument is not an array.
* @since 2.1
*/
public static int getLength(final double[] array) {
if (array == null) {
return 0;
}
return array.length;
}
/**
* <p>Returns the length of the specified array.
* This method can deal with {@code Object} arrays and with primitive arrays.
*
* <p>If the input array is {@code null}, {@code 0} is returned.
*
* <pre>
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or {@code 0} if the array is {@code null}
* @throws IllegalArgumentException if the object argument is not an array.
* @since 2.1
*/
public static int getLength(final float[] array) {
if (array == null) {
return 0;
}
return array.length;
}
/**
* <p>Returns the length of the specified array.
* This method can deal with {@code Object} arrays and with primitive arrays.
*
* <p>If the input array is {@code null}, {@code 0} is returned.
*
* <pre>
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or {@code 0} if the array is {@code null}
* @throws IllegalArgumentException if the object argument is not an array.
* @since 2.1
*/
public static int getLength(final boolean[] array) {
if (array == null) {
return 0;
}
return array.length;
}
/**
* <p>Returns the length of the specified array.
* This method can deal with {@code Object} arrays and with primitive arrays.
*
* <p>If the input array is {@code null}, {@code 0} is returned.
*
* <pre>
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or {@code 0} if the array is {@code null}
* @throws IllegalArgumentException if the object argument is not an array.
* @since 2.1
*/
public static <T> int getLength(final T[] array) {
if (array == null) {
return 0;
}
return array.length;
}
}