org.apache.commons.lang3.Validate.validIndex()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(17.4k)|赞(0)|评价(0)|浏览(98)

本文整理了Java中org.apache.commons.lang3.Validate.validIndex()方法的一些代码示例,展示了Validate.validIndex()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Validate.validIndex()方法的具体详情如下:
包路径:org.apache.commons.lang3.Validate
类名称:Validate
方法名:validIndex

Validate.validIndex介绍

[英]Validates that the index is within the bounds of the argument character sequence; otherwise throwing an exception.

Validate.validIndex(myStr, 2);

If the character sequence is null, then the message of the exception is "The validated object is null".

If the index is invalid, then the message of the exception is "The validated character sequence index is invalid: " followed by the index.
[中]验证索引是否在参数字符序列的范围内;否则将引发异常。

Validate.validIndex(myStr, 2);

如果字符序列为空,则异常消息为“已验证对象为空”。
如果索引无效,则异常消息为“验证的字符序列索引无效:”后跟索引。

代码示例

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <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 &quot;The validated collection index is invalid: &quot;
 * 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));
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <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 &quot;The validated object is null&quot;.</p>
 *
 * <p>If the index is invalid, then the message of the exception is
 * &quot;The validated array index is invalid: &quot; 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));
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <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 &quot;The validated object is
 * null&quot;.</p>
 *
 * <p>If the index is invalid, then the message of the exception
 * is &quot;The validated character sequence index is invalid: &quot;
 * 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));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@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);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@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);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@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);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@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);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@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);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@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);
}

代码示例来源:origin: de.knightsoft-net/gwt-commons-lang3

/**
 * <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 &quot;The validated collection index is invalid: &quot;
 * 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));
}

代码示例来源:origin: io.virtdata/virtdata-lib-realer

/**
 * <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 &quot;The validated collection index is invalid: &quot;
 * 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));
}

代码示例来源:origin: io.virtdata/virtdata-lib-curves4

/**
 * <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 &quot;The validated collection index is invalid: &quot;
 * 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));
}

代码示例来源:origin: de.knightsoft-net/gwt-commons-lang3

/**
 * <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 &quot;The validated object is null&quot;.</p>
 *
 * <p>If the index is invalid, then the message of the exception is
 * &quot;The validated array index is invalid: &quot; 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));
}

代码示例来源:origin: io.virtdata/virtdata-lib-realer

/**
 * <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 &quot;The validated object is null&quot;.</p>
 *
 * <p>If the index is invalid, then the message of the exception is
 * &quot;The validated array index is invalid: &quot; 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));
}

代码示例来源:origin: io.virtdata/virtdata-lib-curves4

/**
 * <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 &quot;The validated object is null&quot;.</p>
 *
 * <p>If the index is invalid, then the message of the exception is
 * &quot;The validated array index is invalid: &quot; 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));
}

代码示例来源:origin: io.virtdata/virtdata-lib-realer

/**
 * <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 &quot;The validated object is
 * null&quot;.</p>
 *
 * <p>If the index is invalid, then the message of the exception
 * is &quot;The validated character sequence index is invalid: &quot;
 * 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));
}

代码示例来源:origin: de.knightsoft-net/gwt-commons-lang3

/**
 * <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 &quot;The validated object is
 * null&quot;.</p>
 *
 * <p>If the index is invalid, then the message of the exception
 * is &quot;The validated character sequence index is invalid: &quot;
 * 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));
}

代码示例来源:origin: io.virtdata/virtdata-lib-curves4

/**
 * <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 &quot;The validated object is
 * null&quot;.</p>
 *
 * <p>If the index is invalid, then the message of the exception
 * is &quot;The validated character sequence index is invalid: &quot;
 * 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));
}

代码示例来源:origin: NanamiArihara/FoodCraft-Reloaded

public void setProperties(float... properties) {
  Validate.validIndex(ArrayUtils.toObject(properties), 4);
  this.properties = properties;
}

相关文章