Skip to content

Commit 090c186

Browse files
authored
feat(core): Support DatePrefix for auto config. (#583)
1 parent 063faf5 commit 090c186

File tree

7 files changed

+170
-78
lines changed

7 files changed

+170
-78
lines changed

cosid-core/src/main/java/me/ahoo/cosid/converter/DatePrefixIdConverter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323
import java.time.format.DateTimeFormatter;
2424

2525
public class DatePrefixIdConverter implements IdConverter, Decorator<IdConverter> {
26-
private final String prefix;
2726
private final String pattern;
2827
private final DateTimeFormatter formatter;
2928
private final String delimiter;
3029
private final IdConverter actual;
3130

32-
public DatePrefixIdConverter(String prefix, String pattern, String delimiter, IdConverter actual) {
33-
this.prefix = prefix;
31+
public DatePrefixIdConverter(String pattern, String delimiter, IdConverter actual) {
3432
this.pattern = pattern;
3533
this.formatter = DateTimeFormatter.ofPattern(pattern);
3634
this.delimiter = delimiter;
@@ -41,12 +39,12 @@ public DatePrefixIdConverter(String prefix, String pattern, String delimiter, Id
4139
@Nonnull
4240
@Override
4341
public String asString(long id) {
44-
return prefix + LocalDateTime.now().format(formatter) + delimiter + actual.asString(id);
42+
return LocalDateTime.now().format(formatter) + delimiter + actual.asString(id);
4543
}
4644

4745
@Override
4846
public long asLong(@Nonnull String idString) {
49-
int appendedLength = prefix.length() + pattern.length() + delimiter.length();
47+
int appendedLength = pattern.length() + delimiter.length();
5048
String idStr = idString.substring(appendedLength);
5149
return actual.asLong(idStr);
5250
}
@@ -59,6 +57,6 @@ public IdConverter getActual() {
5957

6058
@Override
6159
public Stat stat() {
62-
return new DatePrefixConverterStat(getClass().getSimpleName(), prefix, formatter.toString(), actual.stat());
60+
return new DatePrefixConverterStat(getClass().getSimpleName(), pattern, actual.stat());
6361
}
6462
}

cosid-core/src/main/java/me/ahoo/cosid/stat/converter/DatePrefixConverterStat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
import me.ahoo.cosid.stat.Stat;
1717

18-
public record DatePrefixConverterStat(String kind, String prefix, String formatter, Stat actual) implements Stat {
18+
public record DatePrefixConverterStat(String kind, String pattern, Stat actual) implements Stat {
1919
}

cosid-core/src/test/java/me/ahoo/cosid/converter/DatePrefixIdConverterTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@
2525
import static org.hamcrest.Matchers.instanceOf;
2626

2727
class DatePrefixIdConverterTest {
28-
private static final String PREFIX = "prefix_";
29-
private final DatePrefixIdConverter idConverter = new DatePrefixIdConverter(PREFIX, "yyMMdd", DEFAULT_DELIMITER, ToStringIdConverter.INSTANCE);
28+
private final DatePrefixIdConverter idConverter = new DatePrefixIdConverter("yyMMdd", DEFAULT_DELIMITER, ToStringIdConverter.INSTANCE);
3029

3130
@Test
3231
void asString() {
3332
String idString = idConverter.asString(1);
34-
String expected = PREFIX + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMdd")) + DEFAULT_DELIMITER + "1";
33+
String expected = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMdd")) + DEFAULT_DELIMITER + "1";
3534
assertThat(idString, equalTo(expected));
3635
}
3736

3837
@Test
3938
void asLong() {
40-
assertThat(idConverter.asLong("prefix_240618-1"), equalTo(1L));
39+
assertThat(idConverter.asLong("240618-1"), equalTo(1L));
4140
}
4241

4342
@Test

cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDecorator.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import me.ahoo.cosid.IdConverter;
1717
import me.ahoo.cosid.IdGenerator;
18+
import me.ahoo.cosid.converter.DatePrefixIdConverter;
1819
import me.ahoo.cosid.converter.PrefixIdConverter;
1920
import me.ahoo.cosid.converter.Radix36IdConverter;
2021
import me.ahoo.cosid.converter.Radix62IdConverter;
@@ -25,16 +26,17 @@
2526
import com.google.common.base.Strings;
2627

2728
import java.lang.reflect.InvocationTargetException;
29+
import java.util.Date;
2830

2931
public abstract class IdConverterDecorator<T extends IdGenerator> {
3032
protected final T idGenerator;
3133
protected final IdConverterDefinition converterDefinition;
32-
34+
3335
protected IdConverterDecorator(T idGenerator, IdConverterDefinition converterDefinition) {
3436
this.idGenerator = idGenerator;
3537
this.converterDefinition = converterDefinition;
3638
}
37-
39+
3840
public T decorate() {
3941
IdConverter idConverter = ToStringIdConverter.INSTANCE;
4042
switch (converterDefinition.getType()) {
@@ -45,47 +47,58 @@ public T decorate() {
4547
case CUSTOM -> idConverter = newCustom();
4648
default -> throw new IllegalStateException("Unexpected value: " + converterDefinition.getType());
4749
}
48-
50+
4951
IdConverterDefinition.GroupPrefix groupPrefix = converterDefinition.getGroupPrefix();
50-
52+
5153
if (groupPrefix.isEnabled() && groupPrefix.isBeforePrefix()) {
5254
idConverter = new GroupedPrefixIdConverter(groupPrefix.getDelimiter(), idConverter);
5355
}
56+
57+
IdConverterDefinition.DatePrefix datePrefix = converterDefinition.getDatePrefix();
58+
if (datePrefix.isEnabled() && datePrefix.isBeforePrefix()) {
59+
idConverter = new DatePrefixIdConverter(datePrefix.getPattern(), datePrefix.getDelimiter(), idConverter);
60+
}
61+
5462
if (!Strings.isNullOrEmpty(converterDefinition.getPrefix())) {
5563
idConverter = new PrefixIdConverter(converterDefinition.getPrefix(), idConverter);
5664
}
5765
if (groupPrefix.isEnabled() && !groupPrefix.isBeforePrefix()) {
5866
idConverter = new GroupedPrefixIdConverter(groupPrefix.getDelimiter(), idConverter);
5967
}
68+
69+
if (datePrefix.isEnabled() && !datePrefix.isBeforePrefix()) {
70+
idConverter = new DatePrefixIdConverter(datePrefix.getPattern(), datePrefix.getDelimiter(), idConverter);
71+
}
72+
6073
if (!Strings.isNullOrEmpty(converterDefinition.getSuffix())) {
6174
idConverter = new SuffixIdConverter(converterDefinition.getSuffix(), idConverter);
6275
}
63-
76+
6477
return newIdGenerator(idConverter);
6578
}
66-
79+
6780
protected IdConverter newRadix() {
6881
IdConverterDefinition.Radix radix = converterDefinition.getRadix();
6982
return Radix62IdConverter.of(radix.isPadStart(), radix.getCharSize());
7083
}
71-
84+
7285
protected IdConverter newRadix36() {
7386
IdConverterDefinition.Radix36 radix36 = converterDefinition.getRadix36();
7487
return Radix36IdConverter.of(radix36.isPadStart(), radix36.getCharSize());
7588
}
76-
89+
7790
protected IdConverter newToString(IdConverter defaultIdConverter) {
7891
IdConverterDefinition.ToString toString = converterDefinition.getToString();
7992
if (toString != null) {
8093
return new ToStringIdConverter(toString.isPadStart(), toString.getCharSize());
8194
}
8295
return defaultIdConverter;
8396
}
84-
97+
8598
protected IdConverter newSnowflakeFriendly() {
8699
throw new UnsupportedOperationException("newSnowflakeFriendly");
87100
}
88-
101+
89102
protected IdConverter newCustom() {
90103
IdConverterDefinition.Custom custom = converterDefinition.getCustom();
91104
try {
@@ -94,6 +107,6 @@ protected IdConverter newCustom() {
94107
throw new RuntimeException(e);
95108
}
96109
}
97-
110+
98111
protected abstract T newIdGenerator(IdConverter idConverter);
99112
}

0 commit comments

Comments
 (0)