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

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

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

Validate.noNullElements介绍

[英]Validate that the specified argument iterable is neither null nor contains any elements that are null; otherwise throwing an exception.

Validate.noNullElements(myCollection);

If the iterable is null, then the message in the exception is "The validated object is null".

If the array has a null element, then the message in the exception is "The validated iterable contains null element at index: " followed by the index.
[中]验证指定的参数iterable既不是null,也不包含任何null元素;否则将引发异常。

Validate.noNullElements(myCollection);

如果iterable为null,则异常中的消息为“已验证对象为null”。
如果数组有null元素,则异常中的消息是“已验证的iterable包含索引处的null元素:”后跟索引。

代码示例

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

/**
 * <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 &quot;The validated object is null&quot;.</p>
 *
 * <p>If the array has a {@code null} element, then the message in the
 * exception is &quot;The validated array contains null element at index:
 * &quot; 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);
}

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

/**
 * <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 &quot;The validated object is null&quot;.</p>
 *
 * <p>If the array has a {@code null} element, then the message in the
 * exception is &quot;The validated iterable contains null element at index:
 * &quot; 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);
}

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

/**
 * 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;
}

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

/**
 * <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));
}

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

/**
 * 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;
}

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

/**
 * 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);
}

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

/**
 * <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;
}

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

/**
 * 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;
}

代码示例来源:origin: SpongePowered/SpongeAPI

/**
 * 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);
}

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

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

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

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

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

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

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

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

代码示例来源:origin: SpongePowered/SpongeAPI

/**
 * 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);
}

代码示例来源:origin: jamesagnew/hapi-fhir

/**
 * 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);
  }
}

代码示例来源:origin: jamesagnew/hapi-fhir

/**
 * 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));
  }
}

代码示例来源:origin: jamesagnew/hapi-fhir

/**
 * 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));
  }
}

代码示例来源:origin: jamesagnew/hapi-fhir

/**
 * 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));
}

代码示例来源: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);
}

相关文章