別瞎寫工具類了,Spring自帶的這些他不香麼?
AI全套:Python3+TensorFlow打造人臉識別智能小程序
最新人工智能資料-Google工程師親授 Tensorflow-入門到進階
黑馬頭條項目 - Java Springboot2.0(視頻、資料、代碼和講義)14天完整版
來源:juejin.cn/post/7043403364020781064 斷言
斷言是一個邏輯判斷,用于檢查不應該發(fā)生的情況
Assert 關鍵字在 JDK1.4 中引入,可通過 JVM 參數(shù)-enableassertions開啟
SpringBoot 中提供了 Assert 斷言工具類,通常用于數(shù)據(jù)合法性檢查
// 要求參數(shù) object 必須為非空(Not Null),否則拋出異常,不予放行// 參數(shù) message 參數(shù)用于定制異常信息。void notNull(Object object, String message)// 要求參數(shù)必須空(Null),否則拋出異常,不予『放行』。// 和 notNull() 方法斷言規(guī)則相反void isNull(Object object, String message)// 要求參數(shù)必須為真(True),否則拋出異常,不予『放行』。void isTrue(boolean expression, String message)// 要求參數(shù)(List/Set)必須非空(Not Empty),否則拋出異常,不予放行void notEmpty(Collection collection, String message)// 要求參數(shù)(String)必須有長度(即,Not Empty),否則拋出異常,不予放行void hasLength(String text, String message)// 要求參數(shù)(String)必須有內(nèi)容(即,Not Blank),否則拋出異常,不予放行void hasText(String text, String message)// 要求參數(shù)是指定類型的實例,否則拋出異常,不予放行void isInstanceOf(Class type, Object obj, String message)// 要求參數(shù) `subType` 必須是參數(shù) superType 的子類或?qū)崿F(xiàn)類,否則拋出異常,不予放行void isAssignable(Class superType, Class subType, String message)
對象、數(shù)組、集合
ObjectUtils
1.獲取對象的基本信息 // 獲取對象的類名。參數(shù)為 null 時,返回字符串:"null"String nullSafeClassName(Object obj)// 參數(shù)為 null 時,返回 0int nullSafeHashCode(Object object)// 參數(shù)為 null 時,返回字符串:"null"String nullSafeToString(boolean[] array)// 獲取對象 HashCode(十六進制形式字符串)。參數(shù)為 null 時,返回 0String getIdentityHexString(Object obj)// 獲取對象的類名和 HashCode。參數(shù)為 null 時,返回字符串:""String identityToString(Object obj)// 相當于 toString()方法,但參數(shù)為 null 時,返回字符串:""String getDisplayString(Object obj)
2.判斷工具
// 判斷數(shù)組是否為空boolean isEmpty(Object[] array)// 判斷參數(shù)對象是否是數(shù)組boolean isArray(Object obj)// 判斷數(shù)組中是否包含指定元素boolean containsElement(Object[] array, Object element)// 相等,或同為 null時,返回 trueboolean nullSafeEquals(Object o1, Object o2)/*判斷參數(shù)對象是否為空,判斷標準為:Optional: Optional.empty()Array: length == 0CharSequence: length == 0Collection: Collection.isEmpty()Map: Map.isEmpty()*/boolean isEmpty(Object obj)
3.其他工具方法 // 向參數(shù)數(shù)組的末尾追加新元素,并返回一個新數(shù)組A[] addObjectToArray(A[] array, O obj)// 原生基礎類型數(shù)組 --> 包裝類數(shù)組Object[] toObjectArray(Object source)
StringUtils
1.字符串判斷工具 // 判斷字符串是否為 null,或 ""。注意,包含空白符的字符串為非空boolean isEmpty(Object str)// 判斷字符串是否是以指定內(nèi)容結束。忽略大小寫boolean endsWithIgnoreCase(String str, String suffix)// 判斷字符串是否已指定內(nèi)容開頭。忽略大小寫boolean startsWithIgnoreCase(String str, String prefix)// 是否包含空白符boolean containsWhitespace(String str)// 判斷字符串非空且長度不為 0,即,Not Emptyboolean hasLength(CharSequence str)// 判斷字符串是否包含實際內(nèi)容,即非僅包含空白符,也就是 Not Blankboolean hasText(CharSequence str)// 判斷字符串指定索引處是否包含一個子串。boolean substringMatch(CharSequence str, int index, CharSequence substring)// 計算一個字符串中指定子串的出現(xiàn)次數(shù)int countOccurrencesOf(String str, String sub)
2.字符串操作工具
// 查找并替換指定子串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)// 對數(shù)組的每一項執(zhí)行 trim() 方法String[] trimArrayElements(String[] array)// 將 URL 字符串進行解碼String uriDecode(String source, Charset charset)
3.路徑相關工具方法 // 解析路徑字符串,優(yōu)化其中的 “..”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
1.集合判斷工具 // 判斷 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)
2.集合操作工具 // 將 Array 中的元素都添加到 List/Set 中void mergeArrayIntoCollection(Object array, Collection collection) // 將 Properties 中的鍵值對都添加到 Map 中void mergePropertiesIntoMap(Properties props, Map map) // 返回 List 中最后一個元素T lastElement(List list) // 返回 Set 中最后一個元素T lastElement(Set set) // 返回參數(shù) candidates 中第一個存在于參數(shù) source 中的元素E findFirstMatch(Collection> source, Collection candidates) // 返回 List/Set 中指定類型的元素。T findValueOfType(Collection> collection, Class type) // 返回 List/Set 中指定類型的元素。如果第一種類型未找到,則查找第二種類型,以此類推Object findValueOfType(Collection> collection, Class>[] types)// 返回 List/Set 中元素的類型Class> findCommonElementType(Collection> collection)
文件、資源、IO 流 FileCopyUtils
1.輸入 // 從文件中讀入到字節(jié)數(shù)組中byte[] copyToByteArray(File in)// 從輸入流中讀入到字節(jié)數(shù)組中byte[] copyToByteArray(InputStream in)// 從輸入流中讀入到字符串中String copyToString(Reader in)
2.輸出 // 從字節(jié)數(shù)組到文件void copy(byte[] in, File out)// 從文件到文件int copy(File in, File out)// 從字節(jié)數(shù)組到輸出流void copy(byte[] in, OutputStream out)// 從輸入流到輸出流int copy(InputStream in, OutputStream out)// 從輸入流到輸出流int copy(Reader in, Writer out)// 從字符串到輸出流void copy(String in, Writer out)
ResourceUtils
1.從資源路徑獲取文件 // 判斷字符串是否是一個合法的 URL 字符串。static boolean isUrl(String resourceLocation)// 獲取 URLstatic URL getURL(String resourceLocation)// 獲取文件(在 JAR 包內(nèi)無法正常使用,需要是一個獨立的文件)static FilegetFile(String resourceLocation)
2.Resource // 文件系統(tǒng)資源 D:\...FileSystemResource// URL 資源,如 file://... http://...UrlResource// 類路徑下的資源,classpth:...ClassPathResource// Web 容器上下文中的資源(jar 包、war 包)ServletContextResource
// 判斷資源是否存在boolean exists()// 從資源中獲得 File 對象File getFile()// 從資源中獲得 URI 對象URI getURI()// 從資源中獲得 URI 對象URL getURL()// 獲得資源的 InputStreamInputStream getInputStream()// 獲得資源的描述信息String getDescription()
StreamUtils
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)
2.輸出 byte[] copyToByteArray(InputStream in)String copyToString(InputStream in, Charset charset)// 舍棄輸入流中的內(nèi)容int drain(InputStream in)
反射、AOP
ReflectionUtils
// 在類中查找指定方法Method findMethod(Class> clazz, String name)// 同上,額外提供方法參數(shù)類型作查找條件Method findMethod(Class> clazz, String name, Class>... paramTypes)// 獲得類中所有方法,包括繼承而來的Method[] getAllDeclaredMethods(Class> leafClass)// 在類中查找指定構造方法ConstructoraccessibleConstructor(Class clazz, Class>... parameterTypes) // 是否是 equals() 方法boolean isEqualsMethod(Method method)// 是否是 hashCode() 方法boolean isHashCodeMethod(Method method)// 是否是 toString() 方法boolean isToStringMethod(Method method)// 是否是從 Object 類繼承而來的方法boolean isObjectMethod(Method method)// 檢查一個方法是否聲明拋出指定異常boolean declaresException(Method method, Class> exceptionType)
2.執(zhí)行方法 // 執(zhí)行方法Object invokeMethod(Method method, Object target)// 同上,提供方法參數(shù)Object invokeMethod(Method method, Object target, Object... args)// 取消 Java 權限檢查。以便后續(xù)執(zhí)行該私有方法void makeAccessible(Method method)// 取消 Java 權限檢查。以便后續(xù)執(zhí)行私有構造方法void makeAccessible(Constructor> ctor)
3.獲取字段 // 在類中查找指定屬性Field findField(Class> clazz, String name)// 同上,多提供了屬性的類型Field findField(Class> clazz, String name, Class> type)// 是否為一個 "public static final" 屬性boolean isPublicStaticFinal(Field field)
4.設置字段 // 獲取 target 對象的 field 屬性值Object getField(Field field, Object target)// 設置 target 對象的 field 屬性值,值為 valuevoid setField(Field field, Object target, Object value)// 同類對象屬性對等賦值void shallowCopyFieldState(Object src, Object dest)// 取消 Java 的權限控制檢查。以便后續(xù)讀寫該私有屬性void makeAccessible(Field field)// 對類的每個屬性執(zhí)行 callbackvoid doWithFields(Class> clazz, ReflectionUtils.FieldCallback fc)// 同上,多了個屬性過濾功能。void doWithFields(Class> clazz, ReflectionUtils.FieldCallback fc,ReflectionUtils.FieldFilter ff)// 同上,但不包括繼承而來的屬性void doWithLocalFields(Class> clazz, ReflectionUtils.FieldCallback fc)
AopUtils
1.判斷代理類型 // 判斷是不是 Spring 代理對象boolean isAopProxy()// 判斷是不是 jdk 動態(tài)代理對象isJdkDynamicProxy()// 判斷是不是 CGLIB 代理對象boolean isCglibProxy()
2.獲取被代理對象的 class // 獲取被代理的目標 classClass> getTargetClass()
AopContext
1.獲取當前對象的代理對象 Object currentProxy()-End-
全棧架構社區(qū)交流群
?「全棧架構社區(qū)」建立了讀者架構師交流群,大家可以添加小編微信進行加群。歡迎有想法、樂于分享的朋友們一起交流學習。
Flutter 移動應用開發(fā)實戰(zhàn) 視頻(開發(fā)你自己的抖音APP) Java面試進階訓練營 第2季(分布式篇) Java高級 - 分布式系統(tǒng)開發(fā)技術視頻
評論
圖片
表情
