/** * <p>Validates that the index is within the bounds of the argument * collection; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myCollection, 2);</pre> * * <p>If the index is invalid, then the message of the exception * is "The validated collection index is invalid: " * followed by the index.</p> * * @param <T> the collection type * @param collection the collection to check, validated not null by this method * @param index the index to check * @return the validated collection (never {@code null} for method chaining) * @throws NullPointerException if the collection is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(Collection, int, String, Object...) * * @since 3.0 */ public static <T extends Collection<?>> T validIndex(final T collection, final int index) { return validIndex(collection, index, DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE, Integer.valueOf(index)); }
/** * <p>Validates that the index is within the bounds of the argument * array; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myArray, 2);</pre> * * <p>If the array is {@code null}, then the message of the exception * is "The validated object is null".</p> * * <p>If the index is invalid, then the message of the exception is * "The validated array index is invalid: " followed by the * index.</p> * * @param <T> the array type * @param array the array to check, validated not null by this method * @param index the index to check * @return the validated array (never {@code null} for method chaining) * @throws NullPointerException if the array is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(Object[], int, String, Object...) * * @since 3.0 */ public static <T> T[] validIndex(final T[] array, final int index) { return validIndex(array, index, DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE, Integer.valueOf(index)); }
/** * <p>Validates that the index is within the bounds of the argument * character sequence; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myStr, 2);</pre> * * <p>If the character sequence is {@code null}, then the message * of the exception is "The validated object is * null".</p> * * <p>If the index is invalid, then the message of the exception * is "The validated character sequence index is invalid: " * followed by the index.</p> * * @param <T> the character sequence type * @param chars the character sequence to check, validated not null by this method * @param index the index to check * @return the validated character sequence (never {@code null} for method chaining) * @throws NullPointerException if the character sequence is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(CharSequence, int, String, Object...) * * @since 3.0 */ public static <T extends CharSequence> T validIndex(final T chars, final int index) { return validIndex(chars, index, DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE, Integer.valueOf(index)); }
@Test public void testValidIndex_withMessage_collection() { final Collection<String> coll = new ArrayList<>(); coll.add(null); coll.add(null); Validate.validIndex(coll, 0, "Broken: "); Validate.validIndex(coll, 1, "Broken: "); try { Validate.validIndex(coll, -1, "Broken: "); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("Broken: ", ex.getMessage()); } try { Validate.validIndex(coll, 2, "Broken: "); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("Broken: ", ex.getMessage()); } final List<String> strColl = Arrays.asList("Hi"); final List<String> test = Validate.validIndex(strColl, 0, "Message"); assertSame(strColl, test); }
@Test public void testValidIndex_collection() { final Collection<String> coll = new ArrayList<>(); coll.add(null); coll.add(null); Validate.validIndex(coll, 0); Validate.validIndex(coll, 1); try { Validate.validIndex(coll, -1); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("The validated collection index is invalid: -1", ex.getMessage()); } try { Validate.validIndex(coll, 2); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("The validated collection index is invalid: 2", ex.getMessage()); } final List<String> strColl = Arrays.asList("Hi"); final List<String> test = Validate.validIndex(strColl, 0); assertSame(strColl, test); }
@Test public void testValidIndex_withMessage_charSequence() { final CharSequence str = "Hi"; Validate.validIndex(str, 0, "Broken: "); Validate.validIndex(str, 1, "Broken: "); try { Validate.validIndex(str, -1, "Broken: "); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("Broken: ", ex.getMessage()); } try { Validate.validIndex(str, 2, "Broken: "); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("Broken: ", ex.getMessage()); } final String input = "Hi"; final String test = Validate.validIndex(input, 0, "Message"); assertSame(input, test); }
@Test public void testValidIndex_charSequence() { final CharSequence str = "Hi"; Validate.validIndex(str, 0); Validate.validIndex(str, 1); try { Validate.validIndex(str, -1); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("The validated character sequence index is invalid: -1", ex.getMessage()); } try { Validate.validIndex(str, 2); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { assertEquals("The validated character sequence index is invalid: 2", ex.getMessage()); } final String input = "Hi"; final String test = Validate.validIndex(input, 0); assertSame(input, test); }
@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); }
/** * <p>Validates that the index is within the bounds of the argument * collection; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myCollection, 2);</pre> * * <p>If the index is invalid, then the message of the exception * is "The validated collection index is invalid: " * followed by the index.</p> * * @param <T> the collection type * @param collection the collection to check, validated not null by this method * @param index the index to check * @return the validated collection (never {@code null} for method chaining) * @throws NullPointerException if the collection is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(Collection, int, String, Object...) * * @since 3.0 */ public static <T extends Collection<?>> T validIndex(final T collection, final int index) { return validIndex(collection, index, DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE, Integer.valueOf(index)); }
/** * <p>Validates that the index is within the bounds of the argument * collection; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myCollection, 2);</pre> * * <p>If the index is invalid, then the message of the exception * is "The validated collection index is invalid: " * followed by the index.</p> * * @param <T> the collection type * @param collection the collection to check, validated not null by this method * @param index the index to check * @return the validated collection (never {@code null} for method chaining) * @throws NullPointerException if the collection is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(Collection, int, String, Object...) * * @since 3.0 */ public static <T extends Collection<?>> T validIndex(final T collection, final int index) { return validIndex(collection, index, DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE, Integer.valueOf(index)); }
/** * <p>Validates that the index is within the bounds of the argument * collection; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myCollection, 2);</pre> * * <p>If the index is invalid, then the message of the exception * is "The validated collection index is invalid: " * followed by the index.</p> * * @param <T> the collection type * @param collection the collection to check, validated not null by this method * @param index the index to check * @return the validated collection (never {@code null} for method chaining) * @throws NullPointerException if the collection is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(Collection, int, String, Object...) * * @since 3.0 */ public static <T extends Collection<?>> T validIndex(final T collection, final int index) { return validIndex(collection, index, DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE, Integer.valueOf(index)); }
/** * <p>Validates that the index is within the bounds of the argument * array; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myArray, 2);</pre> * * <p>If the array is {@code null}, then the message of the exception * is "The validated object is null".</p> * * <p>If the index is invalid, then the message of the exception is * "The validated array index is invalid: " followed by the * index.</p> * * @param <T> the array type * @param array the array to check, validated not null by this method * @param index the index to check * @return the validated array (never {@code null} for method chaining) * @throws NullPointerException if the array is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(Object[], int, String, Object...) * * @since 3.0 */ public static <T> T[] validIndex(final T[] array, final int index) { return validIndex(array, index, DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE, Integer.valueOf(index)); }
/** * <p>Validates that the index is within the bounds of the argument * array; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myArray, 2);</pre> * * <p>If the array is {@code null}, then the message of the exception * is "The validated object is null".</p> * * <p>If the index is invalid, then the message of the exception is * "The validated array index is invalid: " followed by the * index.</p> * * @param <T> the array type * @param array the array to check, validated not null by this method * @param index the index to check * @return the validated array (never {@code null} for method chaining) * @throws NullPointerException if the array is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(Object[], int, String, Object...) * * @since 3.0 */ public static <T> T[] validIndex(final T[] array, final int index) { return validIndex(array, index, DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE, Integer.valueOf(index)); }
/** * <p>Validates that the index is within the bounds of the argument * array; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myArray, 2);</pre> * * <p>If the array is {@code null}, then the message of the exception * is "The validated object is null".</p> * * <p>If the index is invalid, then the message of the exception is * "The validated array index is invalid: " followed by the * index.</p> * * @param <T> the array type * @param array the array to check, validated not null by this method * @param index the index to check * @return the validated array (never {@code null} for method chaining) * @throws NullPointerException if the array is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(Object[], int, String, Object...) * * @since 3.0 */ public static <T> T[] validIndex(final T[] array, final int index) { return validIndex(array, index, DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE, Integer.valueOf(index)); }
/** * <p>Validates that the index is within the bounds of the argument * character sequence; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myStr, 2);</pre> * * <p>If the character sequence is {@code null}, then the message * of the exception is "The validated object is * null".</p> * * <p>If the index is invalid, then the message of the exception * is "The validated character sequence index is invalid: " * followed by the index.</p> * * @param <T> the character sequence type * @param chars the character sequence to check, validated not null by this method * @param index the index to check * @return the validated character sequence (never {@code null} for method chaining) * @throws NullPointerException if the character sequence is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(CharSequence, int, String, Object...) * * @since 3.0 */ public static <T extends CharSequence> T validIndex(final T chars, final int index) { return validIndex(chars, index, DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE, Integer.valueOf(index)); }
/** * <p>Validates that the index is within the bounds of the argument * character sequence; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myStr, 2);</pre> * * <p>If the character sequence is {@code null}, then the message * of the exception is "The validated object is * null".</p> * * <p>If the index is invalid, then the message of the exception * is "The validated character sequence index is invalid: " * followed by the index.</p> * * @param <T> the character sequence type * @param chars the character sequence to check, validated not null by this method * @param index the index to check * @return the validated character sequence (never {@code null} for method chaining) * @throws NullPointerException if the character sequence is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(CharSequence, int, String, Object...) * * @since 3.0 */ public static <T extends CharSequence> T validIndex(final T chars, final int index) { return validIndex(chars, index, DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE, Integer.valueOf(index)); }
/** * <p>Validates that the index is within the bounds of the argument * character sequence; otherwise throwing an exception.</p> * * <pre>Validate.validIndex(myStr, 2);</pre> * * <p>If the character sequence is {@code null}, then the message * of the exception is "The validated object is * null".</p> * * <p>If the index is invalid, then the message of the exception * is "The validated character sequence index is invalid: " * followed by the index.</p> * * @param <T> the character sequence type * @param chars the character sequence to check, validated not null by this method * @param index the index to check * @return the validated character sequence (never {@code null} for method chaining) * @throws NullPointerException if the character sequence is {@code null} * @throws IndexOutOfBoundsException if the index is invalid * @see #validIndex(CharSequence, int, String, Object...) * * @since 3.0 */ public static <T extends CharSequence> T validIndex(final T chars, final int index) { return validIndex(chars, index, DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE, Integer.valueOf(index)); }
public void setProperties(float... properties) { Validate.validIndex(ArrayUtils.toObject(properties), 4); this.properties = properties; }