From 440e6627b7bde4cac4c04d30b9ea98b396d8c756 Mon Sep 17 00:00:00 2001 From: mikusugar Date: Wed, 14 Jul 2021 18:38:56 +0800 Subject: [PATCH 1/7] =?UTF-8?q?add:=20Radnom=20MAC=E3=80=81UUID=E3=80=81?= =?UTF-8?q?=E7=BB=8F=E7=BA=AC=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../random/core/constant/ServiceName.java | 5 ++ .../core/service/impl/RandomLonLat.java | 63 +++++++++++++++++++ .../random/core/service/impl/RandomMAC.java | 44 +++++++++++++ .../random/core/service/impl/RandomUUID.java | 30 +++++++++ 4 files changed, 142 insertions(+) create mode 100644 sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomLonLat.java create mode 100644 sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomMAC.java create mode 100644 sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomUUID.java diff --git a/sugar_random_core/src/main/java/me/mikusugar/random/core/constant/ServiceName.java b/sugar_random_core/src/main/java/me/mikusugar/random/core/constant/ServiceName.java index 8bf6279..6d29c47 100644 --- a/sugar_random_core/src/main/java/me/mikusugar/random/core/constant/ServiceName.java +++ b/sugar_random_core/src/main/java/me/mikusugar/random/core/constant/ServiceName.java @@ -28,4 +28,9 @@ public class ServiceName { public static final String RANDOM_IP = "随机生成IP"; + public static final String UUID = "UUID"; + + public static final String MAC = "MAC"; + + public static final String LONLAT = "经纬度"; } diff --git a/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomLonLat.java b/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomLonLat.java new file mode 100644 index 0000000..146acf6 --- /dev/null +++ b/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomLonLat.java @@ -0,0 +1,63 @@ +package me.mikusugar.random.core.service.impl; + +import me.mikusugar.random.core.bean.SugarJsonNode; +import me.mikusugar.random.core.constant.ServiceName; +import me.mikusugar.random.core.service.AbstractRandomService; +import me.mikusugar.random.core.utils.RandomUtilInterface; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.util.Random; + +/** + * @author mikusugar + */ +@Service(ServiceName.LONLAT) +public class RandomLonLat extends AbstractRandomService { + + @Override + protected RandomUtilInterface createRandomUtilInterface(String input) { + final String[] strs = input.trim().split(","); + double[] in = new double[strs.length]; + for (int i = 0; i < strs.length; i++) { + in[i] = Double.parseDouble(strs[i]); + } + return () -> randomLonLat(in[0], in[1], in[2], in[3]); + } + + @Override + public String helpText() { + return "输入4位数字,如 112.1212, 134.1212, 43.12, 65.123 分别是 最小经度 最大经度 最小纬度 最大纬度 将返回这个结果内的值"; + } + + /** + * @param MinLon 最小经度 + * @param MaxLon 最大经度 + * @param MinLat 最小纬度 + * @param MaxLat 最大纬度 + * @return [lon, lat] + */ + private static String randomLonLat(double MinLon, double MaxLon, double MinLat, double MaxLat) { + BigDecimal db = new BigDecimal(Math.random() * (MaxLon - MinLon) + MinLon); + String lon = db.setScale(6, BigDecimal.ROUND_HALF_UP).toString(); + db = new BigDecimal(Math.random() * (MaxLat - MinLat) + MinLat); + String lat = db.setScale(6, BigDecimal.ROUND_HALF_UP).toString(); + return lon + "," + lat; + } + + + @Override + public boolean check(String type, String input) { + if (!SugarJsonNode.TYPE.STRING.toString().equals(type)) return false; + try { + final String[] strs = input.trim().split(","); + if (strs.length != 4) return false; + for (String str : strs) { + Double.parseDouble(str); + } + } catch (Exception e) { + return false; + } + return true; + } +} diff --git a/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomMAC.java b/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomMAC.java new file mode 100644 index 0000000..fbca1cb --- /dev/null +++ b/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomMAC.java @@ -0,0 +1,44 @@ +package me.mikusugar.random.core.service.impl; + +import me.mikusugar.random.core.bean.SugarJsonNode; +import me.mikusugar.random.core.constant.ServiceName; +import me.mikusugar.random.core.service.AbstractRandomService; +import me.mikusugar.random.core.utils.RandomUtilInterface; +import org.springframework.stereotype.Service; + +import java.util.Random; + +/** + * @author mikusugar + */ +@Service(ServiceName.MAC) +public class RandomMAC extends AbstractRandomService { + + private final static Random random = new Random(); + private final static String SEPARATOR_OF_MAC = ":"; + private final static String _02X = "%02x"; + + @Override + protected RandomUtilInterface createRandomUtilInterface(String input) { + return () -> { + String[] mac = { + String.format(_02X, 0x52), + String.format(_02X, 0x54), + String.format(_02X, 0x00), + String.format(_02X, random.nextInt(0xff)), + String.format(_02X, random.nextInt(0xff)), + String.format(_02X, random.nextInt(0xff))}; + return String.join(SEPARATOR_OF_MAC, mac); + }; + } + + @Override + public String helpText() { + return "随机生成MAC地址,无需输入"; + } + + @Override + public boolean check(String type, String input) { + return SugarJsonNode.TYPE.STRING.toString().equals(type); + } +} diff --git a/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomUUID.java b/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomUUID.java new file mode 100644 index 0000000..40d2b5e --- /dev/null +++ b/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomUUID.java @@ -0,0 +1,30 @@ +package me.mikusugar.random.core.service.impl; + +import me.mikusugar.random.core.bean.SugarJsonNode; +import me.mikusugar.random.core.constant.ServiceName; +import me.mikusugar.random.core.service.AbstractRandomService; +import me.mikusugar.random.core.utils.RandomUtilInterface; +import org.springframework.stereotype.Service; + +import java.util.UUID; + +/** + * @author mikusugar + */ +@Service(ServiceName.UUID) +public class RandomUUID extends AbstractRandomService { + @Override + protected RandomUtilInterface createRandomUtilInterface(String input) { + return () -> UUID.randomUUID().toString(); + } + + @Override + public String helpText() { + return "java 原生的UUID,无需输入"; + } + + @Override + public boolean check(String type, String input) { + return SugarJsonNode.TYPE.STRING.toString().equals(type); + } +} From 293134afa8b876a8d2f2ae8db2c950732bfc0415 Mon Sep 17 00:00:00 2001 From: mikusugar Date: Fri, 16 Jul 2021 15:12:20 +0800 Subject: [PATCH 2/7] =?UTF-8?q?add:=20cli=E6=B7=BB=E5=8A=A0=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sugar/random/cli/service/CliService.java | 45 +++++--- .../sugar/random/cli/utils/CliUtils.java | 103 +++++++++++------- .../core/service/impl/RandomLonLat.java | 1 - 3 files changed, 95 insertions(+), 54 deletions(-) diff --git a/sugar_random_cli/src/main/java/me/mikusugar/sugar/random/cli/service/CliService.java b/sugar_random_cli/src/main/java/me/mikusugar/sugar/random/cli/service/CliService.java index 392ecdd..8de563c 100644 --- a/sugar_random_cli/src/main/java/me/mikusugar/sugar/random/cli/service/CliService.java +++ b/sugar_random_cli/src/main/java/me/mikusugar/sugar/random/cli/service/CliService.java @@ -152,6 +152,24 @@ public String add(String field, String father, String dtype, String rtype, return "字段配置添加成功~"; } + @ShellMethod(value = "删除字段,会同时删除该字段的所有子字段,无法删除root字段", group = "random") + public String delNode(String field) { + if (!map.containsKey(field)) return CliUtils.showField(field) + "不存在"; + final SugarJsonNode father = CliUtils.findFather(rootNode, field); + final SugarJsonNode node = map.get(field); + dfsDelNode(node); + father.getNexts().remove(node); + return CliUtils.showField(field) + "已删除"; + } + + private void dfsDelNode(SugarJsonNode node) { + if (node.getNexts() != null) { + node.getNexts().forEach(next -> dfsDelNode(next)); + } + map.remove(node.getName()); + } + + @ShellMethod(value = "移除所有配置", group = "random") public String removeAll() { this.map.clear(); @@ -161,14 +179,14 @@ public String removeAll() { @ShellMethod(value = "生成文件到本地", group = "random") public String outFile(int num, String path) throws IOException { - final FileWriter out = new FileWriter(new File(path+".json")); + final FileWriter out = new FileWriter(new File(path + ".json")); int size = num; - while (size-->0) { + while (size-- > 0) { val res = new StringBuilder(); SugarJsonUtils.toJsonStr(null, rootNode, res); out.write(res.toString()); - if(size>0){ - out.write(","+System.lineSeparator()); + if (size > 0) { + out.write("," + System.lineSeparator()); } } out.flush(); @@ -180,18 +198,17 @@ public String outFile(int num, String path) throws IOException { // group config /////////////////////////////////////////////////////////////////////////// - @ShellMethod(value = "配置读取",group = "config") + @ShellMethod(value = "配置读取", group = "config") public String read(String name) throws JsonProcessingException { final Optional savaRepositoryById = configSavaRepository.findById(name); if (savaRepositoryById.isPresent()) { - this.rootNode=SugarJsonNodeSerialization.read( - savaRepositoryById.get().getJson(),randomServiceMap); + this.rootNode = SugarJsonNodeSerialization.read( + savaRepositoryById.get().getJson(), randomServiceMap); map.clear(); dfsAddMap(this.rootNode); return "配置读取成功"; - } - else return "配置名不存在!"; + } else return "配置名不存在!"; } private void dfsAddMap(SugarJsonNode node) { @@ -200,9 +217,9 @@ private void dfsAddMap(SugarJsonNode node) { node.getNexts().forEach(this::dfsAddMap); } - @ShellMethod(value="配置存储",group = "config") + @ShellMethod(value = "配置存储", group = "config") public String save(String name) throws JsonProcessingException { - if(name==null||name.trim().isEmpty()) { + if (name == null || name.trim().isEmpty()) { return "配置名为空,无法存储!"; } final String json = SugarJsonNodeSerialization.write(rootNode); @@ -213,9 +230,9 @@ public String save(String name) throws JsonProcessingException { return "配置存储成功"; } - @ShellMethod(value = "配置删除",group = "config") - public String delConfig(String name){ - if(name==null||name.trim().isEmpty()) { + @ShellMethod(value = "配置删除", group = "config") + public String delConfig(String name) { + if (name == null || name.trim().isEmpty()) { return "配置名为空,无法删除!"; } configSavaRepository.deleteById(name); diff --git a/sugar_random_cli/src/main/java/me/mikusugar/sugar/random/cli/utils/CliUtils.java b/sugar_random_cli/src/main/java/me/mikusugar/sugar/random/cli/utils/CliUtils.java index 0ec1368..7518a7a 100644 --- a/sugar_random_cli/src/main/java/me/mikusugar/sugar/random/cli/utils/CliUtils.java +++ b/sugar_random_cli/src/main/java/me/mikusugar/sugar/random/cli/utils/CliUtils.java @@ -7,48 +7,73 @@ */ public class CliUtils { - /** - * 生成一颗树的形状 - * @param rootNode 根节点 - * @return 树形状的字符串 - */ - public static String JsonNodeToTreeStr(SugarJsonNode rootNode){ - StringBuilder res = new StringBuilder(); - dfs(rootNode, res, 0); - return res.toString(); - } + /** + * 生成一颗树的形状 + * + * @param rootNode 根节点 + * @return 树形状的字符串 + */ + public static String JsonNodeToTreeStr(SugarJsonNode rootNode) { + StringBuilder res = new StringBuilder(); + dfs(rootNode, res, 0); + return res.toString(); + } - /** - * 生成一颗🌲 - * - * @param node 节点对象 - * @param res 🌲的存储 - * @param level 层数 - */ - private static void dfs(SugarJsonNode node, StringBuilder res, final int level) { - if (node == null) return; - if (level == 0) { - res.append("🍭 root 根节点").append(System.lineSeparator()); - } else { - int num = level; - while (num-- > 1) res.append("│ "); - res.append("├──"); - res.append(node.getName()) - .append(" ") - .append(node.getType()) - .append(" ") - .append(node.getRandomServiceName()) - .append(" "); - String input; - if (node.getRandomService() == null - || node.getRandomService().getInput() == null) input = "没有配置"; - else input = node.getRandomService().getInput(); - res.append(input).append(System.lineSeparator()); + /** + * 生成一颗🌲 + * + * @param node 节点对象 + * @param res 🌲的存储 + * @param level 层数 + */ + private static void dfs(SugarJsonNode node, StringBuilder res, final int level) { + if (node == null) return; + if (level == 0) { + res.append("🍭 root 根节点").append(System.lineSeparator()); + } else { + int num = level; + while (num-- > 1) res.append("│ "); + res.append("├──"); + res.append(node.getName()) + .append(" ") + .append(node.getType()) + .append(" ") + .append(node.getRandomServiceName()) + .append(" "); + String input; + if (node.getRandomService() == null + || node.getRandomService().getInput() == null) input = "没有配置"; + else input = node.getRandomService().getInput(); + res.append(input).append(System.lineSeparator()); + } + if (node.getNexts() != null) { + node.getNexts().forEach(next -> dfs(next, res, level + 1)); + } } - if (node.getNexts() != null) { - node.getNexts().forEach(next -> dfs(next, res, level + 1)); + + public static String showField(String field) { + return "【" + field + "】"; } - } + /** + * 找到字段的父亲 + * + * @param rootNode 根节点 + * @param field 字段 + * @return 父亲字段 + */ + public static SugarJsonNode findFather(SugarJsonNode rootNode, String field) { + if (rootNode == null) return null; + if (rootNode.getNexts() != null) { + for (SugarJsonNode next : rootNode.getNexts()) { + if (next.getName().equals(field)) return rootNode; + } + for (SugarJsonNode next : rootNode.getNexts()) { + final SugarJsonNode res = findFather(next, field); + if (res != null) return res; + } + } + return null; + } } diff --git a/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomLonLat.java b/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomLonLat.java index 146acf6..0f5d0b6 100644 --- a/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomLonLat.java +++ b/sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomLonLat.java @@ -7,7 +7,6 @@ import org.springframework.stereotype.Service; import java.math.BigDecimal; -import java.util.Random; /** * @author mikusugar From 986da24954d366b62fc9b6193d1780adffd73af4 Mon Sep 17 00:00:00 2001 From: mikusugar Date: Fri, 16 Jul 2021 17:13:06 +0800 Subject: [PATCH 3/7] =?UTF-8?q?add:=20Cli=E5=90=AF=E5=8A=A8=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sugar_random_cli/src/main/resources/banner.txt | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100755 sugar_random_cli/src/main/resources/banner.txt diff --git a/sugar_random_cli/src/main/resources/banner.txt b/sugar_random_cli/src/main/resources/banner.txt new file mode 100755 index 0000000..1c9fe1b --- /dev/null +++ b/sugar_random_cli/src/main/resources/banner.txt @@ -0,0 +1,6 @@ + ____ ____ _ +/ ___| _ _ __ _ __ _ _ __ | _ \ __ _ _ __ __| | ___ _ __ ___ +\___ \ | | | | / _` | / _` || '__|| |_) | / _` || '_ \ / _` | / _ \ | '_ ` _ \ + ___) || |_| || (_| || (_| || | | _ < | (_| || | | || (_| || (_) || | | | | | +|____/ \__,_| \__, | \__,_||_| |_| \_\ \__,_||_| |_| \__,_| \___/ |_| |_| |_| + |___/ From fa3bb0fe4639a9178613d46ab26503b3afae15aa Mon Sep 17 00:00:00 2001 From: mikusugar Date: Mon, 19 Jul 2021 11:52:08 +0800 Subject: [PATCH 4/7] =?UTF-8?q?add:=20SugarJsonNode=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=88=B6=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/sugarrandom/SugarRandomView.java | 1 + .../me/mikusugar/sugar/random/cli/App.java | 10 ---- .../sugar/random/cli/service/CliService.java | 33 ++++++++++++ .../cli/service/CustomPromptProvider.java | 31 +++++++++++ .../sugar/random/cli/utils/CliUtils.java | 5 ++ .../random/core/bean/SugarJsonNode.java | 51 ++++++++++--------- .../utils/SugarJsonNodeSerialization.java | 25 +++++---- 7 files changed, 114 insertions(+), 42 deletions(-) create mode 100644 sugar_random_cli/src/main/java/me/mikusugar/sugar/random/cli/service/CustomPromptProvider.java diff --git a/src/main/java/me/mikusugar/randomsugar/app/views/sugarrandom/SugarRandomView.java b/src/main/java/me/mikusugar/randomsugar/app/views/sugarrandom/SugarRandomView.java index 8140ca4..cd58ad4 100755 --- a/src/main/java/me/mikusugar/randomsugar/app/views/sugarrandom/SugarRandomView.java +++ b/src/main/java/me/mikusugar/randomsugar/app/views/sugarrandom/SugarRandomView.java @@ -164,6 +164,7 @@ public void onComponentEvent(ClickEvent