From eba31a93f3ee79475840cbe3425efb0232824b78 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 26 Sep 2023 19:45:39 +0800 Subject: [PATCH] =?UTF-8?q?ValidatableStringRecord=20=E5=AE=9E=E7=8E=B0=20?= =?UTF-8?q?Comparable=20=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + .../commons/domain/ValidatableStringRecord.java | 8 +++++++- .../domain/ValidatableStringRecordTests.java | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 36417c9..3a58532 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,7 @@ UTF-8 + 8 1.8 1.8 2.13.5 diff --git a/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java b/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java index 0ff178b..5c3ac3c 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java @@ -31,7 +31,8 @@ import xyz.zhouxy.plusone.commons.util.RegexUtil; * @author ZhouXY * @since 0.1.0 */ -public abstract class ValidatableStringRecord { +public abstract class ValidatableStringRecord + implements Comparable { private final String value; protected ValidatableStringRecord(String value, Pattern pattern) { @@ -51,6 +52,11 @@ public abstract class ValidatableStringRecord { return this.value; } + @Override + public int compareTo(ValidatableStringRecord o) { + return this.value.compareTo(o.value); + } + @Override public String toString() { return this.value(); diff --git a/src/test/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecordTests.java b/src/test/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecordTests.java index a21170e..d2a6727 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecordTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecordTests.java @@ -10,6 +10,11 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.annotation.ValueObject; import xyz.zhouxy.plusone.commons.constant.PatternConsts; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + class ValidatableStringRecordTests { private static final Logger log = LoggerFactory.getLogger(ValidatableStringRecordTests.class); @@ -21,6 +26,16 @@ class ValidatableStringRecordTests { String usernameStr = username.value(); assertNotNull(usernameStr); log.info("usernameStr: {}", usernameStr); + + List usernames = Arrays.asList( + Username.of("ZhouXY108"), + Username.of("code_108"), + Username.of("Luquan"), + Username.of("Code108") + ); + log.info("{}", Collections.max(usernames)); + log.info("{}", Collections.max(usernames, + Comparator.comparing(o -> o.value().toLowerCase()))); } }