ValidatableStringRecord 实现 Comparable 接口

feature/seq
ZhouXY108 2023-09-26 19:45:39 +08:00
parent 383478d2e3
commit eba31a93f3
3 changed files with 23 additions and 1 deletions

View File

@ -10,6 +10,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>8</java.version>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<jackson.version>2.13.5</jackson.version> <jackson.version>2.13.5</jackson.version>

View File

@ -31,7 +31,8 @@ import xyz.zhouxy.plusone.commons.util.RegexUtil;
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a> * @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @since 0.1.0 * @since 0.1.0
*/ */
public abstract class ValidatableStringRecord { public abstract class ValidatableStringRecord
implements Comparable<ValidatableStringRecord> {
private final String value; private final String value;
protected ValidatableStringRecord(String value, Pattern pattern) { protected ValidatableStringRecord(String value, Pattern pattern) {
@ -51,6 +52,11 @@ public abstract class ValidatableStringRecord {
return this.value; return this.value;
} }
@Override
public int compareTo(ValidatableStringRecord o) {
return this.value.compareTo(o.value);
}
@Override @Override
public String toString() { public String toString() {
return this.value(); return this.value();

View File

@ -10,6 +10,11 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.annotation.ValueObject; import xyz.zhouxy.plusone.commons.annotation.ValueObject;
import xyz.zhouxy.plusone.commons.constant.PatternConsts; 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 { class ValidatableStringRecordTests {
private static final Logger log = LoggerFactory.getLogger(ValidatableStringRecordTests.class); private static final Logger log = LoggerFactory.getLogger(ValidatableStringRecordTests.class);
@ -21,6 +26,16 @@ class ValidatableStringRecordTests {
String usernameStr = username.value(); String usernameStr = username.value();
assertNotNull(usernameStr); assertNotNull(usernameStr);
log.info("usernameStr: {}", usernameStr); log.info("usernameStr: {}", usernameStr);
List<Username> 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.<Username, String>comparing(o -> o.value().toLowerCase())));
} }
} }