ObjectUtil.java
package com.tradecloud.domain.base.utils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.functors.NotNullPredicate;
import org.apache.commons.collections4.functors.NullPredicate;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class ObjectUtil {
public static boolean anyNull(Object... objects) {
return CollectionUtils.exists(Arrays.asList(objects), NullPredicate.INSTANCE);
}
public static boolean allNotNull(Object... objects) {
return !anyNull(objects);
}
public static boolean anyNotNull(Object... objects) {
return CollectionUtils.exists(Arrays.asList(objects), NotNullPredicate.INSTANCE);
}
public static boolean allNull(Object... objects) {
return !anyNotNull(objects);
}
public static boolean containsAll(String s, String... all) {
for (String a : all) {
if (!s.contains(a)) {
return false;
}
}
return true;
}
public static int countNulls(Object... objects) {
int count = 0;
for (Object obj : objects) {
if (obj == null) {
count++;
}
}
return count;
}
/**
* Validates that the supplied object is not null. If it is, we throw an exception and specify the name of the object.
*
* @param object the object to check for null
* @param name the name of the field represented by the object, this will be used in the exception for information
* @throws IllegalArgumentException if object is null
*/
public static void validateNotNull(Object object, String name) {
if (object == null) {
throw new IllegalArgumentException(StringUtils.capitalize(name) + " is required");
}
}
public static void validateNotEmpty(Object objects[], String name) {
if (objects == null || objects.length == 0) {
throw new IllegalArgumentException(StringUtils.capitalize(name) + " is required");
}
}
public static void validateNotEmpty(Collection objects, String name) {
if (objects == null || objects.isEmpty()) {
throw new IllegalArgumentException(StringUtils.capitalize(name) + " is required");
}
}
/**
* Returns the first element of a collection, or null if there is none.
*
* @param <T>
* @param col
* @return
*/
public static <T> T first(Collection<T> col) {
return col.isEmpty() ? null : col.iterator().next();
}
/**
* Useful for printing messages. eg 'Item x selected' and 'Items x,y selected'.
*
* @param singular
* @param plural
* @param col
* @return
*/
public static String singularPlural(String singular, String plural, Collection col) {
return col.size() == 1 ? singular : plural;
}
/**
* This is a bit nasty. Needed for hibnerate, where you call a merge, and you want to find your original object in a collection.
*
* @param <T>
* @param list
* @return
*/
public static <T> T get(List<T> list, T object) {
return list.get(list.indexOf(object));
}
public static int fromRoman(String roman) {
int value = 0;
int[] sumSeries = new int[roman.length()];
for (int i = 0; i < roman.length(); i++) {
// we need to consider order, XI is 6 whilst IX is 4
if (roman.charAt(i) == 'I') {
sumSeries[i] = 1;
} else if (roman.charAt(i) == 'V') {
sumSeries[i] = 5;
} else if (roman.charAt(i) == 'X') {
sumSeries[i] = 10;
} else {
// if we get to here, we don't have a roman numeral, tell user
// return -1
return -1;
}
}
// Now we must deal with the series we have created in the form of
// an array named sumSeries
// eg. III - 1, 1, 1
// eg. IV - 1, 5
int counter = 0;
int next;
do {
if (counter == sumSeries.length - 1) {
next = 0;
} else {
next = sumSeries[counter + 1];
}
if (sumSeries[counter] >= next) {
value += sumSeries[counter];
} else {
value = next - sumSeries[counter] - sumSeries[counter + 1];
}
counter++;
} while (counter < sumSeries.length);
return value;
}
}