From 9cb59d063494fe69cfa20fcdebed1c8f047540ed Mon Sep 17 00:00:00 2001 From: Kalven Date: Mon, 26 Aug 2019 16:41:14 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-1117=20=E5=B0=81=E8=A3=85=E4=BA=86String?= =?UTF-8?q?Util=E5=A4=A7=E9=83=A8=E5=88=86=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E5=B0=81=E8=A3=85=E4=BA=86=E9=83=A8=E5=88=86=E7=94=A8=E5=88=B0?= =?UTF-8?q?=E7=B1=BB=EF=BC=8C=E5=AF=B9=E5=8F=AF=E4=BB=A5=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=9A=84=E7=B1=BB=E7=BC=96=E5=86=99=E4=BA=86=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/fanruan/api/util/BaseKit.java | 26 +++ .../java/com/fanruan/api/util/GUICoreKit.java | 24 +++ .../java/com/fanruan/api/util/GeneralKit.java | 4 + .../java/com/fanruan/api/util/StringKit.java | 166 ++++++++++++++++++ .../com/fanruan/api/util/GeneralKitTest.java | 12 ++ .../com/fanruan/api/util/StringKitTest.java | 142 +++++++++++++++ 6 files changed, 374 insertions(+) create mode 100644 src/main/java/com/fanruan/api/util/BaseKit.java create mode 100644 src/main/java/com/fanruan/api/util/GUICoreKit.java diff --git a/src/main/java/com/fanruan/api/util/BaseKit.java b/src/main/java/com/fanruan/api/util/BaseKit.java new file mode 100644 index 0000000..7206d1f --- /dev/null +++ b/src/main/java/com/fanruan/api/util/BaseKit.java @@ -0,0 +1,26 @@ +package com.fanruan.api.util; + +import com.fr.base.BaseUtils; + +import javax.swing.*; +import java.io.InputStream; + +public class BaseKit { + /** + * 从file中读数据,以流的形式读出数据 + * @param file + * @return 返回file文件,文件数据流 + */ + public static InputStream readResource(String file) { + return BaseUtils.readResource(file); + } + + /** + * 从file中读取数据,根据数据返回Icon图片 + * @param file + * @return 返回Icon图片 + */ + public static Icon readIcon(String file) { + return BaseUtils.readIcon(file); + } +} diff --git a/src/main/java/com/fanruan/api/util/GUICoreKit.java b/src/main/java/com/fanruan/api/util/GUICoreKit.java new file mode 100644 index 0000000..4914cfc --- /dev/null +++ b/src/main/java/com/fanruan/api/util/GUICoreKit.java @@ -0,0 +1,24 @@ +package com.fanruan.api.util; + +import com.fr.design.utils.gui.GUICoreUtils; + +import javax.swing.*; +import java.awt.*; + +public class GUICoreKit { + + public static JPanel createFlowPane(Object... var0) { + return GUICoreUtils.createFlowPane(var0); + } + + /** + * 弹出菜单,在坐标为x,y处弹出内容为component的菜单 + * @param menu + * @param component + * @param x + * @param y + */ + public static void showPopupMenu(JPopupMenu menu, Component component, int x, int y) { + GUICoreUtils.showPopupMenu(menu, component, x, y); + } +} diff --git a/src/main/java/com/fanruan/api/util/GeneralKit.java b/src/main/java/com/fanruan/api/util/GeneralKit.java index 7b69760..f5e328e 100644 --- a/src/main/java/com/fanruan/api/util/GeneralKit.java +++ b/src/main/java/com/fanruan/api/util/GeneralKit.java @@ -49,4 +49,8 @@ public class GeneralKit { public static @Nullable Number objectToNumber(Object obj, boolean returnNull) { return GeneralUtils.objectToNumber(obj, returnNull); } + + public static @Nullable String readBuildNO() { + return GeneralUtils.readBuildNO(); + } } diff --git a/src/main/java/com/fanruan/api/util/StringKit.java b/src/main/java/com/fanruan/api/util/StringKit.java index e79d263..bbd2f06 100644 --- a/src/main/java/com/fanruan/api/util/StringKit.java +++ b/src/main/java/com/fanruan/api/util/StringKit.java @@ -1,6 +1,7 @@ package com.fanruan.api.util; import com.fr.stable.StringUtils; +import java.io.UnsupportedEncodingException; /** * @author richie @@ -95,4 +96,169 @@ public class StringKit { return StringUtils.trim(string); } + /** + *如果String为空则返回空字符串,否则返回String本身的值,保证String不空。 + * @param text + * @return + */ + public static String alwaysNotNull(String text) { + return StringUtils.alwaysNotNull(text); + } + /** + * 获取text字符串中去除结尾suffix字符串的部分 + * @param text + * @param suffix + * @return 若text字符串结尾是suffix,则返回去除suffix的部分,否则返回text + */ + public static String cutStringEndWith(String text, String suffix) { + return StringUtils.cutStringEndWith(text, suffix); + } + + /**获取text字符串去除开头startString部分 + * + * @param text + * @param startString + * @return 若text字符串开头是suffix,则返回去除startString部分否则返回text + */ + public static String cutStringStartWith(String text, String startString) { + return StringUtils.cutStringStartWith(text, startString); + } + + /** + * 去掉text字符串两边的空格或ASCII码小于空格的字符 + * @param text + * @return 若text只含空格字符,或ASCII码小于空格的字符,返回null,否则返回去除两边空字符后的text + */ + public static String trimToNull(String text) { + return StringUtils.trimToNull(text); + } + + /** + * 令text以startString开头 + * @param text + * @param startString + * @return 若text不以startString开头,在text前添加startString。若text以startString开头返回text + */ + public static String perfectStart(String text, String startString) { + return StringUtils.perfectStart(text, startString); + } + + /** + * 令text以suffix结尾 + * @param text + * @param suffix + * @return 若text不以suffix结尾,在text后添加suffix。若text以suffix结尾返回text + */ + public static String perfectEnd(String text, String suffix) { + return StringUtils.perfectEnd(text, suffix); + } + + /** + * 令text的开头结尾都是str + * @param text + * @param str + * @return 若text的开头或结尾不是str,在相应位置添加str,若开头结尾都是text,返回text + */ + public static String perfectSurround(String text, String str) { + return StringUtils.perfectSurround(text, str); + } + + /** + * 返回text长度 + * @param text + * @return 若text为null返回0,否则返回text长度 + */ + public static int getLength(String text) { + return StringUtils.getLength(text); + } + + /** + * 判断text1和text2在去掉开头或结尾的ignoreStr部分是否相等 + * @param text1 + * @param text2 + * @param ignoreStr + * @return 如果text1和txet2去掉开头结尾的ignoreStr后相等返回true,不等返回false + */ + public static boolean equalsIgnore(String text1, String text2, String ignoreStr) { + return StringUtils.equalsIgnore(text1, text2, ignoreStr); + } + + /** + * 将str加入到strArr字符串数组的每个字符串后方(不包括最后一个字符串),同时将结果字符串数组连接起来,形成新的字符串 + * @param str + * @param strArr + * @return 若strArr为空则返回null,若strArr长度为0返回""空字符串,否则返回连接后的字符串 + */ + public static String join(String str, String[] strArr) { + return StringUtils.join(str, strArr); + } + + /** + * 将text字符串中的A-J转换成0-9,工具函数 + * @param text + * @return 返回转换后的字符串 + */ + public static String parseVersion(String text) { + return StringUtils.parseVersion(text); + } + + /** + * 判断text是否是json中数组的string形式 + * @param text + * @return 若是数组的String形式返回true,否则返回flase + */ + public static boolean isArrayType(String text) { + return StringUtils.isArrayType(text); + } + + /** + *从原本json数组类型的String取出其中的每一个用逗号间隔元素,作为String二维数组返回值 + * @param text + * @return 取出text中用逗号间隔的元素 + */ + public static String[][] stringToArray(String text) { + return StringUtils.stringToArray(text); + } + + /** + *对text中每个字符,用charsetName为名的编码方式解码后,返回解码后字节长度从0到bytelength的部分,以String形式返回 + * @param text + * @param charsetName + * @param byteLength + * @return 若text为空或byteLength小于等于0返回空字符串,否则以String形式返回解码后字节长度从0到bytelength的部分 + */ + public static String subStringByByteLength(String text, String charsetName, int byteLength) throws UnsupportedEncodingException { + return StringUtils.subStringByByteLength(text, charsetName, byteLength); + } + + /** + * 比较text和compareStr两字符串值是否相等 + * @param text + * @param compareStr + * @return 若text和compareStr两字符串的值相同返回true,否则返回false + */ + public static boolean equals(String text, String compareStr) { + return StringUtils.equals(text, compareStr); + } + + /** + * 比较text和compareStr两字符串忽略大小写的情况下值是否相等 + * @param text + * @param compareStr + * @return 在忽略大小写的情况下,若text和compareStr两字符串的值相同返回true,否则返回false + */ + public static boolean equalsIgnoreCase(String text, String compareStr) { + return StringUtils.equalsIgnoreCase(text, compareStr); + } + + /** + * 扩展text的长度至stringLength,不足则以空格填充 + * @param text + * @param stringLength + * @return 若text长度比stringLength,则将text扩充到stringLength长度,否则返回text + */ + public static String rightPad(String text, int stringLength) { + return StringUtils.rightPad(text, stringLength); + } + } diff --git a/src/test/java/com/fanruan/api/util/GeneralKitTest.java b/src/test/java/com/fanruan/api/util/GeneralKitTest.java index cbead61..84b8e23 100644 --- a/src/test/java/com/fanruan/api/util/GeneralKitTest.java +++ b/src/test/java/com/fanruan/api/util/GeneralKitTest.java @@ -15,4 +15,16 @@ public class GeneralKitTest extends Prepare { public void getMacAddress() throws Exception{ Assert.assertEquals(GeneralKit.getMacAddress(), com.fr.general.GeneralUtils.getMacAddress()); } + + @Test + public void objectToString() { + Integer s = new Integer(1); + Assert.assertEquals(GeneralKit.objectToString(s),"1"); + } + + @Test + public void readBuildNO() { + Assert.assertEquals(GeneralKit.readBuildNO(), com.fr.general.GeneralUtils.readBuildNO()); + } + } \ No newline at end of file diff --git a/src/test/java/com/fanruan/api/util/StringKitTest.java b/src/test/java/com/fanruan/api/util/StringKitTest.java index 3e07b43..8bef1a3 100644 --- a/src/test/java/com/fanruan/api/util/StringKitTest.java +++ b/src/test/java/com/fanruan/api/util/StringKitTest.java @@ -14,18 +14,32 @@ public class StringKitTest { @Test public void isEmpty() { + assertEquals(StringKit.isEmpty(""),true); + assertEquals(StringKit.isEmpty("asd"),false); } @Test public void isNotEmpty() { + assertEquals(StringKit.isNotEmpty("asd"),true); + assertEquals(StringKit.isNotEmpty(""),false); } @Test public void isBlank() { + assertEquals(StringKit.isNotBlank(null),false); + assertEquals(StringKit.isNotBlank(""),false); + assertEquals(StringKit.isNotBlank(" "),false); + assertEquals(StringKit.isNotBlank("bob"),true); + assertEquals(StringKit.isNotBlank(" bob "),true); } @Test public void isNotBlank() { + assertEquals(StringKit.isBlank(null),true); + assertEquals(StringKit.isBlank(""),true); + assertEquals(StringKit.isBlank(" "),true); + assertEquals(StringKit.isBlank("bob"),false); + assertEquals(StringKit.isBlank(" bob "),false); } @Test public void trim(){ @@ -36,4 +50,132 @@ public class StringKitTest { Assert.assertEquals(StringKit.trim(stringWithoutBlank), "abc"); Assert.assertEquals(StringKit.trim(blankString), ""); } + + @Test + public void alwaysNotNull() { + assertEquals(StringKit.alwaysNotNull(null),""); + assertEquals(StringKit.alwaysNotNull(""),""); + assertEquals(StringKit.alwaysNotNull("asd"),"asd"); + } + + @Test + public void cutStringEndWith() { + assertEquals(StringKit.cutStringEndWith("bob is","is"), "bob "); + assertEquals(StringKit.cutStringEndWith("","is"), ""); + } + + @Test + public void cutStringStartWith() { + assertEquals(StringKit.cutStringStartWith("bob is","bob "), "is"); + assertEquals(StringKit.cutStringStartWith("","is"), ""); + } + + @Test + public void trimToNull() { + assertEquals(StringKit.trimToNull(" "),null); + assertEquals(StringKit.trimToNull(" s "),"s"); + } + + @Test + public void perfectStart() { + assertEquals(StringKit.perfectStart("bobob","bob"),"bobob"); + assertEquals(StringKit.perfectStart("sbobob","bob"),"bobsbobob"); + } + + @Test + public void perfectEnd() { + assertEquals(StringKit.perfectEnd("bobob","bob"),"bobob"); + assertEquals(StringKit.perfectEnd("bobobs","bob"),"bobobsbob"); + } + + @Test + public void perfectSurround() { + assertEquals(StringKit.perfectSurround("bobob","bob"),"bobob"); + assertEquals(StringKit.perfectSurround("sbobobs","bob"),"bobsbobobsbob"); + } + + @Test + public void getLength() { + assertEquals(StringKit.getLength("asd"),3); + assertEquals(StringKit.getLength(""),0); + assertEquals(StringKit.getLength(null),0); + } + + @Test + public void equalsIgnore() { + assertEquals(StringKit.equalsIgnore("asd","asd","qwewqe"),true); + assertEquals(StringKit.equalsIgnore("asd","as","d"),true); + assertEquals(StringKit.equalsIgnore("asd","asdd","d"),false); + } + + @Test + public void join() { + String[] s = new String[3]; + String[] s1 = new String[0]; + s[0]= new String("qqq"); + s[1]= new String("www"); + s[2]= new String("eee"); + assertEquals(StringKit.join("asd",s),"qqqasdwwwasdeee"); + assertEquals(StringKit.join("",s),"qqqwwweee"); + assertEquals(StringKit.join("asd",null),null); + assertEquals(StringKit.join("asd",s1),""); + } + + + @Test + public void parseVersion() { + assertEquals(StringKit.parseVersion("BCD123"),"123123"); + } + + @Test + public void isArrayType() { + assertEquals(StringKit.isArrayType("[[]]"),true); + assertEquals(StringKit.isArrayType("[["),false); + } + + @Test + public void stringToArray() { + String[][] s = new String[1][3]; + s[0][0] = new String("a"); + s[0][1] = new String("b"); + s[0][2] = new String("c"); + assertEquals(StringKit.stringToArray("[[a,b,c]]"),s); + } + + @Test + public void subStringByByteLength() { + try { + assertEquals(StringKit.subStringByByteLength("asd", "UTF-8", 1), "a"); + assertEquals(StringKit.subStringByByteLength(" ", "UTF-8", 1), ""); + assertEquals(StringKit.subStringByByteLength("asd", "UTF-8", 0), ""); + }catch (Exception e){ + + } + } + + @Test + public void equals() { + String s = new String("asd"); + String s1 = new String("asd"); + String s2 = new String("asds"); + assertEquals(StringKit.equals(s, s1), true); + assertEquals(StringKit.equals(s, s2), false); + assertEquals(StringKit.equals(null, null), true); + } + + @Test + public void equalsIgnoreCase() { + String s = new String("Asd"); + String s1 = new String("asd"); + String s2 = new String("asds"); + assertEquals(StringKit.equalsIgnoreCase(s, s1), true); + assertEquals(StringKit.equalsIgnoreCase(s, s2), false); + assertEquals(StringKit.equalsIgnoreCase(null, null), true); + } + + @Test + public void rightPad() { + assertEquals(StringKit.rightPad("asd",5), "asd "); + assertEquals(StringKit.rightPad("asd",-1), "asd"); + } } \ No newline at end of file