/** * <p>Validate that the specified argument array is neither * {@code null} nor contains any elements that are {@code null}; * otherwise throwing an exception.</p> * * <pre>Validate.noNullElements(myArray);</pre> * * <p>If the array is {@code null}, then the message in the exception * is "The validated object is null".</p> * * <p>If the array has a {@code null} element, then the message in the * exception is "The validated array contains null element at index: * " followed by the index.</p> * * @param <T> the array type * @param array the array to check, validated not null by this method * @return the validated array (never {@code null} method for chaining) * @throws NullPointerException if the array is {@code null} * @throws IllegalArgumentException if an element is {@code null} * @see #noNullElements(Object[], String, Object...) */ public static <T> T[] noNullElements(final T[] array) { return noNullElements(array, DEFAULT_NO_NULL_ELEMENTS_ARRAY_EX_MESSAGE); }
/** * <p>Validate that the specified argument iterable is neither * {@code null} nor contains any elements that are {@code null}; * otherwise throwing an exception. * * <pre>Validate.noNullElements(myCollection);</pre> * * <p>If the iterable is {@code null}, then the message in the exception * is "The validated object is null".</p> * * <p>If the array has a {@code null} element, then the message in the * exception is "The validated iterable contains null element at index: * " followed by the index.</p> * * @param <T> the iterable type * @param iterable the iterable to check, validated not null by this method * @return the validated iterable (never {@code null} method for chaining) * @throws NullPointerException if the array is {@code null} * @throws IllegalArgumentException if an element is {@code null} * @see #noNullElements(Iterable, String, Object...) */ public static <T extends Iterable<?>> T noNullElements(final T iterable) { return noNullElements(iterable, DEFAULT_NO_NULL_ELEMENTS_COLLECTION_EX_MESSAGE); }
/** * Find the "best guess" middle value among comparables. If there is an even * number of total values, the lower of the two middle values will be returned. * @param <T> type of values processed by this method * @param items to compare * @return T at middle position * @throws NullPointerException if items is {@code null} * @throws IllegalArgumentException if items is empty or contains {@code null} values * @since 3.0.1 */ @SafeVarargs public static <T extends Comparable<? super T>> T median(final T... items) { Validate.notEmpty(items); Validate.noNullElements(items); final TreeSet<T> sort = new TreeSet<>(); Collections.addAll(sort, items); @SuppressWarnings("unchecked") //we know all items added were T instances final T result = (T) sort.toArray()[(sort.size() - 1) / 2]; return result; }
/** * <p>Creates a long bit vector representation of the given array of Enum values.</p> * * <p>This generates a value that is usable by {@link EnumUtils#processBitVector}.</p> * * <p>Do not use this method if you have more than 64 values in your Enum, as this * would create a value greater than a long can hold.</p> * * @param enumClass the class of the enum we are working with, not {@code null} * @param values the values we want to convert, not {@code null} * @param <E> the type of the enumeration * @return a long whose value provides a binary representation of the given set of enum values. * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null} * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values * @since 3.0.1 * @see #generateBitVectors(Class, Iterable) */ @SafeVarargs public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final E... values) { Validate.noNullElements(values); return generateBitVector(enumClass, Arrays.asList(values)); }
/** * Find the "best guess" middle value among comparables. If there is an even * number of total values, the lower of the two middle values will be returned. * @param <T> type of values processed by this method * @param comparator to use for comparisons * @param items to compare * @return T at middle position * @throws NullPointerException if items or comparator is {@code null} * @throws IllegalArgumentException if items is empty or contains {@code null} values * @since 3.0.1 */ @SafeVarargs public static <T> T median(final Comparator<T> comparator, final T... items) { Validate.notEmpty(items, "null/empty items"); Validate.noNullElements(items); Validate.notNull(comparator, "null comparator"); final TreeSet<T> sort = new TreeSet<>(comparator); Collections.addAll(sort, items); @SuppressWarnings("unchecked") //we know all items added were T instances final T result = (T) sort.toArray()[(sort.size() - 1) / 2]; return result; }
/** * Create a parameterized type instance. * * @param owner the owning type * @param raw the raw class to create a parameterized type instance for * @param typeArguments the types used for parameterization * * @return {@link ParameterizedType} * @since 3.2 */ public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> raw, final Type... typeArguments) { Validate.notNull(raw, "raw class is null"); final Type useOwner; if (raw.getEnclosingClass() == null) { Validate.isTrue(owner == null, "no owner allowed for top-level %s", raw); useOwner = null; } else if (owner == null) { useOwner = raw.getEnclosingClass(); } else { Validate.isTrue(isAssignable(owner, raw.getEnclosingClass()), "%s is invalid owner type for parameterized %s", owner, raw); useOwner = owner; } Validate.noNullElements(typeArguments, "null type argument at index %s"); Validate.isTrue(raw.getTypeParameters().length == typeArguments.length, "invalid number of type parameters specified: expected %d, got %d", raw.getTypeParameters().length, typeArguments.length); return new ParameterizedTypeImpl(raw, useOwner, typeArguments); }
/** * <p>Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.</p> * * <p>This generates a value that is usable by {@link EnumUtils#processBitVectors}.</p> * * <p>Use this method if you have more than 64 values in your Enum.</p> * * @param enumClass the class of the enum we are working with, not {@code null} * @param values the values we want to convert, not {@code null}, neither containing {@code null} * @param <E> the type of the enumeration * @return a long[] whose values provide a binary representation of the given set of enum values * with least significant digits rightmost. * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null} * @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null} * @since 3.2 */ @SafeVarargs public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final E... values) { asEnum(enumClass); Validate.noNullElements(values); final EnumSet<E> condensed = EnumSet.noneOf(enumClass); Collections.addAll(condensed, values); final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1]; for (final E value : condensed) { result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE); } ArrayUtils.reverse(result); return result; }
/** * Append {@code types} to {@code buf} with separator {@code sep}. * @param buf destination * @param sep separator * @param types to append * @return {@code buf} * @since 3.2 */ private static <T> StringBuilder appendAllTo(final StringBuilder buf, final String sep, final T... types) { Validate.notEmpty(Validate.noNullElements(types)); if (types.length > 0) { buf.append(toString(types[0])); for (int i = 1; i < types.length; i++) { buf.append(sep).append(toString(types[i])); } } return buf; }
/** * Creates a new {@link Color} combining the provided {@link DyeColor} * objects. Since {@link DyeColor}s can be converted into {@link Color} * objects themselves, their summation and average is taken into effect * to properly mix the colors together. * * @param colors The colors to mix * @return The final output mixed color */ public static Color mixDyeColors(DyeColor... colors) { Validate.noNullElements(colors, "No nulls allowed!"); final Color[] actualColors = new Color[colors.length]; for (int i = 0; i < colors.length; i++) { actualColors[i] = colors[i].getColor(); } return mixColors(actualColors); }
@Test public void testNoNullElementsCollection2() { final List<String> coll = new ArrayList<>(); coll.add("a"); coll.add("b"); Validate.noNullElements(coll, "MSG"); try { Validate.noNullElements((Collection<?>) null, "MSG"); fail("Expecting NullPointerException"); } catch (final NullPointerException ex) { assertEquals("The validated object is null", ex.getMessage()); } coll.set(1, null); try { Validate.noNullElements(coll, "MSG"); fail("Expecting IllegalArgumentException"); } catch (final IllegalArgumentException ex) { assertEquals("MSG", ex.getMessage()); } coll.set(1, "b"); final List<String> test = Validate.noNullElements(coll, "Message"); assertSame(coll, test); }
@Test public void testNoNullElementsCollection1() { final List<String> coll = new ArrayList<>(); coll.add("a"); coll.add("b"); Validate.noNullElements(coll); try { Validate.noNullElements((Collection<?>) null); fail("Expecting NullPointerException"); } catch (final NullPointerException ex) { assertEquals("The validated object is null", ex.getMessage()); } coll.set(1, null); try { Validate.noNullElements(coll); fail("Expecting IllegalArgumentException"); } catch (final IllegalArgumentException ex) { assertEquals("The validated collection contains null element at index: 1", ex.getMessage()); } coll.set(1, "b"); final List<String> test = Validate.noNullElements(coll); assertSame(coll, test); }
@Test public void testNoNullElementsArray1() { String[] array = new String[]{"a", "b"}; Validate.noNullElements(array); try { Validate.noNullElements((Object[]) null); fail("Expecting NullPointerException"); } catch (final NullPointerException ex) { assertEquals("The validated object is null", ex.getMessage()); } array[1] = null; try { Validate.noNullElements(array); fail("Expecting IllegalArgumentException"); } catch (final IllegalArgumentException ex) { assertEquals("The validated array contains null element at index: 1", ex.getMessage()); } array = new String[]{"a", "b"}; final String[] test = Validate.noNullElements(array); assertSame(array, test); }
@Test public void testNoNullElementsArray2() { String[] array = new String[]{"a", "b"}; Validate.noNullElements(array, "MSG"); try { Validate.noNullElements((Object[]) null, "MSG"); fail("Expecting NullPointerException"); } catch (final NullPointerException ex) { assertEquals("The validated object is null", ex.getMessage()); } array[1] = null; try { Validate.noNullElements(array, "MSG"); fail("Expecting IllegalArgumentException"); } catch (final IllegalArgumentException ex) { assertEquals("MSG", ex.getMessage()); } array = new String[]{"a", "b"}; final String[] test = Validate.noNullElements(array, "Message"); assertSame(array, test); }
/** * Creates a new {@link Color} combining the provided {@link Color} * objects, their summation and average is taken into effect * to properly mix the colors together. * * @param colors The colors to mix * @return The final output mixed color */ public static Color mixColors(Color... colors) { Validate.noNullElements(colors, "No null colors allowed!"); checkArgument(colors.length > 0, "Cannot have an empty color array!"); if (colors.length == 1) { return colors[0]; } int red = colors[0].getRed(); int green = colors[0].getGreen(); int blue = colors[0].getBlue(); for (int i = 1; i < colors.length; i++) { red += colors[i].getRed(); green += colors[i].getGreen(); blue += colors[i].getBlue(); } int averageRed = Math.round((float) red / colors.length); int averageGreen = Math.round((float) green / colors.length); int averageBlue = Math.round((float) blue / colors.length); return ofRgb(averageRed, averageGreen, averageBlue); }
/** * Sets the resource providers for this server */ public void setResourceProviders(Collection<IResourceProvider> theProviders) { Validate.noNullElements(theProviders, "theProviders must not contain any null elements"); myResourceProviders.clear(); if (theProviders != null) { myResourceProviders.addAll(theProviders); } }
/** * Sets the non-resource specific providers which implement method calls on this server * * @see #setResourceProviders(Collection) */ public void setProviders(Object... theProviders) { Validate.noNullElements(theProviders, "theProviders must not contain any null elements"); myPlainProviders.clear(); if (theProviders != null) { myPlainProviders.addAll(Arrays.asList(theProviders)); } }
/** * Sets (or clears) the list of interceptors * * @param theInterceptors The list of interceptors (may be null) */ public void setInterceptors(IServerInterceptor... theInterceptors) { Validate.noNullElements(theInterceptors, "theInterceptors must not contain any null elements"); myInterceptors.clear(); if (theInterceptors != null) { myInterceptors.addAll(Arrays.asList(theInterceptors)); } }
/** * Register a group of providers. These could be Resource Providers (classes implementing {@link IResourceProvider}) or "plain" providers, or a mixture of the two. * * @param theProviders a {@code Collection} of theProviders. The parameter could be null or an empty {@code Collection} */ public void registerProviders(Object... theProviders) { Validate.noNullElements(theProviders); registerProviders(Arrays.asList(theProviders)); }
@Test public void testValidIndex_array() { final Object[] array = new Object[2]; Validate.validIndex(array, 0); Validate.validIndex(array, 1); try { Validate.validIndex(array, -1); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("The validated array index is invalid: -1", ex.getMessage()); } try { Validate.validIndex(array, 2); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("The validated array index is invalid: 2", ex.getMessage()); } final String[] strArray = new String[]{"Hi"}; final String[] test = Validate.noNullElements(strArray); assertSame(strArray, test); }
@Test public void testValidIndex_withMessage_array() { final Object[] array = new Object[2]; Validate.validIndex(array, 0, "Broken: "); Validate.validIndex(array, 1, "Broken: "); try { Validate.validIndex(array, -1, "Broken: "); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("Broken: ", ex.getMessage()); } try { Validate.validIndex(array, 2, "Broken: "); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("Broken: ", ex.getMessage()); } final String[] strArray = new String[]{"Hi"}; final String[] test = Validate.noNullElements(strArray, "Message"); assertSame(strArray, test); }