diff --git a/AsLib/libBeardedManZhao.dll b/AsLib/libBeardedManZhao.dll index bcc0958..731de9b 100644 Binary files a/AsLib/libBeardedManZhao.dll and b/AsLib/libBeardedManZhao.dll differ diff --git a/README-Chinese.md b/README-Chinese.md index 090eb54..c1ce534 100644 --- a/README-Chinese.md +++ b/README-Chinese.md @@ -23,7 +23,7 @@ AS库目录有多个版本,如果希望查询不同版本的更新日志以及 io.github.BeardedManZhao algorithmStar - 1.30 + 1.31 ``` diff --git a/README.md b/README.md index bbc71f3..2f48446 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ can add it to your maven project, or you can download it from Releases and manua io.github.BeardedManZhao algorithmStar - 1.30 + 1.31 ``` diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index 4a31f50..1aff7ee 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -8,9 +8,12 @@ ### 更新日志 -* 框架版本:1.29 - 1.30 -* 新增单位数值类操作数,此操作数可以实现基本计算,同时还可以实现带有单位的计算效果,其还具有单位转换操作,且允许自定义单位!!! - * 内置了 BaseValue 类,其可以实现加减乘除运算,同时还支持单位数值的转换,可以直接使用 +* 框架版本:1.30 - 1.31 +* 针对单位数值中的减法进行修复,被减数与减数之间的位置进行的矫正 +* 针对单位数值中的乘除算法进行升级,您在自定义单位数值的时候可以通过在 `@BaseUnit` 中指定了 `needUnifiedUnit` + 属性,来决定计算乘除的时候是否需要进行单位统一,这有利于区分数值计算和物理计算,物理计算需要保持单位一致 +* 针对内置的单位数值进行矫正和升级,对于物理单位和数学单位能够实现各自的计算 +* 针对内置单位数值 `DateValue` 进行升级,其可以接收更多的时间单位 ```java package zhao.algorithmMagic; @@ -18,100 +21,39 @@ package zhao.algorithmMagic; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.BaseValueFactory; import zhao.algorithmMagic.operands.unit.BaseValue; - -import java.net.MalformedURLException; +import zhao.algorithmMagic.operands.unit.DateValue; public class MAIN1 { - public static void main(String[] args) { - // 构建一个用来创建 BaseValue.class 的工厂类 TODO 这里的类型可以是其它的 但是要确保是 BaseValue 的子类 - final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(BaseValue.class); - // 获取到数值 - final BaseValue number_1 = baseValueFactory.parse(200); - System.out.println("number_1 = " + number_1); - final BaseValue number_2 = baseValueFactory.parse(1024); - System.out.println("number_2 = " + number_2); - - // 基本的运算演示 - System.out.println("============"); - System.out.println(number_2 + " + " + number_1 + " = " + number_1.add(number_2)); - System.out.println(number_2 + " + " + 2 + number_2.getNowBase().getValue() + " = " + number_2.add(2)); - - System.out.println("============"); - System.out.println(number_2 + " - " + number_1 + " = " + number_1.diff(number_2)); - System.out.println(number_2 + " - " + 2 + number_2.getNowBase().getValue() + " = " + number_2.diff(2)); - - System.out.println("============"); - System.out.println(number_2 + " * " + number_1 + " = " + number_1.multiply(number_2)); - System.out.println(number_2 + " * " + 2 + number_2.getNowBase().getValue() + " = " + number_2.multiply(2)); - - System.out.println("============"); - System.out.println(number_2 + " / " + number_1 + " = " + number_1.divide(number_2)); - System.out.println(number_2 + " / " + 2 + number_2.getNowBase().getValue() + " = " + number_2.divide(2)); - - // 将 1.024千 转换为 10.24百 和 0.1024 万 - System.out.println("============"); - System.out.println(number_2.switchUnits("百")); - System.out.println(number_2.switchUnits("万")); - } -} - -``` - -* 如果需要自定义单位,可以直接继承 BaseValue 类,然后标注 BaseUnit 注解即可,下面是一个基本的实例(下面的类也已经加入到了AS库) - * 继承 BaseValue 并重写静态的 parse 函数 - * 标注 BaseUnit 注解 在其中设置单位 与 进制值 - -```java -package zhao.algorithmMagic.operands.unit; - -import zhao.algorithmMagic.utils.dataContainer.KeyValue; - -/** - * 重量单位数值,在这里可以直接使用重量相关的单位! - * - * Weight unit value, weight related units can be directly used here! - * @author zhao - */ -@BaseUnit(value = { - "t(吨)", "kg(千克)", "g(克)", "mg(毫克)", "ug(微克)", "ng(纳克)", "pg(皮克)", "fg(飞克)" -}, baseValue = 1000) -public class WeightValue extends BaseValue { - - protected WeightValue(double valueNumber, Class c, KeyValue baseNameKeyValue) { - super(valueNumber, c, baseNameKeyValue); - } - /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed - * @return 解析之后的单位数值对象 - *

- * Parsed Unit Value Object - */ - public static BaseValue parse(double valueNumber) { - return parse(valueNumber, null); - } - - /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed - * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. - *

- * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. - * @return 解析之后的单位数值对象 - *

- * Parsed Unit Value Object - */ - protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue) { - return new BaseValue(valueNumber, WeightValue.class, baseNameKeyValue); - } + public static void main(String[] args) { + // 在这里我们获取到的就是单位数值的工厂类 在这里的函数参数是工厂要构造的单位数值的类型 + // 请确保您在这里提供的单位数值类具有 @BaseUnit 注解和 parse 函数 + // 内置的单位数值类都是有 @BaseUnit 注解和 parse 函数的,如果您有自定义单位数值的需求需要注意 + final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(DateValue.class); + final BaseValue parse1 = baseValueFactory.parse(2000); + // 使用工厂类 准备一个时间对象 这里的单位是毫秒开始的 所以这个数值是 1.024 秒 + final BaseValue parse2 = baseValueFactory.parse(1024); + // 在这里我们再构建一个 1 天 + final BaseValue parse3 = baseValueFactory.parse(24 * 60 * 60 * 1000); + System.out.println(parse3); + + // 打印结果 + System.out.println(parse2); + // 计算 1.024 秒 / 2 + System.out.println(parse2.divide(2)); + // 计算 1.024 秒 / 2000 毫秒 如果 isNeedUnifiedUnit = true 代表的这里会先将 单位统一然后再计算 + // TODO 针对乘除法来说 一般可能不需要带着单位计算哦! + System.out.println(parse2.divide(parse1)); + // 计算 1天 * 2 + System.out.println(parse3.multiply(2)); + + // 查看此单位数值是否需要进行单位统一再计算 + System.out.println(parse2.isNeedUnifiedUnit()); + } } - ``` -然后就可以进行使用啦 +* 针对单位数值注解 `@BaseUnit` 进行升级,其可以实现自定义单位的进制,也可以像原来一样指定一个通用进制,通用进制会形成一种等比效应。 ```java package zhao.algorithmMagic; @@ -119,41 +61,41 @@ package zhao.algorithmMagic; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.BaseValueFactory; import zhao.algorithmMagic.operands.unit.BaseValue; -import zhao.algorithmMagic.operands.unit.WeightValue; - -import java.net.MalformedURLException; +import zhao.algorithmMagic.operands.unit.DateValue; public class MAIN1 { + public static void main(String[] args) { - // 构建一个用来创建 WeightValue.class 的工厂类 TODO 这里的类型可以是其它的 但是要确保是 BaseValue 的子类 - final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(WeightValue.class); - // 获取到数值 - final BaseValue number_1 = baseValueFactory.parse(200); - System.out.println("number_1 = " + number_1); - // 将第一个的单位转换为 克 - number_1.switchUnitsNotChange("g(克)"); - System.out.println("number_1 = " + number_1); - // 获取到第二个数值 - final BaseValue number_2 = baseValueFactory.parse(102.4); - // 将第二个转换为 毫克 - number_2.switchUnitsNotChange("mg(毫克)"); - System.out.println("number_2 = " + number_2); - // 进行一个计算 - System.out.println("number_1 + number_2 = " + number_1.add(number_2)); + // 在这里我们获取到的就是单位数值的工厂类 在这里的函数参数是工厂要构造的单位数值的类型 + // 请确保您在这里提供的单位数值类具有 @BaseUnit 注解和 parse 函数 + final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(BaseValue.class); + // 使用工厂类 将 1024 转换成为一个单位数值 + final BaseValue parse1 = baseValueFactory.parse(2); + // 使用工厂类 将 1024000 转换成为一个单位数值 + final BaseValue parse2 = baseValueFactory.parse(1024); + // 计算加减乘除结果 + System.out.println(parse2.add(parse1)); + System.out.println(parse2.diff(parse1)); + System.out.println(parse2.multiply(parse1)); + System.out.println(parse2.divide(parse1)); + + System.out.println("=============="); + + final BaseValueFactory baseValueFactory1 = AlgorithmStar.baseValueFactory(DateValue.class); + final BaseValue parse11 = baseValueFactory1.parse(100); + final BaseValue parse12 = baseValueFactory1.parse(10000); + final BaseValue parse23 = baseValueFactory1.parse(1000 * 60); + System.out.println(parse11); + System.out.println(parse12); + System.out.println(parse23); + System.out.println(parse12.add(parse11)); + System.out.println(parse12.diff(parse11)); + System.out.println(parse12.multiply(parse11)); + System.out.println(parse12.divide(parse11)); + + System.out.println(parse23.divide(parse12)); } } - ``` -* 内置的单位数值类 - -| 类路径 | 名称 | 加入版本 | 支持计算 | -|-----------------------------------------------|---------|-------|------| -| zhao.algorithmMagic.operands.unit.BaseValue | 基础单位数值类 | v1.30 | yes | -| zhao.algorithmMagic.operands.unit.DataValue | 数据单位数值类 | v1.30 | yes | -| zhao.algorithmMagic.operands.unit.VolumeValue | 容量单位数值类 | v1.30 | yes | -| zhao.algorithmMagic.operands.unit.WeightValue | 重量单位数值类 | v1.30 | yes | -| zhao.algorithmMagic.operands.unit.DateValue | 时间单位数值类 | v1.30 | yes | - - -### Version update date : 2024-01-30 \ No newline at end of file +### Version update date : 2024-02-09 \ No newline at end of file diff --git a/src_code/README.md b/src_code/README.md index 726d64e..42c72d1 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -8,9 +8,14 @@ ### Update log: -* Framework version: 1.29 - 1.30 -* Add unit value operands, which can achieve basic calculations and also achieve calculation effects with units. They also have unit conversion operations and allow custom units!!! - * Built in BaseValue class, it can implement addition, subtraction, multiplication, and division operations, and also supports conversion of unit values, which can be directly used +* Framework version: 1.30 - 1.31 +* Fix subtraction in unit values and correct the position between the subtracted and subtracted numbers +* Upgrade the multiplication and division algorithm for unit values. When customizing unit values, you can determine + whether unit unity is required when calculating multiplication and division by specifying the 'needUnifiedUnit' + attribute in `@BaseUnit`. This helps to distinguish between numerical and physical calculations, and physical + calculations need to maintain unit consistency +* Correct and upgrade built-in unit values, enabling separate calculations for physical and mathematical units +* Upgrade for built-in unit value `DateValue`, which can receive more time units ```java package zhao.algorithmMagic; @@ -18,100 +23,40 @@ package zhao.algorithmMagic; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.BaseValueFactory; import zhao.algorithmMagic.operands.unit.BaseValue; - -import java.net.MalformedURLException; +import zhao.algorithmMagic.operands.unit.DateValue; public class MAIN1 { - public static void main(String[] args) { - // 构建一个用来创建 BaseValue.class 的工厂类 TODO 这里的类型可以是其它的 但是要确保是 BaseValue 的子类 - final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(BaseValue.class); - // 获取到数值 - final BaseValue number_1 = baseValueFactory.parse(200); - System.out.println("number_1 = " + number_1); - final BaseValue number_2 = baseValueFactory.parse(1024); - System.out.println("number_2 = " + number_2); - - // 基本的运算演示 - System.out.println("============"); - System.out.println(number_2 + " + " + number_1 + " = " + number_1.add(number_2)); - System.out.println(number_2 + " + " + 2 + number_2.getNowBase().getValue() + " = " + number_2.add(2)); - - System.out.println("============"); - System.out.println(number_2 + " - " + number_1 + " = " + number_1.diff(number_2)); - System.out.println(number_2 + " - " + 2 + number_2.getNowBase().getValue() + " = " + number_2.diff(2)); - - System.out.println("============"); - System.out.println(number_2 + " * " + number_1 + " = " + number_1.multiply(number_2)); - System.out.println(number_2 + " * " + 2 + number_2.getNowBase().getValue() + " = " + number_2.multiply(2)); - - System.out.println("============"); - System.out.println(number_2 + " / " + number_1 + " = " + number_1.divide(number_2)); - System.out.println(number_2 + " / " + 2 + number_2.getNowBase().getValue() + " = " + number_2.divide(2)); - - // 将 1.024千 转换为 10.24百 和 0.1024 万 - System.out.println("============"); - System.out.println(number_2.switchUnits("百")); - System.out.println(number_2.switchUnits("万")); - } -} - -``` - -* If you need to customize the unit, you can directly inherit the BaseValue class and annotate it with BaseUnit annotation. Below is a basic instance (the following class has also been added to the AS library) - * Inherit BaseValue and rewrite the static parse function - * Annotate BaseUnit to set units and base values in it - -```java -package zhao.algorithmMagic.operands.unit; - -import zhao.algorithmMagic.utils.dataContainer.KeyValue; - -/** - * 重量单位数值,在这里可以直接使用重量相关的单位! - * - * Weight unit value, weight related units can be directly used here! - * @author zhao - */ -@BaseUnit(value = { - "t(吨)", "kg(千克)", "g(克)", "mg(毫克)", "ug(微克)", "ng(纳克)", "pg(皮克)", "fg(飞克)" -}, baseValue = 1000) -public class WeightValue extends BaseValue { - - protected WeightValue(double valueNumber, Class c, KeyValue baseNameKeyValue) { - super(valueNumber, c, baseNameKeyValue); - } - /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed - * @return 解析之后的单位数值对象 - *

- * Parsed Unit Value Object - */ - public static BaseValue parse(double valueNumber) { - return parse(valueNumber, null); - } - - /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed - * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. - *

- * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. - * @return 解析之后的单位数值对象 - *

- * Parsed Unit Value Object - */ - protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue) { - return new BaseValue(valueNumber, WeightValue.class, baseNameKeyValue); - } + public static void main(String[] args) { + // 在这里我们获取到的就是单位数值的工厂类 在这里的函数参数是工厂要构造的单位数值的类型 + // 请确保您在这里提供的单位数值类具有 @BaseUnit 注解和 parse 函数 + // 内置的单位数值类都是有 @BaseUnit 注解和 parse 函数的,如果您有自定义单位数值的需求需要注意 + final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(DateValue.class); + final BaseValue parse1 = baseValueFactory.parse(2000); + // 使用工厂类 准备一个时间对象 这里的单位是毫秒开始的 所以这个数值是 1.024 秒 + final BaseValue parse2 = baseValueFactory.parse(1024); + // 在这里我们再构建一个 1 天 + final BaseValue parse3 = baseValueFactory.parse(24 * 60 * 60 * 1000); + System.out.println(parse3); + + // 打印结果 + System.out.println(parse2); + // 计算 1.024 秒 / 2 + System.out.println(parse2.divide(2)); + // 计算 1.024 秒 / 2000 毫秒 如果 isNeedUnifiedUnit = true 代表的这里会先将 单位统一然后再计算 + // TODO 针对乘除法来说 一般可能不需要带着单位计算哦! + System.out.println(parse2.divide(parse1)); + // 计算 1天 * 2 + System.out.println(parse3.multiply(2)); + + // 查看此单位数值是否需要进行单位统一再计算 + System.out.println(parse2.isNeedUnifiedUnit()); + } } - ``` -Then you can start using it +* Upgrade the annotation `@BaseUnit` for unit numerical values, which can implement custom unit bases or specify a + universal base as before. Universal base will form a proportional effect. ```java package zhao.algorithmMagic; @@ -119,41 +64,41 @@ package zhao.algorithmMagic; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.BaseValueFactory; import zhao.algorithmMagic.operands.unit.BaseValue; -import zhao.algorithmMagic.operands.unit.WeightValue; - -import java.net.MalformedURLException; +import zhao.algorithmMagic.operands.unit.DateValue; public class MAIN1 { + public static void main(String[] args) { - // 构建一个用来创建 WeightValue.class 的工厂类 TODO 这里的类型可以是其它的 但是要确保是 BaseValue 的子类 - final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(WeightValue.class); - // 获取到数值 - final BaseValue number_1 = baseValueFactory.parse(200); - System.out.println("number_1 = " + number_1); - // 将第一个的单位转换为 克 - number_1.switchUnitsNotChange("g(克)"); - System.out.println("number_1 = " + number_1); - // 获取到第二个数值 - final BaseValue number_2 = baseValueFactory.parse(102.4); - // 将第二个转换为 毫克 - number_2.switchUnitsNotChange("mg(毫克)"); - System.out.println("number_2 = " + number_2); - // 进行一个计算 - System.out.println("number_1 + number_2 = " + number_1.add(number_2)); + // 在这里我们获取到的就是单位数值的工厂类 在这里的函数参数是工厂要构造的单位数值的类型 + // 请确保您在这里提供的单位数值类具有 @BaseUnit 注解和 parse 函数 + final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(BaseValue.class); + // 使用工厂类 将 1024 转换成为一个单位数值 + final BaseValue parse1 = baseValueFactory.parse(2); + // 使用工厂类 将 1024000 转换成为一个单位数值 + final BaseValue parse2 = baseValueFactory.parse(1024); + // 计算加减乘除结果 + System.out.println(parse2.add(parse1)); + System.out.println(parse2.diff(parse1)); + System.out.println(parse2.multiply(parse1)); + System.out.println(parse2.divide(parse1)); + + System.out.println("=============="); + + final BaseValueFactory baseValueFactory1 = AlgorithmStar.baseValueFactory(DateValue.class); + final BaseValue parse11 = baseValueFactory1.parse(100); + final BaseValue parse12 = baseValueFactory1.parse(10000); + final BaseValue parse23 = baseValueFactory1.parse(1000 * 60); + System.out.println(parse11); + System.out.println(parse12); + System.out.println(parse23); + System.out.println(parse12.add(parse11)); + System.out.println(parse12.diff(parse11)); + System.out.println(parse12.multiply(parse11)); + System.out.println(parse12.divide(parse11)); + + System.out.println(parse23.divide(parse12)); } } - ``` -* Built in unit value class. - -| 类路径 | 名称 | 加入版本 | 支持计算 | -|-----------------------------------------------|---------|-------|------| -| zhao.algorithmMagic.operands.unit.BaseValue | 基础单位数值类 | v1.30 | yes | -| zhao.algorithmMagic.operands.unit.DataValue | 数据单位数值类 | v1.30 | yes | -| zhao.algorithmMagic.operands.unit.VolumeValue | 容量单位数值类 | v1.30 | yes | -| zhao.algorithmMagic.operands.unit.WeightValue | 重量单位数值类 | v1.30 | yes | -| zhao.algorithmMagic.operands.unit.DateValue | 时间单位数值类 | v1.30 | yes | - - -### Version update date : 2024-01-30 \ No newline at end of file +### Version update date : 2024-02-09 \ No newline at end of file diff --git a/src_code/pom.xml b/src_code/pom.xml index 71ec533..63ab338 100644 --- a/src_code/pom.xml +++ b/src_code/pom.xml @@ -5,7 +5,7 @@ io.github.BeardedManZhao algorithmStar - 1.30 + 1.31 jar algorithmStar algorithmStar-java diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index 2adeb8a..8cbb97c 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -1,13 +1,10 @@ package zhao.algorithmMagic; -import zhao.algorithmMagic.core.AlgorithmStar; -import zhao.algorithmMagic.core.HelpFactory; +import zhao.algorithmMagic.algorithm.OperationAlgorithmManager; public class MAIN1 { + public static void main(String[] args) { - // 获取帮助信息工厂类 - final HelpFactory helpFactory = AlgorithmStar.helpFactory(); - // 下载帮助文档 - System.out.println("帮助文档保存到:" + helpFactory.saveHelpFile(HelpFactory.ALL, "D:/")); + System.out.println(OperationAlgorithmManager.VERSION); } } \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/OperationAlgorithmManager.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/OperationAlgorithmManager.java index de65e24..7462975 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/OperationAlgorithmManager.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/OperationAlgorithmManager.java @@ -21,7 +21,7 @@ public final class OperationAlgorithmManager implements OperationAlgorithm { /** * AS version */ - public final static float VERSION = 1.30f; + public final static float VERSION = 1.31f; /** * 计算组件的日志打印开关,当此处值为false的时候,计算组件中的日志将不会被打印,logger也不会被调用,一般来说,这里为了减少冗余的字符串实例化操作,会设置为false,当需要调试的时候才需要打开此处的数值。 diff --git a/src_code/src/main/java/zhao/algorithmMagic/core/BaseValueFactory.java b/src_code/src/main/java/zhao/algorithmMagic/core/BaseValueFactory.java index a69d3b1..a91d6b0 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/core/BaseValueFactory.java +++ b/src_code/src/main/java/zhao/algorithmMagic/core/BaseValueFactory.java @@ -16,9 +16,9 @@ public class BaseValueFactory { public BaseValueFactory(Class baseValueClass) { try { - this.parseM1 = baseValueClass.getMethod("parse", double.class); + this.parseM1 = baseValueClass.getMethod("parse", double.class, BaseValueFactory.class); } catch (NoSuchMethodException e) { - throw new UnsupportedOperationException("请在 " + baseValueClass.getTypeName() + " 中实现一个 public static BaseValue parse(double valueNumber) 函数!!!"); + throw new UnsupportedOperationException("请在 " + baseValueClass.getTypeName() + " 中实现一个 public static BaseValue parse(double valueNumber, BaseValueFactory baseValueFactoryClass) 函数!!!"); } } @@ -32,7 +32,7 @@ public BaseValueFactory(Class baseValueClass) { */ public BaseValue parse(double number) { try { - return (BaseValue) parseM1.invoke(null, number); + return (BaseValue) parseM1.invoke(null, number, this); } catch (IllegalAccessException | InvocationTargetException e) { throw new UnsupportedOperationException(e); } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseUnit.java b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseUnit.java index 1b9f2f9..50f8901 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseUnit.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseUnit.java @@ -4,7 +4,7 @@ /** * 进制数值单位注解,您可以在这里设置一个进制数值单位的进制数值,以及每个子单位的名字,请确保每个子单位之间的进制数值是个等比数列。 - * + *

* Annotations for base numerical units. You can set a base numerical unit and the name of each subunit here. Please ensure that the base numerical values between each subunit are a proportional sequence. */ @Target(ElementType.TYPE) @@ -20,10 +20,34 @@ */ String[] value(); + /** * @return 每个相邻子单位之间的进制数值。 *

- * The decimal value between each adjacent subunit. + * 如果只设置了一个元素,则这里代表的就是每个相邻的子单位之间的进制差,例如:["米", "分米", "厘米"] 中的每个单位的进制差都是 10 所以您可以在这里直接写 10; + *

+ * 如果是多个元素就代表指定位置的子单位距离它下一个单位的禁止差 例如:["h","m","s","ms"] 中 进制差为 [60, 60, 1000, 0] 代表的就是下一个单位与当前单位的倍数 + *

+ * The decimal value between each adjacent subunit. If only one element is set, it represents the base difference between each adjacent subunit. For example, the base difference for each unit in ["meter", "decimeter", "centimeter"] is 10, so you can directly write 10 here; If there are multiple elements, it represents the forbidden difference between the subunit at the specified position and its next unit. For example, in ["h", "m", "s", "ms"], the decimal difference is [60, 60, 1000, 0], which represents the multiple of the current unit for the next unit + */ + double[] baseValue() default 10; + + /** + * @return 在数学中,乘法运算需要注意两个方面:数字和单位。 + *

+ * 当数字相乘时,可以直接进行乘法运算。例如,2升乘以2升,数字部分是2乘以2,结果是4。 + *

+ * 当单位相乘时,需要先统一单位,然后再进行乘法运算。例如,2米乘以2米,单位是米,可以直接相乘得到4平方米。但如果单位不同,比如2米乘以2厘米,需要先统一单位,把2厘米转换为米(即0.01米),然后再相乘得到0.04平方米。 + *

+ * 因此,在进行乘法运算时,需要先判断数字和单位是否一致,如果单位不一致需要先统一单位再进行计算。 + *

+ * In mathematics, multiplication operations require attention to two aspects: numbers and units. + *

+ * When multiplying numbers, multiplication can be performed directly. For example, multiplying 2 liters by 2 liters, the numerical part is 2 times 2, and the result is 4. + *

+ * When multiplying units, it is necessary to first unify the units before performing multiplication operations. For example, multiplying 2 meters by 2 meters in meters can directly yield 4 square meters. But if the units are different, such as multiplying 2 meters by 2 centimeters, it is necessary to first unify the units, convert 2 centimeters to meters (i.e. 0.01 meters), and then multiply them to obtain 0.04 square meters. + *

+ * Therefore, when performing multiplication operations, it is necessary to first check whether the numbers and units are consistent. If the units are not consistent, it is necessary to first unify the units before calculating. */ - double baseValue() default 10; + boolean needUnifiedUnit() default true; } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseUnitObj.java b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseUnitObj.java index 3259233..f5b5368 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseUnitObj.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseUnitObj.java @@ -35,7 +35,7 @@ static TempBaseData parse(BaseUnit baseUnit) { return tempBaseData; } final double[] doubles = new double[value.length]; - double number = 1, base = baseUnit.baseValue(); + double number = 1, base = baseUnit.baseValue()[0]; HashMap hashMap = new HashMap<>(); // 构建位权 int length = value.length - 1; @@ -46,7 +46,7 @@ static TempBaseData parse(BaseUnit baseUnit) { doubles[length] = number *= base; hashMap.put(value[length], length); } - final TempBaseData tempBaseData1 = new TempBaseData(base, doubles, value, hashMap); + final TempBaseData tempBaseData1 = new TempBaseData(base, doubles, value, hashMap, baseUnit.needUnifiedUnit()); TEMP_BASE_DATA_HASH_MAP.put(baseUnit, tempBaseData1); return tempBaseData1; } @@ -135,11 +135,41 @@ static TempBaseData parse(BaseUnit baseUnit) { */ double[] getBaseWeight(); + /** + * @return 当前单位对象中的每个子单位的进制差,位权按照从大到小的顺序迭代。 + *

+ * The bit weight of each subunit in the current unit object. Bit weight is iterated in ascending order. + * 例如:["h","m","s","ms"] 中 进制差为 [60, 60, 1000, 0] 代表的就是下一个单位举例当前单位的倍数 + */ + double[] getBaseDiff(); + + /** + * 如果需要设置为 true;Set to true if necessary + * + * @return 在数学中,乘法运算需要注意两个方面:数字和单位。 + *

+ * 当数字相乘时,可以直接进行乘法运算。例如,2升乘以2升,数字部分是2乘以2,结果是4。 + *

+ * 当单位相乘时,需要先统一单位,然后再进行乘法运算。例如,2米乘以2米,单位是米,可以直接相乘得到4平方米。但如果单位不同,比如2米乘以2厘米,需要先统一单位,把2厘米转换为米(即0.01米),然后再相乘得到0.04平方米。 + *

+ * 因此,在进行乘法运算时,需要先判断数字和单位是否一致,如果单位不一致需要先统一单位再进行计算。 + *

+ * In mathematics, multiplication operations require attention to two aspects: numbers and units. + *

+ * When multiplying numbers, multiplication can be performed directly. For example, multiplying 2 liters by 2 liters, the numerical part is 2 times 2, and the result is 4. + *

+ * When multiplying units, it is necessary to first unify the units before performing multiplication operations. For example, multiplying 2 meters by 2 meters in meters can directly yield 4 square meters. But if the units are different, such as multiplying 2 meters by 2 centimeters, it is necessary to first unify the units, convert 2 centimeters to meters (i.e. 0.01 meters), and then multiply them to obtain 0.04 square meters. + *

+ * Therefore, when performing multiplication operations, it is necessary to first check whether the numbers and units are consistent. If the units are not consistent, it is necessary to first unify the units before calculating. + */ + boolean isNeedUnifiedUnit(); + /** * 位权单位元数据存储类 */ final class TempBaseData { private final HashMap hashMap; + private final boolean needUnifiedUnit; private final double baseValue; private final double[] baseWeights; private final String[] baseNames; @@ -147,16 +177,18 @@ final class TempBaseData { /** * 构造函数 * - * @param baseValue 进制数值 - * @param baseWeights 位权数组 - * @param baseNames 子单位名称 - * @param hashMap 子单位哈希索引 + * @param baseValue 进制数值 + * @param baseWeights 位权数组 + * @param baseNames 子单位名称 + * @param hashMap 子单位哈希索引 + * @param needUnifiedUnit 计算乘除的时候是否需要将单位统一 */ - public TempBaseData(double baseValue, double[] baseWeights, String[] baseNames, HashMap hashMap) { + public TempBaseData(double baseValue, double[] baseWeights, String[] baseNames, HashMap hashMap, boolean needUnifiedUnit) { this.baseValue = baseValue; this.baseWeights = baseWeights; this.baseNames = baseNames; this.hashMap = hashMap; + this.needUnifiedUnit = needUnifiedUnit; } public HashMap getHashMap() { @@ -174,6 +206,10 @@ public double[] getBaseWeights() { public String[] getBaseNames() { return baseNames; } + + public boolean isNeedUnifiedUnit() { + return needUnifiedUnit; + } } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseValue.java b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseValue.java index bd66579..8ff3148 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseValue.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/BaseValue.java @@ -1,5 +1,6 @@ package zhao.algorithmMagic.operands.unit; +import zhao.algorithmMagic.core.BaseValueFactory; import zhao.algorithmMagic.operands.Operands; import zhao.algorithmMagic.utils.dataContainer.KeyValue; @@ -12,27 +13,37 @@ */ @BaseUnit(value = { "亿", "千万", "百万", "十万", "万", "千", "百", "十", "个" -}) +}, needUnifiedUnit = false) public class BaseValue extends Number implements Operands { private final static Class BaseValueClass = BaseValue.class; protected final double valueNumber; + protected final BaseValueFactory baseValueFactoryClass; protected final BaseUnitObj baseUnitObj; + protected double srcValue; protected KeyValue baseNameKeyValue; - public BaseValue(double valueNumber, Class c, KeyValue baseNameKeyValue) { - this(valueNumber, MatchBaseUnit.parse(c), baseNameKeyValue); + public BaseValue(double valueNumber, Class c, KeyValue baseNameKeyValue, BaseValueFactory baseValueFactoryClass) { + this(valueNumber, baseValueFactoryClass, MatchBaseUnit.parse(c), baseNameKeyValue); } - protected BaseValue(double valueNumber, BaseUnitObj baseUnitObj, KeyValue baseNameKeyValue) { + protected BaseValue(double valueNumber, BaseValueFactory baseValueFactoryClass, BaseUnitObj baseUnitObj, KeyValue baseNameKeyValue) { + this.baseValueFactoryClass = baseValueFactoryClass; final boolean b = baseNameKeyValue == null; + srcValue = valueNumber; this.baseNameKeyValue = b ? baseUnitObj.getBaseNameByValue(valueNumber) : baseNameKeyValue; - final double baseValue = baseUnitObj.getBaseValue(); - int index = baseUnitObj.getBaseWeight().length; + final double[] baseValue = baseUnitObj.getBaseDiff(); + final double[] baseWeight = baseUnitObj.getBaseWeight(); + final double baseValue1 = baseUnitObj.getBaseValue(); if (b) { - // 对数值进行化简 - while (valueNumber >= baseValue && --index > this.baseNameKeyValue.getKey()) { - valueNumber /= baseValue; + int index = 0; + for (double v : baseValue) { + if (valueNumber >= baseWeight[index++]) { + valueNumber /= v; + } + if (baseValue1 > valueNumber) { + break; + } } } this.valueNumber = valueNumber; @@ -40,30 +51,36 @@ protected BaseValue(double valueNumber, BaseUnitObj baseUnitObj, KeyValue - * Value that needs to be parsed + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseValueFactoryClass 单位数值构造工厂类 + *

+ * Unit Value Construction Factory Class * @return 解析之后的单位数值对象 *

* Parsed Unit Value Object */ - public static BaseValue parse(double valueNumber) { - return parse(valueNumber, null); + public static BaseValue parse(double valueNumber, BaseValueFactory baseValueFactoryClass) { + return parse(valueNumber, null, baseValueFactoryClass); } /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed - * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. - *

- * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. + *

+ * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @param baseValueFactoryClass 单位数值构造工厂类 + *

+ * Unit Value Construction Factory Class * @return 解析之后的单位数值对象 *

* Parsed Unit Value Object */ - protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue) { - return new BaseValue(valueNumber, BaseValueClass, baseNameKeyValue); + protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue, BaseValueFactory baseValueFactoryClass) { + return new BaseValue(valueNumber, BaseValueClass, baseNameKeyValue, baseValueFactoryClass); } public BaseUnitObj getBaseUnitObj() { @@ -94,7 +111,7 @@ public BaseValue add(BaseValue value) { // 切换被操作数的权值 新值 final BaseValue nowBase1 = value.switchUnits(this.getNowBase()); // 直接使用自己和被操作数计算 使用新值的单位 - return new BaseValue(nowBase1.doubleValue() + this.doubleValue(), nowBase1.baseUnitObj, nowBase1.getNowBase()); + return new BaseValue(this.doubleValue() + nowBase1.doubleValue(), baseValueFactoryClass, nowBase1.baseUnitObj, nowBase1.getNowBase()); } /** @@ -111,7 +128,7 @@ public BaseValue diff(BaseValue value) { // 切换被操作数的权值 新值 final BaseValue nowBase1 = value.switchUnits(this.getNowBase()); // 直接使用自己和被操作数计算 使用新值的单位 - return new BaseValue(nowBase1.doubleValue() - this.doubleValue(), nowBase1.baseUnitObj, nowBase1.getNowBase()); + return new BaseValue(this.doubleValue() - nowBase1.doubleValue(), baseValueFactoryClass, nowBase1.baseUnitObj, nowBase1.getNowBase()); } /** @@ -127,7 +144,7 @@ public BaseValue diff(BaseValue value) { @Override public BaseValue add(Number value) { // 直接使用自己和被操作数计算 使用新值的单位 - return new BaseValue(this.doubleValue() + value.doubleValue(), this.getBaseUnitObj(), this.getNowBase()); + return new BaseValue(this.doubleValue() + value.doubleValue(), baseValueFactoryClass, this.getBaseUnitObj(), this.getNowBase()); } /** @@ -142,7 +159,7 @@ public BaseValue add(Number value) { @Override public BaseValue diff(Number value) { // 直接使用自己和被操作数计算 使用新值的单位 - return new BaseValue(this.doubleValue() - value.doubleValue(), this.getBaseUnitObj(), this.getNowBase()); + return new BaseValue(this.doubleValue() - value.doubleValue(), baseValueFactoryClass, this.getBaseUnitObj(), this.getNowBase()); } /** @@ -158,10 +175,15 @@ public BaseValue diff(Number value) { * The calculation result will return a new unit value object. */ public BaseValue multiply(BaseValue value) { - // 切换被操作数的权值 新值 - final BaseValue nowBase1 = value.switchUnits(this.getNowBase()); + if (!this.baseUnitObj.isNeedUnifiedUnit()) { + // 切换被操作数的权值 新值 + final String[] baseName = this.baseUnitObj.getBaseName(); + final BaseValue nowBase1 = value.switchUnits(baseName[baseName.length - 1]); + // 直接使用自己和被操作数计算 使用新值的单位 + return new BaseValue(this.doubleValue() * nowBase1.doubleValue(), baseValueFactoryClass, this.baseUnitObj, this.getNowBase()); + } // 直接使用自己和被操作数计算 使用新值的单位 - return new BaseValue(nowBase1.doubleValue() * this.doubleValue(), nowBase1.baseUnitObj, nowBase1.getNowBase()); + return this.baseValueFactoryClass.parse(this.getSrcValue() * value.getSrcValue()); } /** @@ -177,10 +199,24 @@ public BaseValue multiply(BaseValue value) { * The calculation result will return a new unit value object. */ public BaseValue divide(BaseValue value) { - // 切换被操作数的权值 新值 - final BaseValue nowBase1 = value.switchUnits(this.getNowBase()); + if (!this.baseUnitObj.isNeedUnifiedUnit()) { + // 切换被操作数的权值 新值 + final String[] baseName = this.baseUnitObj.getBaseName(); + final BaseValue nowBase1 = value.switchUnits(baseName[baseName.length - 1]); + // 直接使用自己和被操作数计算 使用新值的单位 + return new BaseValue(this.doubleValue() / nowBase1.doubleValue(), baseValueFactoryClass, this.baseUnitObj, this.getNowBase()); + } // 直接使用自己和被操作数计算 使用新值的单位 - return new BaseValue(nowBase1.doubleValue() / this.doubleValue(), nowBase1.baseUnitObj, nowBase1.getNowBase()); + return this.baseValueFactoryClass.parse(this.getSrcValue() / value.getSrcValue()); + } + + /** + * @return 获取到最低位权单位对应的数值,也是原本的数值,没有经过化简的数值。 + *

+ * The value corresponding to the lowest weighted unit obtained is also the original value, which has not been simplified. + */ + public double getSrcValue() { + return srcValue; } /** @@ -196,7 +232,7 @@ public BaseValue divide(BaseValue value) { * The calculation result will return a new unit value object. */ public BaseValue multiply(Number value) { - return new BaseValue(this.doubleValue() * value.doubleValue(), this.getBaseUnitObj(), this.getNowBase()); + return new BaseValue(this.doubleValue() * value.doubleValue(), baseValueFactoryClass, this.getBaseUnitObj(), this.getNowBase()); } /** @@ -212,7 +248,7 @@ public BaseValue multiply(Number value) { * The calculation result will return a new unit value object. */ public BaseValue divide(Number value) { - return new BaseValue(this.doubleValue() / value.doubleValue(), this.getBaseUnitObj(), this.getNowBase()); + return new BaseValue(this.doubleValue() / value.doubleValue(), baseValueFactoryClass, this.getBaseUnitObj(), this.getNowBase()); } /** @@ -231,6 +267,19 @@ public BaseValue switchUnits(KeyValue targetUnit) { return baseUnitObj.switchBaseName(this, targetUnit); } + /** + * 不改变数值的方式,只改变单位度量。 + *

+ * Do not change the numerical value, only change the unit measurement. + * + * @param targetUnit 需要被切换到的目标单位 + *

+ * The target unit that needs to be switched to + */ + public void switchUnitsNotChange(KeyValue targetUnit) { + this.baseNameKeyValue = targetUnit; + } + /** * 不改变数值的方式,只改变单位度量。 *

@@ -241,7 +290,7 @@ public BaseValue switchUnits(KeyValue targetUnit) { * The target unit that needs to be switched to */ public void switchUnitsNotChange(String targetUnit) { - this.baseNameKeyValue = new KeyValue<>(this.getBaseUnitObj().getIndexByBaseName(targetUnit), targetUnit); + this.switchUnitsNotChange(new KeyValue<>(this.getBaseUnitObj().getIndexByBaseName(targetUnit), targetUnit)); } /** @@ -271,7 +320,7 @@ public BaseValue switchUnits(String targetUnit) { */ @Override public BaseValue expand() { - return null; + return this; } /** @@ -322,6 +371,15 @@ public double doubleValue() { return this.valueNumber; } + /** + * @return 是否需要统一单位 + *

+ * Whether to unify units + */ + public boolean isNeedUnifiedUnit() { + return this.baseUnitObj.isNeedUnifiedUnit(); + } + /** * @return The string representation of the object. */ @@ -329,4 +387,13 @@ public double doubleValue() { public String toString() { return this.doubleValue() + this.getNowBase().getValue(); } + + /** + * @return 本单位数值对象对应的工厂类 + *

+ * The factory class corresponding to the numerical object of this unit + */ + public BaseValueFactory getBaseValueFactoryClass() { + return baseValueFactoryClass; + } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/DataValue.java b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/DataValue.java index 7c8f91e..bcb8658 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/DataValue.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/DataValue.java @@ -1,45 +1,55 @@ package zhao.algorithmMagic.operands.unit; +import zhao.algorithmMagic.core.BaseValueFactory; import zhao.algorithmMagic.utils.dataContainer.KeyValue; /** * 数据进制单位,可以实现数据单位相关的计算 - * + *

* Data unit, which can achieve calculations related to data units + * * @author zhao */ @BaseUnit(value = { - "YB" ,"ZB" ,"EB" ,"TB", "GB", "MB", "KB", "B" + "YB", "ZB", "EB", "TB", "GB", "MB", "KB", "B" }, baseValue = 1024) -public class DataValue extends BaseValue{ - public DataValue(double valueNumber, Class c, KeyValue baseNameKeyValue) { - super(valueNumber, c, baseNameKeyValue); +public class DataValue extends BaseValue { + + + public DataValue(double valueNumber, Class c, KeyValue baseNameKeyValue, BaseValueFactory baseValueFactoryClass) { + super(valueNumber, c, baseNameKeyValue, baseValueFactoryClass); } /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseValueFactoryClass 单位数值构造工厂类 + *

+ * Unit Value Construction Factory Class * @return 解析之后的单位数值对象 *

* Parsed Unit Value Object */ - public static BaseValue parse(double valueNumber) { - return parse(valueNumber, null); + public static BaseValue parse(double valueNumber, BaseValueFactory baseValueFactoryClass) { + return parse(valueNumber, null, baseValueFactoryClass); } /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed - * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. - *

- * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. + *

+ * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @param baseValueFactoryClass 单位数值构造工厂类 + *

+ * Unit Value Construction Factory Class * @return 解析之后的单位数值对象 *

* Parsed Unit Value Object */ - protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue) { - return new BaseValue(valueNumber, DataValue.class, baseNameKeyValue); + protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue, BaseValueFactory baseValueFactoryClass) { + return new BaseValue(valueNumber, DataValue.class, baseNameKeyValue, baseValueFactoryClass); } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/DateValue.java b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/DateValue.java index 8a7effd..bbefbf8 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/DateValue.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/DateValue.java @@ -1,45 +1,52 @@ package zhao.algorithmMagic.operands.unit; +import zhao.algorithmMagic.core.BaseValueFactory; import zhao.algorithmMagic.utils.dataContainer.KeyValue; /** * 时间单位数值类,支持时分秒的运算 - * + *

* Time unit numerical class, supporting operations of hours, minutes, and seconds */ @BaseUnit(value = { - "时", "分", "秒" -}, baseValue = 60) -public class DateValue extends BaseValue{ + "天", "时", "分", "秒", "毫秒" +}, baseValue = {24, 60, 60, 1000, 1}) +public class DateValue extends BaseValue { - public DateValue(double valueNumber, Class c, KeyValue baseNameKeyValue) { - super(valueNumber, c, baseNameKeyValue); + public DateValue(double valueNumber, Class c, KeyValue baseNameKeyValue, BaseValueFactory baseValueFactoryClass) { + super(valueNumber, c, baseNameKeyValue, baseValueFactoryClass); } /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseValueFactoryClass 单位数值构造工厂类 + *

+ * Unit Value Construction Factory Class * @return 解析之后的单位数值对象 *

* Parsed Unit Value Object */ - public static BaseValue parse(double valueNumber) { - return parse(valueNumber, null); + public static BaseValue parse(double valueNumber, BaseValueFactory baseValueFactoryClass) { + return parse(valueNumber, null, baseValueFactoryClass); } /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed - * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. - *

- * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. + *

+ * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @param baseValueFactoryClass 单位数值构造工厂类 + *

+ * Unit Value Construction Factory Class * @return 解析之后的单位数值对象 *

* Parsed Unit Value Object */ - protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue) { - return new BaseValue(valueNumber, DateValue.class, baseNameKeyValue); + protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue, BaseValueFactory baseValueFactoryClass) { + return new BaseValue(valueNumber, DateValue.class, baseNameKeyValue, baseValueFactoryClass); } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/MatchBaseGroupUnit.java b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/MatchBaseGroupUnit.java new file mode 100644 index 0000000..a5e476d --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/MatchBaseGroupUnit.java @@ -0,0 +1,122 @@ +package zhao.algorithmMagic.operands.unit; + +import zhao.algorithmMagic.utils.dataContainer.KeyValue; + +import java.util.HashMap; + +/** + * 匹配单位类,此类能够根据一个类中的注解自动生成合适的类对象。 + *

+ * Matching unit classes, which can automatically generate appropriate class objects based on annotations in a class. + * + * @author zhao + */ +public class MatchBaseGroupUnit extends MatchBaseUnit { + + protected final double[] baseDiff; + + + protected MatchBaseGroupUnit(TempBaseData tempBaseData, double[] baseDiff) { + super(tempBaseData); + final int length = super.tempBaseData.getBaseNames().length; + if (length != baseDiff.length) { + throw new UnsupportedOperationException("The length of the base difference array you provided is [" + baseDiff.length + "]. It is expected to be [" + length + "] because each element in the array corresponds to a base digit"); + } + this.baseDiff = baseDiff; + } + + + /** + * 根据单位注解创建出一个单位对象。 + *

+ * The decimal value between each adjacent subunit. Create a unit object based on the unit annotation. + * + * @param baseUnit 需要被解析的单位注解。 + *

+ * Unit annotations that need to be parsed. + * @return 创建出来的单位对象。 + *

+ * The unit object created. + */ + static TempBaseData parse(BaseUnit baseUnit) { + final String[] value = baseUnit.value(); + final TempBaseData tempBaseData = TEMP_BASE_DATA_HASH_MAP.get(baseUnit); + if (tempBaseData != null) { + return tempBaseData; + } + final double[] doubles = new double[value.length]; + double number = 1; + final double[] doubles1 = baseUnit.baseValue(); + HashMap hashMap = new HashMap<>(); + // 构建位权 + int length = value.length - 1; + hashMap.put(value[length], length); + doubles[length--] = 0; + + for (; length >= 0; length--) { + doubles[length] = number *= doubles1[length]; + hashMap.put(value[length], length); + } + + final TempBaseData tempBaseData1 = new TempBaseData(doubles[doubles.length - 2], doubles, value, hashMap, baseUnit.needUnifiedUnit()); + TEMP_BASE_DATA_HASH_MAP.put(baseUnit, tempBaseData1); + return tempBaseData1; + } + + /** + * @return 当前单位对象中的每个子单位的进制差,位权按照从大到小的顺序迭代。 + *

+ * The bit weight of each subunit in the current unit object. Bit weight is iterated in ascending order. + * 例如:["h","m","s","ms"] 中 进制差为 [60, 60, 1000, 0] 代表的就是下一个单位举例当前单位的倍数 + */ + @Override + public double[] getBaseDiff() { + return this.baseDiff.clone(); + } + + /** + * 将一个具有指定单位的数值转换为另一个具有指定单位的数值 + *

+ * Convert a numerical value with a specified unit to another numerical value with a specified unit + * + * @param baseValue 需要被转换的数值对象 + *

+ * Numerical objects that need to be converted + * @param targetUnit 期望切换到的目标单位 + * + *

+ * Expected target unit to switch to + * @return 计算出能够容纳此数值的单位名称 + *

+ * Calculate the unit name that can accommodate this value + */ + @Override + public BaseValue switchBaseName(BaseValue baseValue, KeyValue targetUnit) { + double value = baseValue.doubleValue(); + final KeyValue nowBase = baseValue.getNowBase(); + // 首先获取到当前的单位对应的索引名称 + final HashMap hashMap = super.tempBaseData.getHashMap(); + final String value1 = targetUnit.getValue(); + if (!hashMap.containsKey(value1)) { + throw new UnsupportedOperationException("The current unit object did not find any sub units [" + value1 + "]. The units you can use are:" + hashMap.keySet()); + } + final double[] baseDiff = this.baseDiff; + // 判断升降级位权 + final Integer i1 = nowBase.getKey(); + final Integer i2 = targetUnit.getKey(); + + if (i2 > i1) { + // 目标的索引大 目标的位权 低 开始降级位权 + for (int i = i1; i <= i2; i++) { + value *= baseDiff[i]; + } + } else { + // 开始升级位权 + for (int i = i1; i >= i2; i--) { + value /= baseDiff[i]; + } + } + + return new BaseValue(value, baseValue.getBaseValueFactoryClass(), baseValue.baseUnitObj, targetUnit); + } +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/MatchBaseUnit.java b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/MatchBaseUnit.java index 98d0f28..c0be7d7 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/MatchBaseUnit.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/MatchBaseUnit.java @@ -3,6 +3,7 @@ import zhao.algorithmMagic.utils.ASMath; import zhao.algorithmMagic.utils.dataContainer.KeyValue; +import java.util.Arrays; import java.util.HashMap; /** @@ -14,9 +15,9 @@ */ public class MatchBaseUnit implements BaseUnitObj { - private final static HashMap, BaseUnitObj> CLASS_BASE_UNIT_OBJ_HASH_MAP = new HashMap<>(); + protected final static HashMap, BaseUnitObj> CLASS_BASE_UNIT_OBJ_HASH_MAP = new HashMap<>(); - private final BaseUnitObj.TempBaseData tempBaseData; + protected final BaseUnitObj.TempBaseData tempBaseData; protected MatchBaseUnit(TempBaseData tempBaseData) { this.tempBaseData = tempBaseData; @@ -41,7 +42,15 @@ public static BaseUnitObj parse(Class c) { } final BaseUnit annotation = c.getAnnotation(BaseUnit.class); if (annotation != null) { - final MatchBaseUnit matchBaseUnit = new MatchBaseUnit(BaseUnitObj.parse(annotation)); + final double[] doubles = annotation.baseValue(); + if (doubles.length == 0) { + throw new UnsupportedOperationException("You must set the baseValue attribute in @BaseUnit, which can be 1 element or >=2 elements, but should not be 0 elements!!!"); + } + final boolean b = doubles.length != 1; + if (b) { + doubles[doubles.length - 1] = 1; + } + final MatchBaseUnit matchBaseUnit = b ? new MatchBaseGroupUnit(MatchBaseGroupUnit.parse(annotation), doubles) : new MatchBaseUnit(BaseUnitObj.parse(annotation)); CLASS_BASE_UNIT_OBJ_HASH_MAP.put(c, matchBaseUnit); return matchBaseUnit; } @@ -101,6 +110,44 @@ public double[] getBaseWeight() { return this.tempBaseData.getBaseWeights().clone(); } + /** + * @return 当前单位对象中的每个子单位的进制差,位权按照从大到小的顺序迭代。 + *

+ * The bit weight of each subunit in the current unit object. Bit weight is iterated in ascending order. + * 例如:["h","m","s","ms"] 中 进制差为 [60, 60, 1000, 0] 代表的就是下一个单位举例当前单位的倍数 + */ + @Override + public double[] getBaseDiff() { + final double[] doubles = new double[this.tempBaseData.getBaseNames().length]; + Arrays.fill(doubles, this.getBaseValue()); + doubles[doubles.length - 1] = 1; + return doubles; + } + + /** + * 如果需要设置为 true;Set to true if necessary + * + * @return 在数学中,乘法运算需要注意两个方面:数字和单位。 + *

+ * 当数字相乘时,可以直接进行乘法运算。例如,2升乘以2升,数字部分是2乘以2,结果是4。 + *

+ * 当单位相乘时,需要先统一单位,然后再进行乘法运算。例如,2米乘以2米,单位是米,可以直接相乘得到4平方米。但如果单位不同,比如2米乘以2厘米,需要先统一单位,把2厘米转换为米(即0.01米),然后再相乘得到0.04平方米。 + *

+ * 因此,在进行乘法运算时,需要先判断数字和单位是否一致,如果单位不一致需要先统一单位再进行计算。 + *

+ * In mathematics, multiplication operations require attention to two aspects: numbers and units. + *

+ * When multiplying numbers, multiplication can be performed directly. For example, multiplying 2 liters by 2 liters, the numerical part is 2 times 2, and the result is 4. + *

+ * When multiplying units, it is necessary to first unify the units before performing multiplication operations. For example, multiplying 2 meters by 2 meters in meters can directly yield 4 square meters. But if the units are different, such as multiplying 2 meters by 2 centimeters, it is necessary to first unify the units, convert 2 centimeters to meters (i.e. 0.01 meters), and then multiply them to obtain 0.04 square meters. + *

+ * Therefore, when performing multiplication operations, it is necessary to first check whether the numbers and units are consistent. If the units are not consistent, it is necessary to first unify the units before calculating. + */ + @Override + public boolean isNeedUnifiedUnit() { + return this.tempBaseData.isNeedUnifiedUnit(); + } + /** * 将一个具有指定单位的数值转换为另一个具有指定单位的数值 *

@@ -140,7 +187,7 @@ public BaseValue switchBaseName(BaseValue baseValue, KeyValue t value /= Math.pow(target, i2); } - return new BaseValue(value, baseValue.baseUnitObj, targetUnit); + return new BaseValue(value, baseValue.getBaseValueFactoryClass(), baseValue.baseUnitObj, targetUnit); } /** diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/VolumeValue.java b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/VolumeValue.java index ed222b8..3aadc66 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/VolumeValue.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/VolumeValue.java @@ -1,46 +1,55 @@ package zhao.algorithmMagic.operands.unit; +import zhao.algorithmMagic.core.BaseValueFactory; import zhao.algorithmMagic.utils.dataContainer.KeyValue; /** * 体积单位数值类,其中保存了体积相关的单位以及数值管理机制 - * + *

* Volume unit numerical class, which stores volume related units and numerical management mechanisms + * * @author zhao */ @BaseUnit(value = { "m³(立方米)", "dm³(立方分米 or 升)", "cm³(立方厘米 or 毫升)", "mm³(立方毫米)" }, baseValue = 1000) -public class VolumeValue extends BaseValue{ +public class VolumeValue extends BaseValue { + - public VolumeValue(double valueNumber, Class c, KeyValue baseNameKeyValue) { - super(valueNumber, c, baseNameKeyValue); + public VolumeValue(double valueNumber, Class c, KeyValue baseNameKeyValue, BaseValueFactory baseValueFactoryClass) { + super(valueNumber, c, baseNameKeyValue, baseValueFactoryClass); } /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseValueFactoryClass 单位数值构造工厂类 + *

+ * Unit Value Construction Factory Class * @return 解析之后的单位数值对象 *

* Parsed Unit Value Object */ - public static BaseValue parse(double valueNumber) { - return parse(valueNumber, null); + public static BaseValue parse(double valueNumber, BaseValueFactory baseValueFactoryClass) { + return parse(valueNumber, null, baseValueFactoryClass); } /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed - * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. - *

- * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. + *

+ * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @param baseValueFactoryClass 单位数值构造工厂类 + *

+ * Unit Value Construction Factory Class * @return 解析之后的单位数值对象 *

* Parsed Unit Value Object */ - protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue) { - return new BaseValue(valueNumber, VolumeValue.class, baseNameKeyValue); + protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue, BaseValueFactory baseValueFactoryClass) { + return new BaseValue(valueNumber, VolumeValue.class, baseNameKeyValue, baseValueFactoryClass); } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/WeightValue.java b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/WeightValue.java index 9b31188..4d4ac38 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/unit/WeightValue.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/unit/WeightValue.java @@ -1,5 +1,6 @@ package zhao.algorithmMagic.operands.unit; +import zhao.algorithmMagic.core.BaseValueFactory; import zhao.algorithmMagic.utils.dataContainer.KeyValue; /** @@ -14,34 +15,40 @@ }, baseValue = 1000) public class WeightValue extends BaseValue { - protected WeightValue(double valueNumber, Class c, KeyValue baseNameKeyValue) { - super(valueNumber, c, baseNameKeyValue); + public WeightValue(double valueNumber, Class c, KeyValue baseNameKeyValue, BaseValueFactory baseValueFactoryClass) { + super(valueNumber, c, baseNameKeyValue, baseValueFactoryClass); } /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseValueFactoryClass 单位数值构造工厂类 + *

+ * Unit Value Construction Factory Class * @return 解析之后的单位数值对象 *

* Parsed Unit Value Object */ - public static BaseValue parse(double valueNumber) { - return parse(valueNumber, null); + public static BaseValue parse(double valueNumber, BaseValueFactory baseValueFactoryClass) { + return parse(valueNumber, null, baseValueFactoryClass); } /** - * @param valueNumber 需要被解析的数值 - *

- * Value that needs to be parsed - * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. - *

- * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. + *

+ * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @param baseValueFactoryClass 单位数值构造工厂类 + *

+ * Unit Value Construction Factory Class * @return 解析之后的单位数值对象 *

* Parsed Unit Value Object */ - protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue) { - return new BaseValue(valueNumber, WeightValue.class, baseNameKeyValue); + protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue, BaseValueFactory baseValueFactoryClass) { + return new BaseValue(valueNumber, WeightValue.class, baseNameKeyValue, baseValueFactoryClass); } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 6f3a8ac..717eba9 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -103,21 +103,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 add DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 add DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray - - /** - * @return 向量中包含的维度数量 - *

- * the number of dimensions contained in the vector - */ - override def getNumberOfDimensions: Int = size - /** * 在两个操作数之间做差的方法,具体用法请参阅API说明。 *

@@ -142,21 +127,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 diff DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 diff DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } - /** - * @return 向量中包含的维度数量 - *

- * the number of dimensions contained in the vector - */ - override def getNumberOfDimensions: Int = size - - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray - /** * 将本对象中的所有数据进行洗牌打乱,随机分布数据行的排列。 *

@@ -191,6 +161,21 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll SparkVector.parse(sparkContext, doubles) } + /** + * + * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 + * + * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. + */ + override def copyToNewArray(): Array[Double] = vector.toArray + + /** + * @return 向量中包含的维度数量 + *

+ * the number of dimensions contained in the vector + */ + override def getNumberOfDimensions: Int = size + /** * 在两个操作数之间做差的方法,具体用法请参阅API说明。 *

diff --git a/src_code/update/1.29-1.30-Chinese.md b/src_code/update/1.29-1.30-Chinese.md new file mode 100644 index 0000000..a11d0f5 --- /dev/null +++ b/src_code/update/1.29-1.30-Chinese.md @@ -0,0 +1,158 @@ +# ![image](https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png) 算法之星-机器大脑 + +- Switch to [English Document](https://github.com/BeardedManZhao/algorithmStar/blob/Zhao-develop/src_code/README.md) +- knowledge base + + + + +### 更新日志 + +* 框架版本:1.29 - 1.30 +* 新增单位数值类操作数,此操作数可以实现基本计算,同时还可以实现带有单位的计算效果,其还具有单位转换操作,且允许自定义单位!!! + * 内置了 BaseValue 类,其可以实现加减乘除运算,同时还支持单位数值的转换,可以直接使用 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.core.BaseValueFactory; +import zhao.algorithmMagic.operands.unit.BaseValue; + +import java.net.MalformedURLException; + +public class MAIN1 { + public static void main(String[] args) { + // 构建一个用来创建 BaseValue.class 的工厂类 TODO 这里的类型可以是其它的 但是要确保是 BaseValue 的子类 + final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(BaseValue.class); + // 获取到数值 + final BaseValue number_1 = baseValueFactory.parse(200); + System.out.println("number_1 = " + number_1); + final BaseValue number_2 = baseValueFactory.parse(1024); + System.out.println("number_2 = " + number_2); + + // 基本的运算演示 + System.out.println("============"); + System.out.println(number_2 + " + " + number_1 + " = " + number_1.add(number_2)); + System.out.println(number_2 + " + " + 2 + number_2.getNowBase().getValue() + " = " + number_2.add(2)); + + System.out.println("============"); + System.out.println(number_2 + " - " + number_1 + " = " + number_1.diff(number_2)); + System.out.println(number_2 + " - " + 2 + number_2.getNowBase().getValue() + " = " + number_2.diff(2)); + + System.out.println("============"); + System.out.println(number_2 + " * " + number_1 + " = " + number_1.multiply(number_2)); + System.out.println(number_2 + " * " + 2 + number_2.getNowBase().getValue() + " = " + number_2.multiply(2)); + + System.out.println("============"); + System.out.println(number_2 + " / " + number_1 + " = " + number_1.divide(number_2)); + System.out.println(number_2 + " / " + 2 + number_2.getNowBase().getValue() + " = " + number_2.divide(2)); + + // 将 1.024千 转换为 10.24百 和 0.1024 万 + System.out.println("============"); + System.out.println(number_2.switchUnits("百")); + System.out.println(number_2.switchUnits("万")); + } +} + +``` + +* 如果需要自定义单位,可以直接继承 BaseValue 类,然后标注 BaseUnit 注解即可,下面是一个基本的实例(下面的类也已经加入到了AS库) + * 继承 BaseValue 并重写静态的 parse 函数 + * 标注 BaseUnit 注解 在其中设置单位 与 进制值 + +```java +package zhao.algorithmMagic.operands.unit; + +import zhao.algorithmMagic.utils.dataContainer.KeyValue; + +/** + * 重量单位数值,在这里可以直接使用重量相关的单位! + * + * Weight unit value, weight related units can be directly used here! + * @author zhao + */ +@BaseUnit(value = { + "t(吨)", "kg(千克)", "g(克)", "mg(毫克)", "ug(微克)", "ng(纳克)", "pg(皮克)", "fg(飞克)" +}, baseValue = 1000) +public class WeightValue extends BaseValue { + + protected WeightValue(double valueNumber, Class c, KeyValue baseNameKeyValue) { + super(valueNumber, c, baseNameKeyValue); + } + + /** + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @return 解析之后的单位数值对象 + *

+ * Parsed Unit Value Object + */ + public static BaseValue parse(double valueNumber) { + return parse(valueNumber, null); + } + + /** + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. + *

+ * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @return 解析之后的单位数值对象 + *

+ * Parsed Unit Value Object + */ + protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue) { + return new BaseValue(valueNumber, WeightValue.class, baseNameKeyValue); + } +} + +``` + +然后就可以进行使用啦 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.core.BaseValueFactory; +import zhao.algorithmMagic.operands.unit.BaseValue; +import zhao.algorithmMagic.operands.unit.WeightValue; + +import java.net.MalformedURLException; + +public class MAIN1 { + public static void main(String[] args) { + // 构建一个用来创建 WeightValue.class 的工厂类 TODO 这里的类型可以是其它的 但是要确保是 BaseValue 的子类 + final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(WeightValue.class); + // 获取到数值 + final BaseValue number_1 = baseValueFactory.parse(200); + System.out.println("number_1 = " + number_1); + // 将第一个的单位转换为 克 + number_1.switchUnitsNotChange("g(克)"); + System.out.println("number_1 = " + number_1); + // 获取到第二个数值 + final BaseValue number_2 = baseValueFactory.parse(102.4); + // 将第二个转换为 毫克 + number_2.switchUnitsNotChange("mg(毫克)"); + System.out.println("number_2 = " + number_2); + // 进行一个计算 + System.out.println("number_1 + number_2 = " + number_1.add(number_2)); + } +} + +``` + +* 内置的单位数值类 + +| 类路径 | 名称 | 加入版本 | 支持计算 | +|-----------------------------------------------|---------|-------|------| +| zhao.algorithmMagic.operands.unit.BaseValue | 基础单位数值类 | v1.30 | yes | +| zhao.algorithmMagic.operands.unit.DataValue | 数据单位数值类 | v1.30 | yes | +| zhao.algorithmMagic.operands.unit.VolumeValue | 容量单位数值类 | v1.30 | yes | +| zhao.algorithmMagic.operands.unit.WeightValue | 重量单位数值类 | v1.30 | yes | +| zhao.algorithmMagic.operands.unit.DateValue | 时间单位数值类 | v1.30 | yes | + +### Version update date : 2024-01-30 \ No newline at end of file diff --git a/src_code/update/1.29-1.30.md b/src_code/update/1.29-1.30.md new file mode 100644 index 0000000..5ab4198 --- /dev/null +++ b/src_code/update/1.29-1.30.md @@ -0,0 +1,161 @@ +# ![image](https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png) Algorithm Star-MachineBrain + +- 切换到 [中文文档](https://github.com/BeardedManZhao/algorithmStar/blob/Zhao-develop/src_code/README-Chinese.md) +- knowledge base + + + + +### Update log: + +* Framework version: 1.29 - 1.30 +* Add unit value operands, which can achieve basic calculations and also achieve calculation effects with units. They + also have unit conversion operations and allow custom units!!! + * Built in BaseValue class, it can implement addition, subtraction, multiplication, and division operations, and + also supports conversion of unit values, which can be directly used + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.core.BaseValueFactory; +import zhao.algorithmMagic.operands.unit.BaseValue; + +import java.net.MalformedURLException; + +public class MAIN1 { + public static void main(String[] args) { + // 构建一个用来创建 BaseValue.class 的工厂类 TODO 这里的类型可以是其它的 但是要确保是 BaseValue 的子类 + final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(BaseValue.class); + // 获取到数值 + final BaseValue number_1 = baseValueFactory.parse(200); + System.out.println("number_1 = " + number_1); + final BaseValue number_2 = baseValueFactory.parse(1024); + System.out.println("number_2 = " + number_2); + + // 基本的运算演示 + System.out.println("============"); + System.out.println(number_2 + " + " + number_1 + " = " + number_1.add(number_2)); + System.out.println(number_2 + " + " + 2 + number_2.getNowBase().getValue() + " = " + number_2.add(2)); + + System.out.println("============"); + System.out.println(number_2 + " - " + number_1 + " = " + number_1.diff(number_2)); + System.out.println(number_2 + " - " + 2 + number_2.getNowBase().getValue() + " = " + number_2.diff(2)); + + System.out.println("============"); + System.out.println(number_2 + " * " + number_1 + " = " + number_1.multiply(number_2)); + System.out.println(number_2 + " * " + 2 + number_2.getNowBase().getValue() + " = " + number_2.multiply(2)); + + System.out.println("============"); + System.out.println(number_2 + " / " + number_1 + " = " + number_1.divide(number_2)); + System.out.println(number_2 + " / " + 2 + number_2.getNowBase().getValue() + " = " + number_2.divide(2)); + + // 将 1.024千 转换为 10.24百 和 0.1024 万 + System.out.println("============"); + System.out.println(number_2.switchUnits("百")); + System.out.println(number_2.switchUnits("万")); + } +} + +``` + +* If you need to customize the unit, you can directly inherit the BaseValue class and annotate it with BaseUnit + annotation. Below is a basic instance (the following class has also been added to the AS library) + * Inherit BaseValue and rewrite the static parse function + * Annotate BaseUnit to set units and base values in it + +```java +package zhao.algorithmMagic.operands.unit; + +import zhao.algorithmMagic.utils.dataContainer.KeyValue; + +/** + * 重量单位数值,在这里可以直接使用重量相关的单位! + * + * Weight unit value, weight related units can be directly used here! + * @author zhao + */ +@BaseUnit(value = { + "t(吨)", "kg(千克)", "g(克)", "mg(毫克)", "ug(微克)", "ng(纳克)", "pg(皮克)", "fg(飞克)" +}, baseValue = 1000) +public class WeightValue extends BaseValue { + + protected WeightValue(double valueNumber, Class c, KeyValue baseNameKeyValue) { + super(valueNumber, c, baseNameKeyValue); + } + + /** + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @return 解析之后的单位数值对象 + *

+ * Parsed Unit Value Object + */ + public static BaseValue parse(double valueNumber) { + return parse(valueNumber, null); + } + + /** + * @param valueNumber 需要被解析的数值 + *

+ * Value that needs to be parsed + * @param baseNameKeyValue 单位的键值对,如果您需要指定数值的单位,您可以在这里进行指定,如果您不需要可以直接设置为 null,请注意 如果您不设置为null 此操作将不会对数值进行任何化简. + *

+ * The key value pairs of units. If you need to specify the unit of a numerical value, you can specify it here. If you don't need it, you can directly set it to null. Please note that if you don't set it to null, this operation will not simplify the numerical value in any way. + * @return 解析之后的单位数值对象 + *

+ * Parsed Unit Value Object + */ + protected static BaseValue parse(double valueNumber, KeyValue baseNameKeyValue) { + return new BaseValue(valueNumber, WeightValue.class, baseNameKeyValue); + } +} + +``` + +Then you can start using it + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.core.BaseValueFactory; +import zhao.algorithmMagic.operands.unit.BaseValue; +import zhao.algorithmMagic.operands.unit.WeightValue; + +import java.net.MalformedURLException; + +public class MAIN1 { + public static void main(String[] args) { + // 构建一个用来创建 WeightValue.class 的工厂类 TODO 这里的类型可以是其它的 但是要确保是 BaseValue 的子类 + final BaseValueFactory baseValueFactory = AlgorithmStar.baseValueFactory(WeightValue.class); + // 获取到数值 + final BaseValue number_1 = baseValueFactory.parse(200); + System.out.println("number_1 = " + number_1); + // 将第一个的单位转换为 克 + number_1.switchUnitsNotChange("g(克)"); + System.out.println("number_1 = " + number_1); + // 获取到第二个数值 + final BaseValue number_2 = baseValueFactory.parse(102.4); + // 将第二个转换为 毫克 + number_2.switchUnitsNotChange("mg(毫克)"); + System.out.println("number_2 = " + number_2); + // 进行一个计算 + System.out.println("number_1 + number_2 = " + number_1.add(number_2)); + } +} + +``` + +* Built in unit value class. + +| 类路径 | 名称 | 加入版本 | 支持计算 | +|-----------------------------------------------|---------|-------|------| +| zhao.algorithmMagic.operands.unit.BaseValue | 基础单位数值类 | v1.30 | yes | +| zhao.algorithmMagic.operands.unit.DataValue | 数据单位数值类 | v1.30 | yes | +| zhao.algorithmMagic.operands.unit.VolumeValue | 容量单位数值类 | v1.30 | yes | +| zhao.algorithmMagic.operands.unit.WeightValue | 重量单位数值类 | v1.30 | yes | +| zhao.algorithmMagic.operands.unit.DateValue | 时间单位数值类 | v1.30 | yes | + +### Version update date : 2024-01-30 \ No newline at end of file