diff --git a/CHANGELOG.md b/CHANGELOG.md index fcb428c6a..68cdf872f 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,13 @@ ------------------------------------------------------------------------------------------------------------- -# 5.8.7.M1 (2022-09-14) +# 5.8.7.M1 (2022-09-15) ### 🐣新特性 * 【core 】 BooleanUtil的andOfWrap和orOfWrap()忽略null(issue#2599@Github) * 【jwt 】 优化JWT自动识别header中的算法,并可自定义header中key的顺序(issue#I5QRUO@Gitee) * 【core 】 IdcardUtil增加convert18To15方法(issue#I5QYCP@Gitee) +* 【core 】 新增AnsiColors(改自Spring Boot)、AnsiColorWrapper,优化QrCodeUtil(pr#778@Gitee) ### 🐞Bug修复 * 【core 】 修复ObjectUtil.defaultIfXXX中NPE问题(pr#2603@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/Ansi8BitColor.java b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/Ansi8BitColor.java index 1350f51b2..7de3a9225 100755 --- a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/Ansi8BitColor.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/Ansi8BitColor.java @@ -59,6 +59,16 @@ public final class Ansi8BitColor implements AnsiElement { this.code = code; } + /** + * 获取颜色代码(0-255) + * + * @return 颜色代码(0 - 255) + */ + @Override + public int getCode() { + return this.code; + } + @Override public boolean equals(Object obj) { if (this == obj) { diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiBackground.java b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiBackground.java index deb7a9acb..93550eb5f 100755 --- a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiBackground.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiBackground.java @@ -1,5 +1,7 @@ package cn.hutool.core.lang.ansi; +import cn.hutool.core.util.StrUtil; + /** * ANSI背景颜色枚举 * @@ -13,97 +15,107 @@ public enum AnsiBackground implements AnsiElement { /** * 默认背景色 */ - DEFAULT("49"), + DEFAULT(49), /** * 黑色 */ - BLACK("40"), + BLACK(40), /** * 红 */ - RED("41"), + RED(41), /** * 绿 */ - GREEN("42"), + GREEN(42), /** * 黄 */ - YELLOW("43"), + YELLOW(43), /** * 蓝 */ - BLUE("44"), + BLUE(44), /** * 品红 */ - MAGENTA("45"), + MAGENTA(45), /** * 青 */ - CYAN("46"), + CYAN(46), /** * 白 */ - WHITE("47"), + WHITE(47), /** * 亮黑 */ - BRIGHT_BLACK("100"), + BRIGHT_BLACK(100), /** * 亮红 */ - BRIGHT_RED("101"), + BRIGHT_RED(101), /** * 亮绿 */ - BRIGHT_GREEN("102"), + BRIGHT_GREEN(102), /** * 亮黄 */ - BRIGHT_YELLOW("103"), + BRIGHT_YELLOW(103), /** * 亮蓝 */ - BRIGHT_BLUE("104"), + BRIGHT_BLUE(104), /** * 亮品红 */ - BRIGHT_MAGENTA("105"), + BRIGHT_MAGENTA(105), /** * 亮青 */ - BRIGHT_CYAN("106"), + BRIGHT_CYAN(106), /** * 亮白 */ - BRIGHT_WHITE("107"); + BRIGHT_WHITE(107); - private final String code; + private final int code; - AnsiBackground(String code) { + AnsiBackground(int code) { this.code = code; } + /** + * 获取ANSI颜色代码 + * + * @return 颜色代码 + */ + @Override + public int getCode() { + return this.code; + } + @Override public String toString() { - return this.code; + return StrUtil.toString(this.code); } } diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColor.java b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColor.java index d478ffb39..0af354327 100755 --- a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColor.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColor.java @@ -1,5 +1,7 @@ package cn.hutool.core.lang.ansi; +import cn.hutool.core.util.StrUtil; + /** * ANSI标准颜色 * @@ -13,97 +15,107 @@ public enum AnsiColor implements AnsiElement { /** * 默认前景色 */ - DEFAULT("39"), + DEFAULT(39), /** * 黑 */ - BLACK("30"), + BLACK(30), /** * 红 */ - RED("31"), + RED(31), /** * 绿 */ - GREEN("32"), + GREEN(32), /** * 黄 */ - YELLOW("33"), + YELLOW(33), /** * 蓝 */ - BLUE("34"), + BLUE(34), /** * 品红 */ - MAGENTA("35"), + MAGENTA(35), /** * 青 */ - CYAN("36"), + CYAN(36), /** * 白 */ - WHITE("37"), + WHITE(37), /** * 亮黑 */ - BRIGHT_BLACK("90"), + BRIGHT_BLACK(90), /** * 亮红 */ - BRIGHT_RED("91"), + BRIGHT_RED(91), /** * 亮绿 */ - BRIGHT_GREEN("92"), + BRIGHT_GREEN(92), /** * 亮黄 */ - BRIGHT_YELLOW("93"), + BRIGHT_YELLOW(93), /** * 亮蓝 */ - BRIGHT_BLUE("94"), + BRIGHT_BLUE(94), /** * 亮品红 */ - BRIGHT_MAGENTA("95"), + BRIGHT_MAGENTA(95), /** * 亮青 */ - BRIGHT_CYAN("96"), + BRIGHT_CYAN(96), /** * 亮白 */ - BRIGHT_WHITE("97"); + BRIGHT_WHITE(97); - private final String code; + private final int code; - AnsiColor(String code) { + AnsiColor(int code) { this.code = code; } + /** + * 获取ANSI颜色代码 + * + * @return 颜色代码 + */ + @Override + public int getCode() { + return this.code; + } + @Override public String toString() { - return this.code; + return StrUtil.toString(this.code); } } diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColorWrapper.java b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColorWrapper.java index b78487865..1915ad1fa 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColorWrapper.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColorWrapper.java @@ -18,15 +18,16 @@ public class AnsiColorWrapper { private final AnsiColors.BitDepth bitDepth; /** - * 创建指定位深度的 {@link AnsiColorWrapper} 实例 - * @param code 颜色编码,位深度为4bit时,code取值范围[30~37],[90~97]。位深度为8bit时,code取值范围[0~255] + * 创建指定位深度的 {@code AnsiColorWrapper} 实例 + * + * @param code 颜色编码,位深度为4bit时,code取值范围[30~37],[90~97]。位深度为8bit时,code取值范围[0~255] * @param bitDepth 位深度 */ public AnsiColorWrapper(int code, AnsiColors.BitDepth bitDepth) { - if (bitDepth==AnsiColors.BitDepth.FOUR){ - Assert.isTrue((30<=code&&code<=37)||(90<=code&&code<=97),"The value of 4 bit color only supported [30~37],[90~97]."); + if (bitDepth == AnsiColors.BitDepth.FOUR) { + Assert.isTrue((30 <= code && code <= 37) || (90 <= code && code <= 97), "The value of 4 bit color only supported [30~37],[90~97]."); } - Assert.isTrue((0<=code&&code<=255),"The value of 8 bit color only supported [0~255]."); + Assert.isTrue((0 <= code && code <= 255), "The value of 8 bit color only supported [0~255]."); this.code = code; this.bitDepth = bitDepth; } @@ -37,24 +38,24 @@ public class AnsiColorWrapper { * @param foreOrBack 区分前景还是背景 * @return {@link AnsiElement} 实例 */ - public AnsiElement toAnsiElement(ForeOrBack foreOrBack){ - if (bitDepth== AnsiColors.BitDepth.FOUR){ - if (foreOrBack == ForeOrBack.FORE){ + public AnsiElement toAnsiElement(ForeOrBack foreOrBack) { + if (bitDepth == AnsiColors.BitDepth.FOUR) { + if (foreOrBack == ForeOrBack.FORE) { for (AnsiColor item : AnsiColor.values()) { - if (StrUtil.equals(item.toString(), StrUtil.toString(this.code))) { + if (item.getCode() == this.code) { return item; } } - throw new IllegalArgumentException(StrUtil.format("No matched AnsiColor instance,code={}",this.code)); + throw new IllegalArgumentException(StrUtil.format("No matched AnsiColor instance,code={}", this.code)); } for (AnsiBackground item : AnsiBackground.values()) { - if (StrUtil.equals(item.toString(), StrUtil.toString(this.code+10))) { + if (item.getCode() == this.code + 10) { return item; } } - throw new IllegalArgumentException(StrUtil.format("No matched AnsiBackground instance,code={}",this.code)); + throw new IllegalArgumentException(StrUtil.format("No matched AnsiBackground instance,code={}", this.code)); } - if (foreOrBack == ForeOrBack.FORE){ + if (foreOrBack == ForeOrBack.FORE) { return Ansi8BitColor.foreground(this.code); } return Ansi8BitColor.background(this.code); diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColors.java b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColors.java index f0aa749b5..a6ab61543 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColors.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColors.java @@ -20,103 +20,28 @@ public final class AnsiColors { private static final Map ANSI_COLOR_MAP; - /** - * @see AnsiColor#BLACK - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_BLACK = 30; - - /** - * @see AnsiColor#RED - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_RED = 31; - - /** - * @see AnsiColor#GREEN - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_GREEN = 32; - - /** - * @see AnsiColor#YELLOW - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_YELLOW = 33; - - /** - * @see AnsiColor#BLUE - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_BLUE = 34; - - /** - * @see AnsiColor#MAGENTA - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_MAGENTA = 35; - - /** - * @see AnsiColor#CYAN - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_CYAN = 36; - - /** - * @see AnsiColor#WHITE - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_WHITE = 37; - - /** - * @see AnsiColor#BRIGHT_BLACK - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_BLACK = 90; - - /** - * @see AnsiColor#BRIGHT_RED - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_RED = 91; - - /** - * @see AnsiColor#BRIGHT_GREEN - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_GREEN = 92; - - /** - * @see AnsiColor#BRIGHT_YELLOW - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_YELLOW = 93; - - /** - * @see AnsiColor#BRIGHT_BLUE - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_BLUE = 94; - - /** - * @see AnsiColor#BRIGHT_MAGENTA - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_MAGENTA = 95; - - /** - * @see AnsiColor#BRIGHT_CYAN - */ - private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_CYAN = 96; - /** * @see AnsiColor#BRIGHT_WHITE */ private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_WHITE = 97; static { - Map colorMap = new LinkedHashMap<>(16); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BLACK, BitDepth.FOUR), new LabColor(0x000000)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_RED, BitDepth.FOUR), new LabColor(0xAA0000)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_GREEN, BitDepth.FOUR), new LabColor(0x00AA00)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_YELLOW, BitDepth.FOUR), new LabColor(0xAA5500)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BLUE, BitDepth.FOUR), new LabColor(0x0000AA)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_MAGENTA, BitDepth.FOUR), new LabColor(0xAA00AA)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_CYAN, BitDepth.FOUR), new LabColor(0x00AAAA)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_WHITE, BitDepth.FOUR), new LabColor(0xAAAAAA)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_BLACK, BitDepth.FOUR), new LabColor(0x555555)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_RED, BitDepth.FOUR), new LabColor(0xFF5555)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_GREEN, BitDepth.FOUR), new LabColor(0x55FF00)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_YELLOW, BitDepth.FOUR), new LabColor(0xFFFF55)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_BLUE, BitDepth.FOUR), new LabColor(0x5555FF)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_MAGENTA, BitDepth.FOUR), new LabColor(0xFF55FF)); - colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_CYAN, BitDepth.FOUR), new LabColor(0x55FFFF)); + Map colorMap = new LinkedHashMap<>(16, 1); + colorMap.put(new AnsiColorWrapper(AnsiColor.BLACK.getCode(), BitDepth.FOUR), new LabColor(0x000000)); + colorMap.put(new AnsiColorWrapper(AnsiColor.RED.getCode(), BitDepth.FOUR), new LabColor(0xAA0000)); + colorMap.put(new AnsiColorWrapper(AnsiColor.GREEN.getCode(), BitDepth.FOUR), new LabColor(0x00AA00)); + colorMap.put(new AnsiColorWrapper(AnsiColor.YELLOW.getCode(), BitDepth.FOUR), new LabColor(0xAA5500)); + colorMap.put(new AnsiColorWrapper(AnsiColor.BLUE.getCode(), BitDepth.FOUR), new LabColor(0x0000AA)); + colorMap.put(new AnsiColorWrapper(AnsiColor.MAGENTA.getCode(), BitDepth.FOUR), new LabColor(0xAA00AA)); + colorMap.put(new AnsiColorWrapper(AnsiColor.CYAN.getCode(), BitDepth.FOUR), new LabColor(0x00AAAA)); + colorMap.put(new AnsiColorWrapper(AnsiColor.WHITE.getCode(), BitDepth.FOUR), new LabColor(0xAAAAAA)); + colorMap.put(new AnsiColorWrapper(AnsiColor.BRIGHT_BLACK.getCode(), BitDepth.FOUR), new LabColor(0x555555)); + colorMap.put(new AnsiColorWrapper(AnsiColor.BRIGHT_RED.getCode(), BitDepth.FOUR), new LabColor(0xFF5555)); + colorMap.put(new AnsiColorWrapper(AnsiColor.BRIGHT_GREEN.getCode(), BitDepth.FOUR), new LabColor(0x55FF00)); + colorMap.put(new AnsiColorWrapper(AnsiColor.BRIGHT_YELLOW.getCode(), BitDepth.FOUR), new LabColor(0xFFFF55)); + colorMap.put(new AnsiColorWrapper(AnsiColor.BRIGHT_BLUE.getCode(), BitDepth.FOUR), new LabColor(0x5555FF)); + colorMap.put(new AnsiColorWrapper(AnsiColor.BRIGHT_MAGENTA.getCode(), BitDepth.FOUR), new LabColor(0xFF55FF)); + colorMap.put(new AnsiColorWrapper(AnsiColor.BRIGHT_CYAN.getCode(), BitDepth.FOUR), new LabColor(0x55FFFF)); colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_WHITE, BitDepth.FOUR), new LabColor(0xFFFFFF)); ANSI_COLOR_MAP = Collections.unmodifiableMap(colorMap); } @@ -152,7 +77,7 @@ public final class AnsiColors { private final Map lookup; /** - * 创建具有指定位深度的新 {@link AnsiColors} 实例。 + * 创建具有指定位深度的新 {@code AnsiColors} 实例。 * @param bitDepth 所需的位深度 */ public AnsiColors(BitDepth bitDepth) { @@ -161,7 +86,7 @@ public final class AnsiColors { private Map getLookup(BitDepth bitDepth) { if (bitDepth == BitDepth.EIGHT) { - Map lookup = new LinkedHashMap<>(256); + final Map lookup = new LinkedHashMap<>(256, 1); for (int i = 0; i < ANSI_8BIT_COLOR_CODE_LOOKUP.length; i++) { lookup.put(new AnsiColorWrapper(i,BitDepth.EIGHT), new LabColor(ANSI_8BIT_COLOR_CODE_LOOKUP[i])); } @@ -239,7 +164,7 @@ public final class AnsiColors { double deltaA = this.a - other.a; double deltaB = this.b - other.b; double deltaH = Math.sqrt(Math.max(0.0, deltaA * deltaA + deltaB * deltaB - deltaC * deltaC)); - return Math.sqrt(Math.max(0.0, Math.pow((this.l - other.l) / (1.0), 2) + return Math.sqrt(Math.max(0.0, Math.pow((this.l - other.l), 2) + Math.pow(deltaC / (1 + 0.045 * c1), 2) + Math.pow(deltaH / (1 + 0.015 * c1), 2.0))); } diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiElement.java b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiElement.java index d09024efd..a2c616088 100755 --- a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiElement.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiElement.java @@ -15,4 +15,12 @@ public interface AnsiElement { @Override String toString(); + /** + * 获取ANSI代码,默认返回-1 + * @return ANSI代码 + * @since 5.8.7 + */ + default int getCode(){ + return -1; + } } diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiStyle.java b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiStyle.java index b95487d15..e92512bb9 100755 --- a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiStyle.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiStyle.java @@ -1,5 +1,7 @@ package cn.hutool.core.lang.ansi; +import cn.hutool.core.util.StrUtil; + /** * ANSI文本样式风格枚举 * @@ -13,37 +15,47 @@ public enum AnsiStyle implements AnsiElement { /** * 重置/正常 */ - NORMAL("0"), + NORMAL(0), /** * 粗体或增加强度 */ - BOLD("1"), + BOLD(1), /** * 弱化(降低强度) */ - FAINT("2"), + FAINT(2), /** * 斜体 */ - ITALIC("3"), + ITALIC(3), /** * 下划线 */ - UNDERLINE("4"); + UNDERLINE(4); - private final String code; + private final int code; - AnsiStyle(String code) { + AnsiStyle(int code) { this.code = code; } + /** + * 获取ANSI文本样式风格代码 + * + * @return 文本样式风格代码 + */ + @Override + public int getCode() { + return this.code; + } + @Override public String toString() { - return this.code; + return StrUtil.toString(this.code); } } diff --git a/hutool-core/src/test/java/cn/hutool/core/convert/Issue2611Test.java b/hutool-core/src/test/java/cn/hutool/core/convert/Issue2611Test.java new file mode 100644 index 000000000..c549d0b12 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/convert/Issue2611Test.java @@ -0,0 +1,17 @@ +package cn.hutool.core.convert; + +import cn.hutool.core.util.NumberUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.math.BigDecimal; + +public class Issue2611Test { + + @Test + public void chineseMoneyToNumberTest(){ + final BigDecimal value = Convert.chineseMoneyToNumber("陆万柒仟伍佰伍拾柒元"); + + Assert.assertEquals("67,557.00", NumberUtil.decimalFormatMoney(value.doubleValue())); + } +} diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/ansi/AnsiEncoderTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/ansi/AnsiEncoderTest.java index b2eaa50cf..883bc2407 100755 --- a/hutool-core/src/test/java/cn/hutool/core/lang/ansi/AnsiEncoderTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/ansi/AnsiEncoderTest.java @@ -1,12 +1,11 @@ package cn.hutool.core.lang.ansi; import cn.hutool.core.lang.Console; -import cn.hutool.core.util.StrUtil; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; -import java.awt.*; +import java.awt.Color; public class AnsiEncoderTest { @@ -36,7 +35,7 @@ public class AnsiEncoderTest { Color backColor = new Color(255 - foreColor.getRed(), 255 - foreColor.getGreen(), 255 - foreColor.getBlue()); AnsiElement backElement = ansiColors.findClosest(backColor).toAnsiElement(ForeOrBack.BACK); String encode = AnsiEncoder.encode(foreElement, backElement, text); - //Console.print( i%2==1?encode+"\n":encode); + Console.print( i%2==1?encode+"\n":encode); } }