org.springframework.util.Assert.nullSafeGet()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(14.0k)|赞(0)|评价(0)|浏览(177)

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

Assert.nullSafeGet介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert a boolean expression, throwing an {@code IllegalArgumentException}
 * if the expression evaluates to {@code false}.
 * <pre class="code">
 * Assert.isTrue(i &gt; 0, () -&gt; "The value '" + i + "' must be greater than zero");
 * </pre>
 * @param expression a boolean expression
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if {@code expression} is {@code false}
 * @since 5.0
 */
public static void isTrue(boolean expression, Supplier<String> messageSupplier) {
  if (!expression) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert a boolean expression, throwing an {@code IllegalStateException}
 * if the expression evaluates to {@code false}.
 * <p>Call {@link #isTrue} if you wish to throw an {@code IllegalArgumentException}
 * on an assertion failure.
 * <pre class="code">
 * Assert.state(id == null,
 *     () -&gt; "ID for " + entity.getName() + " must not already be initialized");
 * </pre>
 * @param expression a boolean expression
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalStateException if {@code expression} is {@code false}
 * @since 5.0
 */
public static void state(boolean expression, Supplier<String> messageSupplier) {
  if (!expression) {
    throw new IllegalStateException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert that an object is {@code null}.
 * <pre class="code">
 * Assert.isNull(value, () -&gt; "The value '" + value + "' must be null");
 * </pre>
 * @param object the object to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the object is not {@code null}
 * @since 5.0
 */
public static void isNull(@Nullable Object object, Supplier<String> messageSupplier) {
  if (object != null) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert that an object is not {@code null}.
 * <pre class="code">
 * Assert.notNull(clazz, () -&gt; "The class '" + clazz.getName() + "' must not be null");
 * </pre>
 * @param object the object to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the object is {@code null}
 * @since 5.0
 */
public static void notNull(@Nullable Object object, Supplier<String> messageSupplier) {
  if (object == null) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert that a Map contains entries; that is, it must not be {@code null}
 * and must contain at least one entry.
 * <pre class="code">
 * Assert.notEmpty(map, () -&gt; "The " + mapType + " map must contain entries");
 * </pre>
 * @param map the map to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the map is {@code null} or contains no entries
 * @since 5.0
 */
public static void notEmpty(@Nullable Map<?, ?> map, Supplier<String> messageSupplier) {
  if (CollectionUtils.isEmpty(map)) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert that the given String contains valid text content; that is, it must not
 * be {@code null} and must contain at least one non-whitespace character.
 * <pre class="code">
 * Assert.hasText(name, () -&gt; "Name for account '" + account.getId() + "' must not be empty");
 * </pre>
 * @param text the String to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the text does not contain valid text content
 * @since 5.0
 * @see StringUtils#hasText
 */
public static void hasText(@Nullable String text, Supplier<String> messageSupplier) {
  if (!StringUtils.hasText(text)) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert that a collection contains elements; that is, it must not be
 * {@code null} and must contain at least one element.
 * <pre class="code">
 * Assert.notEmpty(collection, () -&gt; "The " + collectionType + " collection must contain elements");
 * </pre>
 * @param collection the collection to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the collection is {@code null} or
 * contains no elements
 * @since 5.0
 */
public static void notEmpty(@Nullable Collection<?> collection, Supplier<String> messageSupplier) {
  if (CollectionUtils.isEmpty(collection)) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert that an array contains elements; that is, it must not be
 * {@code null} and must contain at least one element.
 * <pre class="code">
 * Assert.notEmpty(array, () -&gt; "The " + arrayType + " array must contain elements");
 * </pre>
 * @param array the array to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the object array is {@code null} or contains no elements
 * @since 5.0
 */
public static void notEmpty(@Nullable Object[] array, Supplier<String> messageSupplier) {
  if (ObjectUtils.isEmpty(array)) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert that an array contains no {@code null} elements.
 * <p>Note: Does not complain if the array is empty!
 * <pre class="code">
 * Assert.noNullElements(array, () -&gt; "The " + arrayType + " array must contain non-null elements");
 * </pre>
 * @param array the array to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the object array contains a {@code null} element
 * @since 5.0
 */
public static void noNullElements(@Nullable Object[] array, Supplier<String> messageSupplier) {
  if (array != null) {
    for (Object element : array) {
      if (element == null) {
        throw new IllegalArgumentException(nullSafeGet(messageSupplier));
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert that the given String is not empty; that is,
 * it must not be {@code null} and not the empty String.
 * <pre class="code">
 * Assert.hasLength(name, () -&gt; "Name for account '" + account.getId() + "' must not be empty");
 * </pre>
 * @param text the String to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the text is empty
 * @since 5.0
 * @see StringUtils#hasLength
 */
public static void hasLength(@Nullable String text, Supplier<String> messageSupplier) {
  if (!StringUtils.hasLength(text)) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert that the given text does not contain the given substring.
 * <pre class="code">
 * Assert.doesNotContain(name, forbidden, () -&gt; "Name must not contain '" + forbidden + "'");
 * </pre>
 * @param textToSearch the text to search
 * @param substring the substring to find within the text
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the text contains the substring
 * @since 5.0
 */
public static void doesNotContain(@Nullable String textToSearch, String substring, Supplier<String> messageSupplier) {
  if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
      textToSearch.contains(substring)) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Assert a boolean expression, throwing an {@code IllegalArgumentException}
 * if the expression evaluates to {@code false}.
 * <pre class="code">
 * Assert.isTrue(i &gt; 0, () -&gt; "The value '" + i + "' must be greater than zero");
 * </pre>
 * @param expression a boolean expression
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if {@code expression} is {@code false}
 * @since 5.0
 */
public static void isTrue(boolean expression, Supplier<String> messageSupplier) {
  if (!expression) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
 * <pre class="code">
 * Assert.isAssignable(Number.class, myClass, () -&gt; "Processing " + myAttributeName + ":");
 * </pre>
 * @param superType the super type to check against
 * @param subType the sub type to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails. See {@link #isAssignable(Class, Class, String)} for details.
 * @throws IllegalArgumentException if the classes are not assignable
 * @since 5.0
 */
public static void isAssignable(Class<?> superType, @Nullable Class<?> subType, Supplier<String> messageSupplier) {
  notNull(superType, "Super type to check against must not be null");
  if (subType == null || !superType.isAssignableFrom(subType)) {
    assignableCheckFailed(superType, subType, nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Assert that the provided object is an instance of the provided class.
 * <pre class="code">
 * Assert.instanceOf(Foo.class, foo, () -&gt; "Processing " + Foo.class.getSimpleName() + ":");
 * </pre>
 * @param type the type to check against
 * @param obj the object to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails. See {@link #isInstanceOf(Class, Object, String)} for details.
 * @throws IllegalArgumentException if the object is not an instance of type
 * @since 5.0
 */
public static void isInstanceOf(Class<?> type, @Nullable Object obj, Supplier<String> messageSupplier) {
  notNull(type, "Type to check against must not be null");
  if (!type.isInstance(obj)) {
    instanceCheckFailed(type, obj, nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Assert that an object is {@code null}.
 * <pre class="code">
 * Assert.isNull(value, () -&gt; "The value '" + value + "' must be null");
 * </pre>
 * @param object the object to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the object is not {@code null}
 * @since 5.0
 */
public static void isNull(@Nullable Object object, Supplier<String> messageSupplier) {
  if (object != null) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Assert that an object is not {@code null}.
 * <pre class="code">
 * Assert.notNull(clazz, () -&gt; "The class '" + clazz.getName() + "' must not be null");
 * </pre>
 * @param object the object to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the object is {@code null}
 * @since 5.0
 */
public static void notNull(@Nullable Object object, Supplier<String> messageSupplier) {
  if (object == null) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Assert that the given String is not empty; that is,
 * it must not be {@code null} and not the empty String.
 * <pre class="code">
 * Assert.hasLength(name, () -&gt; "Name for account '" + account.getId() + "' must not be empty");
 * </pre>
 * @param text the String to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the text is empty
 * @since 5.0
 * @see StringUtils#hasLength
 */
public static void hasLength(@Nullable String text, Supplier<String> messageSupplier) {
  if (!StringUtils.hasLength(text)) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Assert that an array contains elements; that is, it must not be
 * {@code null} and must contain at least one element.
 * <pre class="code">
 * Assert.notEmpty(array, () -&gt; "The " + arrayType + " array must contain elements");
 * </pre>
 * @param array the array to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the object array is {@code null} or contains no elements
 * @since 5.0
 */
public static void notEmpty(@Nullable Object[] array, Supplier<String> messageSupplier) {
  if (ObjectUtils.isEmpty(array)) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Assert that a Map contains entries; that is, it must not be {@code null}
 * and must contain at least one entry.
 * <pre class="code">
 * Assert.notEmpty(map, () -&gt; "The " + mapType + " map must contain entries");
 * </pre>
 * @param map the map to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails
 * @throws IllegalArgumentException if the map is {@code null} or contains no entries
 * @since 5.0
 */
public static void notEmpty(@Nullable Map<?, ?> map, Supplier<String> messageSupplier) {
  if (CollectionUtils.isEmpty(map)) {
    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
  }
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Assert that the provided object is an instance of the provided class.
 * <pre class="code">
 * Assert.instanceOf(Foo.class, foo, () -&gt; "Processing " + Foo.class.getSimpleName() + ":");
 * </pre>
 * @param type the type to check against
 * @param obj the object to check
 * @param messageSupplier a supplier for the exception message to use if the
 * assertion fails. See {@link #isInstanceOf(Class, Object, String)} for details.
 * @throws IllegalArgumentException if the object is not an instance of type
 * @since 5.0
 */
public static void isInstanceOf(Class<?> type, @Nullable Object obj, Supplier<String> messageSupplier) {
  notNull(type, "Type to check against must not be null");
  if (!type.isInstance(obj)) {
    instanceCheckFailed(type, obj, nullSafeGet(messageSupplier));
  }
}

相关文章