Skip to content

Commit

Permalink
feat(core): Support DatePrefix for auto config. (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Jun 18, 2024
1 parent 063faf5 commit 090c186
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
import java.time.format.DateTimeFormatter;

public class DatePrefixIdConverter implements IdConverter, Decorator<IdConverter> {
private final String prefix;
private final String pattern;
private final DateTimeFormatter formatter;
private final String delimiter;
private final IdConverter actual;

public DatePrefixIdConverter(String prefix, String pattern, String delimiter, IdConverter actual) {
this.prefix = prefix;
public DatePrefixIdConverter(String pattern, String delimiter, IdConverter actual) {
this.pattern = pattern;
this.formatter = DateTimeFormatter.ofPattern(pattern);
this.delimiter = delimiter;
Expand All @@ -41,12 +39,12 @@ public DatePrefixIdConverter(String prefix, String pattern, String delimiter, Id
@Nonnull
@Override
public String asString(long id) {
return prefix + LocalDateTime.now().format(formatter) + delimiter + actual.asString(id);
return LocalDateTime.now().format(formatter) + delimiter + actual.asString(id);
}

@Override
public long asLong(@Nonnull String idString) {
int appendedLength = prefix.length() + pattern.length() + delimiter.length();
int appendedLength = pattern.length() + delimiter.length();
String idStr = idString.substring(appendedLength);
return actual.asLong(idStr);
}
Expand All @@ -59,6 +57,6 @@ public IdConverter getActual() {

@Override
public Stat stat() {
return new DatePrefixConverterStat(getClass().getSimpleName(), prefix, formatter.toString(), actual.stat());
return new DatePrefixConverterStat(getClass().getSimpleName(), pattern, actual.stat());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

import me.ahoo.cosid.stat.Stat;

public record DatePrefixConverterStat(String kind, String prefix, String formatter, Stat actual) implements Stat {
public record DatePrefixConverterStat(String kind, String pattern, Stat actual) implements Stat {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@
import static org.hamcrest.Matchers.instanceOf;

class DatePrefixIdConverterTest {
private static final String PREFIX = "prefix_";
private final DatePrefixIdConverter idConverter = new DatePrefixIdConverter(PREFIX, "yyMMdd", DEFAULT_DELIMITER, ToStringIdConverter.INSTANCE);
private final DatePrefixIdConverter idConverter = new DatePrefixIdConverter("yyMMdd", DEFAULT_DELIMITER, ToStringIdConverter.INSTANCE);

@Test
void asString() {
String idString = idConverter.asString(1);
String expected = PREFIX + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMdd")) + DEFAULT_DELIMITER + "1";
String expected = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMdd")) + DEFAULT_DELIMITER + "1";
assertThat(idString, equalTo(expected));
}

@Test
void asLong() {
assertThat(idConverter.asLong("prefix_240618-1"), equalTo(1L));
assertThat(idConverter.asLong("240618-1"), equalTo(1L));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import me.ahoo.cosid.IdConverter;
import me.ahoo.cosid.IdGenerator;
import me.ahoo.cosid.converter.DatePrefixIdConverter;
import me.ahoo.cosid.converter.PrefixIdConverter;
import me.ahoo.cosid.converter.Radix36IdConverter;
import me.ahoo.cosid.converter.Radix62IdConverter;
Expand All @@ -25,16 +26,17 @@
import com.google.common.base.Strings;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

public abstract class IdConverterDecorator<T extends IdGenerator> {
protected final T idGenerator;
protected final IdConverterDefinition converterDefinition;

protected IdConverterDecorator(T idGenerator, IdConverterDefinition converterDefinition) {
this.idGenerator = idGenerator;
this.converterDefinition = converterDefinition;
}

public T decorate() {
IdConverter idConverter = ToStringIdConverter.INSTANCE;
switch (converterDefinition.getType()) {
Expand All @@ -45,47 +47,58 @@ public T decorate() {
case CUSTOM -> idConverter = newCustom();
default -> throw new IllegalStateException("Unexpected value: " + converterDefinition.getType());
}

IdConverterDefinition.GroupPrefix groupPrefix = converterDefinition.getGroupPrefix();

if (groupPrefix.isEnabled() && groupPrefix.isBeforePrefix()) {
idConverter = new GroupedPrefixIdConverter(groupPrefix.getDelimiter(), idConverter);
}

IdConverterDefinition.DatePrefix datePrefix = converterDefinition.getDatePrefix();
if (datePrefix.isEnabled() && datePrefix.isBeforePrefix()) {
idConverter = new DatePrefixIdConverter(datePrefix.getPattern(), datePrefix.getDelimiter(), idConverter);
}

if (!Strings.isNullOrEmpty(converterDefinition.getPrefix())) {
idConverter = new PrefixIdConverter(converterDefinition.getPrefix(), idConverter);
}
if (groupPrefix.isEnabled() && !groupPrefix.isBeforePrefix()) {
idConverter = new GroupedPrefixIdConverter(groupPrefix.getDelimiter(), idConverter);
}

if (datePrefix.isEnabled() && !datePrefix.isBeforePrefix()) {
idConverter = new DatePrefixIdConverter(datePrefix.getPattern(), datePrefix.getDelimiter(), idConverter);
}

if (!Strings.isNullOrEmpty(converterDefinition.getSuffix())) {
idConverter = new SuffixIdConverter(converterDefinition.getSuffix(), idConverter);
}

return newIdGenerator(idConverter);
}

protected IdConverter newRadix() {
IdConverterDefinition.Radix radix = converterDefinition.getRadix();
return Radix62IdConverter.of(radix.isPadStart(), radix.getCharSize());
}

protected IdConverter newRadix36() {
IdConverterDefinition.Radix36 radix36 = converterDefinition.getRadix36();
return Radix36IdConverter.of(radix36.isPadStart(), radix36.getCharSize());
}

protected IdConverter newToString(IdConverter defaultIdConverter) {
IdConverterDefinition.ToString toString = converterDefinition.getToString();
if (toString != null) {
return new ToStringIdConverter(toString.isPadStart(), toString.getCharSize());
}
return defaultIdConverter;
}

protected IdConverter newSnowflakeFriendly() {
throw new UnsupportedOperationException("newSnowflakeFriendly");
}

protected IdConverter newCustom() {
IdConverterDefinition.Custom custom = converterDefinition.getCustom();
try {
Expand All @@ -94,6 +107,6 @@ protected IdConverter newCustom() {
throw new RuntimeException(e);
}
}

protected abstract T newIdGenerator(IdConverter idConverter);
}
Loading

0 comments on commit 090c186

Please sign in to comment.