首页

基于apahce的commons-lang的Validate对常见的集合及基本数据类型进行有效性校验判断处理

标签:Validate,校验器,apache,commons-lang     发布时间:2018-02-10   

一、前言

关于apachecommons-lang包中org.apache.commons.lang3.Validate数据有效性校验器分析,其主要对表达式expression规则校验、集合map的非空notEmpty校验判断、数组元素的非空判断noNullElements、数组是否Wie第一个元素的判断validIndex、正则表达式的匹配判断matchesPattern、是否为类的实例化判断isInstanceOf等

二、源码说明

package org.apache.commons.lang3;@b@@b@import java.util.Collection;@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.regex.Pattern;@b@@b@public class Validate@b@{@b@  private static final String DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE = "The value %s is not in the specified exclusive range of %s to %s";@b@  private static final String DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE = "The value %s is not in the specified inclusive range of %s to %s";@b@  private static final String DEFAULT_MATCHES_PATTERN_EX = "The string %s does not match the pattern %s";@b@  private static final String DEFAULT_IS_NULL_EX_MESSAGE = "The validated object is null";@b@  private static final String DEFAULT_IS_TRUE_EX_MESSAGE = "The validated expression is false";@b@  private static final String DEFAULT_NO_NULL_ELEMENTS_ARRAY_EX_MESSAGE = "The validated array contains null element at index: %d";@b@  private static final String DEFAULT_NO_NULL_ELEMENTS_COLLECTION_EX_MESSAGE = "The validated collection contains null element at index: %d";@b@  private static final String DEFAULT_NOT_BLANK_EX_MESSAGE = "The validated character sequence is blank";@b@  private static final String DEFAULT_NOT_EMPTY_ARRAY_EX_MESSAGE = "The validated array is empty";@b@  private static final String DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EX_MESSAGE = "The validated character sequence is empty";@b@  private static final String DEFAULT_NOT_EMPTY_COLLECTION_EX_MESSAGE = "The validated collection is empty";@b@  private static final String DEFAULT_NOT_EMPTY_MAP_EX_MESSAGE = "The validated map is empty";@b@  private static final String DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE = "The validated array index is invalid: %d";@b@  private static final String DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE = "The validated character sequence index is invalid: %d";@b@  private static final String DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE = "The validated collection index is invalid: %d";@b@  private static final String DEFAULT_VALID_STATE_EX_MESSAGE = "The validated state is false";@b@  private static final String DEFAULT_IS_ASSIGNABLE_EX_MESSAGE = "Cannot assign a %s to a %s";@b@  private static final String DEFAULT_IS_INSTANCE_OF_EX_MESSAGE = "Expected type: %s, actual: %s";@b@@b@  public static void isTrue(boolean expression, String message, long value)@b@  {@b@    if (!(expression))@b@      throw new IllegalArgumentException(String.format(message, new Object[] { Long.valueOf(value) }));@b@  }@b@@b@  public static void isTrue(boolean expression, String message, double value)@b@  {@b@    if (!(expression))@b@      throw new IllegalArgumentException(String.format(message, new Object[] { Double.valueOf(value) }));@b@  }@b@@b@  public static void isTrue(boolean expression, String message, Object[] values)@b@  {@b@    if (!(expression))@b@      throw new IllegalArgumentException(String.format(message, values));@b@  }@b@@b@  public static void isTrue(boolean expression)@b@  {@b@    if (!(expression))@b@      throw new IllegalArgumentException("The validated expression is false");@b@  }@b@@b@  public static <T> T notNull(T object)@b@  {@b@    return notNull(object, "The validated object is null", new Object[0]);@b@  }@b@@b@  public static <T> T notNull(T object, String message, Object[] values)@b@  {@b@    if (object == null)@b@      throw new NullPointerException(String.format(message, values));@b@@b@    return object;@b@  }@b@@b@  public static <T> T[] notEmpty(T[] array, String message, Object[] values)@b@  {@b@    if (array == null)@b@      throw new NullPointerException(String.format(message, values));@b@@b@    if (array.length == 0)@b@      throw new IllegalArgumentException(String.format(message, values));@b@@b@    return array;@b@  }@b@@b@  public static <T> T[] notEmpty(T[] array)@b@  {@b@    return notEmpty(array, "The validated array is empty", new Object[0]);@b@  }@b@@b@  public static <T extends Collection<?>> T notEmpty(T collection, String message, Object[] values)@b@  {@b@    if (collection == null)@b@      throw new NullPointerException(String.format(message, values));@b@@b@    if (collection.isEmpty())@b@      throw new IllegalArgumentException(String.format(message, values));@b@@b@    return collection;@b@  }@b@@b@  public static <T extends Collection<?>> T notEmpty(T collection)@b@  {@b@    return notEmpty(collection, "The validated collection is empty", new Object[0]);@b@  }@b@@b@  public static <T extends Map<?, ?>> T notEmpty(T map, String message, Object[] values)@b@  {@b@    if (map == null)@b@      throw new NullPointerException(String.format(message, values));@b@@b@    if (map.isEmpty())@b@      throw new IllegalArgumentException(String.format(message, values));@b@@b@    return map;@b@  }@b@@b@  public static <T extends Map<?, ?>> T notEmpty(T map)@b@  {@b@    return notEmpty(map, "The validated map is empty", new Object[0]);@b@  }@b@@b@  public static <T extends CharSequence> T notEmpty(T chars, String message, Object[] values)@b@  {@b@    if (chars == null)@b@      throw new NullPointerException(String.format(message, values));@b@@b@    if (chars.length() == 0)@b@      throw new IllegalArgumentException(String.format(message, values));@b@@b@    return chars;@b@  }@b@@b@  public static <T extends CharSequence> T notEmpty(T chars)@b@  {@b@    return notEmpty(chars, "The validated character sequence is empty", new Object[0]);@b@  }@b@@b@  public static <T extends CharSequence> T notBlank(T chars, String message, Object[] values)@b@  {@b@    if (chars == null)@b@      throw new NullPointerException(String.format(message, values));@b@@b@    if (StringUtils.isBlank(chars))@b@      throw new IllegalArgumentException(String.format(message, values));@b@@b@    return chars;@b@  }@b@@b@  public static <T extends CharSequence> T notBlank(T chars)@b@  {@b@    return notBlank(chars, "The validated character sequence is blank", new Object[0]);@b@  }@b@@b@  public static <T> T[] noNullElements(T[] array, String message, Object[] values)@b@  {@b@    notNull(array);@b@    for (int i = 0; i < array.length; ++i)@b@      if (array[i] == null) {@b@        Object[] values2 = ArrayUtils.add(values, Integer.valueOf(i));@b@        throw new IllegalArgumentException(String.format(message, values2));@b@      }@b@@b@    return array;@b@  }@b@@b@  public static <T> T[] noNullElements(T[] array)@b@  {@b@    return noNullElements(array, "The validated array contains null element at index: %d", new Object[0]);@b@  }@b@@b@  public static <T extends Iterable<?>> T noNullElements(T iterable, String message, Object[] values)@b@  {@b@    notNull(iterable);@b@    int i = 0;@b@    for (Iterator it = iterable.iterator(); it.hasNext(); ++i)@b@      if (it.next() == null) {@b@        Object[] values2 = ArrayUtils.addAll(values, new Object[] { Integer.valueOf(i) });@b@        throw new IllegalArgumentException(String.format(message, values2));@b@      }@b@@b@    return iterable;@b@  }@b@@b@  public static <T extends Iterable<?>> T noNullElements(T iterable)@b@  {@b@    return noNullElements(iterable, "The validated collection contains null element at index: %d", new Object[0]);@b@  }@b@@b@  public static <T> T[] validIndex(T[] array, int index, String message, Object[] values)@b@  {@b@    notNull(array);@b@    if ((index < 0) || (index >= array.length))@b@      throw new IndexOutOfBoundsException(String.format(message, values));@b@@b@    return array;@b@  }@b@@b@  public static <T> T[] validIndex(T[] array, int index)@b@  {@b@    return validIndex(array, index, "The validated array index is invalid: %d", new Object[] { Integer.valueOf(index) });@b@  }@b@@b@  public static <T extends Collection<?>> T validIndex(T collection, int index, String message, Object[] values)@b@  {@b@    notNull(collection);@b@    if ((index < 0) || (index >= collection.size()))@b@      throw new IndexOutOfBoundsException(String.format(message, values));@b@@b@    return collection;@b@  }@b@@b@  public static <T extends Collection<?>> T validIndex(T collection, int index)@b@  {@b@    return validIndex(collection, index, "The validated collection index is invalid: %d", new Object[] { Integer.valueOf(index) });@b@  }@b@@b@  public static <T extends CharSequence> T validIndex(T chars, int index, String message, Object[] values)@b@  {@b@    notNull(chars);@b@    if ((index < 0) || (index >= chars.length()))@b@      throw new IndexOutOfBoundsException(String.format(message, values));@b@@b@    return chars;@b@  }@b@@b@  public static <T extends CharSequence> T validIndex(T chars, int index)@b@  {@b@    return validIndex(chars, index, "The validated character sequence index is invalid: %d", new Object[] { Integer.valueOf(index) });@b@  }@b@@b@  public static void validState(boolean expression)@b@  {@b@    if (!(expression))@b@      throw new IllegalStateException("The validated state is false");@b@  }@b@@b@  public static void validState(boolean expression, String message, Object[] values)@b@  {@b@    if (!(expression))@b@      throw new IllegalStateException(String.format(message, values));@b@  }@b@@b@  public static void matchesPattern(CharSequence input, String pattern)@b@  {@b@    if (!(Pattern.matches(pattern, input)))@b@      throw new IllegalArgumentException(String.format("The string %s does not match the pattern %s", new Object[] { input, pattern }));@b@  }@b@@b@  public static void matchesPattern(CharSequence input, String pattern, String message, Object[] values)@b@  {@b@    if (!(Pattern.matches(pattern, input)))@b@      throw new IllegalArgumentException(String.format(message, values));@b@  }@b@@b@  public static <T> void inclusiveBetween(T start, T end, Comparable<T> value)@b@  {@b@    if ((value.compareTo(start) < 0) || (value.compareTo(end) > 0))@b@      throw new IllegalArgumentException(String.format("The value %s is not in the specified inclusive range of %s to %s", new Object[] { value, start, end }));@b@  }@b@@b@  public static <T> void inclusiveBetween(T start, T end, Comparable<T> value, String message, Object[] values)@b@  {@b@    if ((value.compareTo(start) < 0) || (value.compareTo(end) > 0))@b@      throw new IllegalArgumentException(String.format(message, values));@b@  }@b@@b@  public static <T> void exclusiveBetween(T start, T end, Comparable<T> value)@b@  {@b@    if ((value.compareTo(start) <= 0) || (value.compareTo(end) >= 0))@b@      throw new IllegalArgumentException(String.format("The value %s is not in the specified exclusive range of %s to %s", new Object[] { value, start, end }));@b@  }@b@@b@  public static <T> void exclusiveBetween(T start, T end, Comparable<T> value, String message, Object[] values)@b@  {@b@    if ((value.compareTo(start) <= 0) || (value.compareTo(end) >= 0))@b@      throw new IllegalArgumentException(String.format(message, values));@b@  }@b@@b@  public static void isInstanceOf(Class<?> type, Object obj)@b@  {@b@    if (!(type.isInstance(obj)))@b@      throw new IllegalArgumentException(String.format("Expected type: %s, actual: %s", new Object[] { type.getName(), (obj == null) ? "null" : obj.getClass().getName() }));@b@  }@b@@b@  public static void isInstanceOf(Class<?> type, Object obj, String message, Object[] values)@b@  {@b@    if (!(type.isInstance(obj)))@b@      throw new IllegalArgumentException(String.format(message, values));@b@  }@b@@b@  public static void isAssignableFrom(Class<?> superType, Class<?> type)@b@  {@b@    if (!(superType.isAssignableFrom(type)))@b@      throw new IllegalArgumentException(String.format("Cannot assign a %s to a %s", new Object[] { (type == null) ? "null" : type.getName(), superType.getName() }));@b@  }@b@@b@  public static void isAssignableFrom(Class<?> superType, Class<?> type, String message, Object[] values)@b@  {@b@    if (!(superType.isAssignableFrom(type)))@b@      throw new IllegalArgumentException(String.format(message, values));@b@  }@b@}