/** * Validates that the given collection of elements is unique. * * @param elements The elements to validate for being unique. * @param <T> The actual type. * @return The given value. */ public static <T extends Collection<?>> T unique(T elements) { Set<Object> found = new HashSet<Object>(elements.size()); for (Object element : elements) { if (!found.add(element)) { throw new IllegalArgumentException("Duplicate element: " + element); } } return elements; }
/** * Validates that a collection of generic type descriptions does not contain duplicate type * erasure. * * @param typeDescriptions The type descriptions to validate for being unique. * @param <T> The actual type of the argument. * @return The input value. */ public static <T extends Collection<? extends GenericTypeDescription>> T uniqueRaw( T typeDescriptions) { Map<TypeDescription, GenericTypeDescription> types = new HashMap<TypeDescription, GenericTypeDescription>(typeDescriptions.size()); for (GenericTypeDescription typeDescription : typeDescriptions) { GenericTypeDescription conflictingType = types.put(typeDescription.asErasure(), typeDescription); if (conflictingType != null) { throw new IllegalArgumentException( "Duplicate types: " + typeDescription + " and " + conflictingType); } } return typeDescriptions; }
public static <T extends Collection<?>> boolean isVoid(T collection) { return collection == null || collection.size() == 0; }
/** * Validates that a collection is empty. * * @param collection The collection to be validated. * @param exceptionMessage The message of the exception that is thrown if the collection does * contain an element. * @param <T> The type of the collection. * @return The same collection that was validated. */ public static <T extends Collection<?>> T isEmpty(T collection, String exceptionMessage) { if (collection.size() > 0) { throw new IllegalArgumentException(exceptionMessage); } return collection; }
@Override public boolean isEmpty(T value) { return (value == null) || (value.size() == 0); }