• 当前位置: 首 页 > 教育百科 > 学历/技能 > 正文

    spring内置工具类

    :2022年08月25日
    吾爱破解

    spring内置工具类断言Assert通常用于用于数据合法性检查 复制代码 隐藏代码// 要求参数 object 必须为非空(Not Null),否则抛出异常,不予放行 // 参数 message 参数用于定制异常信息。 void notNul...

    spring内置工具类

    断言Assert

    通常用于用于数据合法性检查

     复制代码 隐藏代码// 要求参数 object 必须为非空(Not Null),否则抛出异常,不予放行
    // 参数 message 参数用于定制异常信息。
    void notNull(Object object, String message)
    // 要求参数必须空(Null),否则抛出异常,不予『放行』。
    // 和 notNull() 方法断言规则相反
    void isNull(Object object, String message)
    // 要求参数必须为真(True),否则抛出异常,不予『放行』。
    void isTrue(boolean expression, String message)
    // 要求参数(List/Set)必须非空(Not Empty),否则抛出异常,不予放行
    void notEmpty(Collection collection, String message)
    // 要求参数(String)必须有长度(即,Not Empty),否则抛出异常,不予放行
    void hasLength(String text, String message)
    // 要求参数(String)必须有内容(即,Not Blank),否则抛出异常,不予放行
    void hasText(String text, String message)
    // 要求参数是指定类型的实例,否则抛出异常,不予放行
    void isInstanceOf(Class type, Object obj, String message)
    // 要求参数 `subType` 必须是参数 superType 的子类或实现类,否则抛出异常,不予放行
    void isAssignable(Class superType, Class subType, String message)

    对象,数组和集合 ObjectUtils

    对象

     复制代码 隐藏代码// 获取对象的类名。参数为 null 时,返回字符串:"null" 
    String nullSafeClassName(Object obj)
    // 参数为 null 时,返回 0
    int nullSafeHashCode(Object object)
    // 参数为 null 时,返回字符串:"null"
    String nullSafeToString(boolean[] array)
    // 获取对象 HashCode(十六进制形式字符串)。参数为 null 时,返回 0 
    String getIdentityHexString(Object obj)
    // 获取对象的类名和 HashCode。 参数为 null 时,返回字符串:"" 
    String identityToString(Object obj)
    // 相当于 toString()方法,但参数为 null 时,返回字符串:""
    String getDisplayString(Object obj)

    判断

     复制代码 隐藏代码// 判断数组是否为空
    boolean isEmpty(Object[] array)
    // 判断参数对象是否是数组
    boolean isArray(Object obj)
    // 判断数组中是否包含指定元素
    boolean containsElement(Object[] array, Object element)
    // 相等,或同为 null时,返回 true
    boolean nullSafeEquals(Object o1, Object o2)
    /*
    判断参数对象是否为空,判断标准为:
        Optional: Optional.empty()
           Array: length == 0
    CharSequence: length == 0
      Collection: Collection.isEmpty()
             Map: Map.isEmpty()
     */
    boolean isEmpty(Object obj)

    字符串StringUtils

    字符串判断工具

     复制代码 隐藏代码// 判断字符串是否为 null,或 ""。注意,包含空白符的字符串为非空
    boolean isEmpty(Object str)
    // 判断字符串是否是以指定内容结束。忽略大小写
    boolean endsWithIgnoreCase(String str, String suffix)
    // 判断字符串是否已指定内容开头。忽略大小写
    boolean startsWithIgnoreCase(String str, String prefix) 
    // 是否包含空白符
    boolean containsWhitespace(String str)
    // 判断字符串非空且长度不为 0,即,Not Empty
    boolean hasLength(CharSequence str)
    // 判断字符串是否包含实际内容,即非仅包含空白符,也就是 Not Blank
    boolean hasText(CharSequence str)
    // 判断字符串指定索引处是否包含一个子串。
    boolean substringMatch(CharSequence str, int index, CharSequence substring)
    // 计算一个字符串中指定子串的出现次数
    int countOccurrencesOf(String str, String sub)

    字符串操作工具

     复制代码 隐藏代码// 查找并替换指定子串
    String replace(String inString, String oldPattern, String newPattern)
    // 去除尾部的特定字符
    String trimTrailingCharacter(String str, char trailingCharacter) 
    // 去除头部的特定字符
    String trimLeadingCharacter(String str, char leadingCharacter)
    // 去除头部的空白符
    String trimLeadingWhitespace(String str)
    // 去除头部的空白符
    String trimTrailingWhitespace(String str)
    // 去除头部和尾部的空白符
    String trimWhitespace(String str)
    // 删除开头、结尾和中间的空白符
    String trimAllWhitespace(String str)
    // 删除指定子串
    String delete(String inString, String pattern)
    // 删除指定字符(可以是多个)
    String deleteAny(String inString, String charsToDelete)
    // 对数组的每一项执行 trim() 方法
    String[] trimArrayElements(String[] array)
    // 将 URL 字符串进行解码
    String uriDecode(String source, Charset charset)

    路径相关工具

     复制代码 隐藏代码// 解析路径字符串,优化其中的 “..” 
    String cleanPath(String path)
    // 解析路径字符串,解析出文件名部分
    String getFilename(String path)
    // 解析路径字符串,解析出文件后缀名
    String getFilenameExtension(String path)
    // 比较两个两个字符串,判断是否是同一个路径。会自动处理路径中的 “..” 
    boolean pathEquals(String path1, String path2)
    // 删除文件路径名中的后缀部分
    String stripFilenameExtension(String path) 
    // 以 “. 作为分隔符,获取其最后一部分
    String unqualify(String qualifiedName)
    // 以指定字符作为分隔符,获取其最后一部分
    String unqualify(String qualifiedName, char separator)

    集合CollectionUtils

    集合判断工具

     复制代码 隐藏代码// 判断 List/Set 是否为空boolean isEmpty(Collection<?> collection)// 判断 Map 是否为空boolean isEmpty(Map<?,?> map)// 判断 List/Set 中是否包含某个对象boolean containsInstance(Collection<?> collection, Object element)// 以迭代器的方式,判断 List/Set 中是否包含某个对象boolean contains(Iterator<?> iterator, Object element)// 判断 List/Set 是否包含某些对象中的任意一个boolean containsAny(Collection<?> source, Collection<?> candidates)// 判断 List/Set 中的每个元素是否唯一。即 List/Set 中不存在重复元素boolean hasUniqueObject(Collection<?> collection)

    集合操作工具

     复制代码 隐藏代码// 将 Array 中的元素都添加到 List/Set 中<E> void mergeArrayIntoCollection(Object array, Collection<E> collection)  // 将 Properties 中的键值对都添加到 Map 中<K,V> void mergePropertiesIntoMap(Properties props, Map<K,V> map)// 返回 List 中最后一个元素<T> T lastElement(List<T> list)  // 返回 Set 中最后一个元素<T> T lastElement(Set<T> set) 
    // 返回参数 candidates 中第一个存在于参数 source 中的元素<E> E findFirstMatch(Collection<?> source, Collection<E> candidates)// 返回 List/Set 中指定类型的元素。<T> T findValueOfType(Collection<?> collection, Class<T> type)// 返回 List/Set 中指定类型的元素。如果第一种类型未找到,则查找第二种类型,以此类推Object findValueOfType(Collection<?> collection, Class<?>[] types)// 返回 List/Set 中元素的类型Class<?> findCommonElementType(Collection<?> collection)

    文件,资源,IO流

    文件FileUtils

    1. 输入

     复制代码 隐藏代码// 从文件中读入到字节数组中
    byte[] copyToByteArray(File in)
    // 从输入流中读入到字节数组中
    byte[] copyToByteArray(InputStream in)
    // 从输入流中读入到字符串中
    String copyToString(Reader in)
    1. 输出

     复制代码 隐藏代码// 从字节数组到文件
    void copy(byte[] in, File out)
    // 从文件到文件
    int copy(File in, File out)
    // 从字节数组到输出流
    void copy(byte[] in, OutputStream out) 
    // 从输入流到输出流
    int copy(InputStream in, OutputStream out) 
    // 从输入流到输出流
    int copy(Reader in, Writer out)
    // 从字符串到输出流
    void copy(String in, Writer out)

    资源

    1. 从资源路径获取文件

     复制代码 隐藏代码// 判断字符串是否是一个合法的 URL 字符串。
    static boolean isUrl(String resourceLocation)
    // 获取 URL
    static URL getURL(String resourceLocation) 
    // 获取文件(在 JAR 包内无法正常使用,需要是一个独立的文件)
    static File getFile(String resourceLocation)
    1. Resource

     复制代码 隐藏代码// 文件系统资源 D:\...
    FileSystemResource
    // URL 资源,如 file://... http://...
    UrlResource
    // 类路径下的资源,classpth:...
    ClassPathResource
    // Web 容器上下文中的资源(jar 包、war 包)
    ServletContextResource
    // 判断资源是否存在
    boolean exists()
    // 从资源中获得 File 对象
    File getFile()
    // 从资源中获得 URI 对象
    URI getURI()
    // 从资源中获得 URI 对象
    URL getURL()
    // 获得资源的 InputStream
    InputStream getInputStream()
    // 获得资源的描述信息
    String getDescription()

    1. 输入

     复制代码 隐藏代码void copy(byte[] in, OutputStream out)
    int copy(InputStream in, OutputStream out)
    void copy(String in, Charset charset, OutputStream out)
    long copyRange(InputStream in, OutputStream out, long start, long end)
    1. 输出

     复制代码 隐藏代码byte[] copyToByteArray(InputStream in)
    String copyToString(InputStream in, Charset charset)
    // 舍弃输入流中的内容
    int drain(InputStream in)
    [编辑:宋聪乔 &发表于江苏]
    [我要纠错]

    来源:本文内容搜集或转自各大网络平台,并已注明来源、出处,如果转载侵犯您的版权或非授权发布,请联系小编,我们会及时审核处理。
    声明:江苏教育黄页对文中观点保持中立,对所包含内容的准确性、可靠性或者完整性不提供任何明示或暗示的保证,不对文章观点负责,仅作分享之用,文章版权及插图属于原作者。

    关键词: spring 内置 工具 断言 Assert
    有价值
    0
    无价值
    0
    猜您喜欢
    最热文章

    暂不支持手机端,请登录电脑端访问

    正在加载验证码......

    请先完成验证