com.univocity.parsers.common.ArgumentUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of univocity-parsers Show documentation
Show all versions of univocity-parsers Show documentation
univocity's open source parsers for processing different text formats using a consistent API
/*******************************************************************************
* Copyright 2014 uniVocity Software Pty Ltd
*
* 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.univocity.parsers.common;
import java.util.*;
/**
* An utility class for validating inputs.
*
* @author uniVocity Software Pty Ltd - [email protected]
*
*/
public class ArgumentUtils {
public static String[] EMPTY_STRING_ARRAY = new String[0];
/**
* Throws an IllegalArgumentException if the given array is null or empty.
* @param argDescription the description of the elements
* @param args the elements to be validated.
* @param Type of arguments to be validated
*/
public static void notEmpty(String argDescription, T... args) {
if (args == null) {
throw new IllegalArgumentException(argDescription + " must not be null");
}
if (args.length == 0) {
throw new IllegalArgumentException(argDescription + " must not be empty");
}
}
/**
* Throws an IllegalArgumentException if the given array is null,empty, or contains null values
* @param argDescription the description of the elements
* @param args the elements to be validated.
* @param Type of arguments to be validated
*/
public static void noNulls(String argDescription, T... args) {
notEmpty(argDescription, args);
for (T arg : args) {
if (arg == null) {
if (args.length > 0) {
throw new IllegalArgumentException(argDescription + " must not contain nulls");
} else {
throw new IllegalArgumentException(argDescription + " must not be null");
}
}
}
}
/**
* Returns the index of an element in a given array.
* @param array the element array
* @param element the element to be looked for in the array.
*
* @return the index of the given element in the array, or -1 if the element could not be found.
*/
public static int indexOf(Object[] array, Object element) {
if (array == null) {
throw new NullPointerException("Null array");
}
if (element == null) {
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
return i;
}
}
} else {
if (element instanceof String && array instanceof String[]) {
for (int i = 0; i < array.length; i++) {
String e = String.valueOf(array[i]);
if (element.toString().equalsIgnoreCase(e)) {
return i;
}
}
}
for (int i = 0; i < array.length; i++) {
if (element.equals(array[i])) {
return i;
}
}
}
return -1;
}
/**
* Searches for elements in a given array and returns the elements not found.
* @param array An array with elements
* @param elements the elements to be found
* @return the elements not found in the array.
*/
public static Object[] findMissingElements(Object[] array, Collection> elements) {
return findMissingElements(array, elements.toArray());
}
/**
* Searches for elements in a given array and returns the elements not found.
* @param array An array with elements
* @param elements the elements to be found
* @return the elements not found in the array.
*/
public static Object[] findMissingElements(Object[] array, Object[] elements) {
List