Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* 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 com.univocity.parsers.common.fields.*;
import com.univocity.parsers.common.input.*;
import java.io.*;
import java.nio.charset.*;
import java.util.*;
import static java.lang.reflect.Array.*;
/**
* An utility class for validating inputs.
*
* @author Univocity Software Pty Ltd - [email protected]
*/
public class ArgumentUtils {
/**
* An empty String array.
*/
public static final String[] EMPTY_STRING_ARRAY = new String[0];
public static final NormalizedString[] EMPTY_NORMALIZED_STRING_ARRAY = new NormalizedString[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 a header, when headers are selected using a {@link FieldSelector}.
*
* @param array the element array
* @param element the element to be looked for in the array.
* @param fieldSelector a field selector that indicates which elements of the given array are selected.
*
* @return the index of the given element in the array, or -1 if the element could not be found.
*/
public static int indexOf(NormalizedString[] array, NormalizedString element, FieldSelector fieldSelector) {
int index = indexOf(array, element);
if (fieldSelector == null || index == -1) {
return index;
}
int[] indexes = fieldSelector.getFieldIndexes(array);
for (int i = 0; i < indexes.length; i++) {
if (indexes[i] == index) {
return i;
}
}
return -1;
}
/**
* Returns the indexes 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 indexes of the given element in the array, or an empty array if no element could be found
*/
public static int[] indexesOf(Object[] array, Object element) {
int[] tmp = new int[0];
int i = 0;
int o = 0;
while (i < array.length) {
i = indexOf(array, element, i);
if (i == -1) {
break;
}
tmp = Arrays.copyOf(tmp, tmp.length + 1);
tmp[o++] = i;
i++;
}
return tmp;
}
/**
* 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) {
return indexOf(array, element, 0);
}
/**
* Returns the index of a character in a given array.
*
* @param array the character array
* @param element the character to be looked for in the array.
* @param from the starting position of the array from where to start the search
*
* @return the index of the given character in the array, or -1 if the character could not be found.
*/
public static int indexOf(char[] array, char element, int from) {
for (int i = from; i < array.length; i++) {
if (array[i] == element) {
return i;
}
}
return -1;
}
/**
* 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.
* @param from the starting position of the array from where to start the search
*
* @return the index of the given element in the array, or -1 if the element could not be found.
*/
private static int indexOf(Object[] array, Object element, int from) {
if (array == null) {
throw new NullPointerException("Null array");
}
if (element == null) {
for (int i = from; i < array.length; i++) {
if (array[i] == null) {
return i;
}
}
} else {
if (element.getClass() != array.getClass().getComponentType()) {
throw new IllegalStateException("a");
}
if (element instanceof String && array instanceof String[]) {
for (int i = from; i < array.length; i++) {
String e = String.valueOf(array[i]);
if (element.toString().equalsIgnoreCase(e)) {
return i;
}
}
} else {
for (int i = from; 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