diff --git a/build.gradle.kts b/build.gradle.kts index 621f911dcb..43aa7f67a1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -75,7 +75,7 @@ configure(libraryProjects) { apply() configure { toolchain { - languageVersion.set(JavaLanguageVersion.of(17)) + languageVersion.set(JavaLanguageVersion.of(8)) } withJavadocJar() withSourcesJar() diff --git a/cosid-core/src/main/java/me/ahoo/cosid/Decorator.java b/cosid-core/src/main/java/me/ahoo/cosid/Decorator.java index 045003d3c1..326b1daeee 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/Decorator.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/Decorator.java @@ -17,10 +17,11 @@ public interface Decorator { */ @Nonnull D getActual(); - + @SuppressWarnings({"unchecked", "rawtypes"}) static D getActual(D any) { - if (any instanceof Decorator decorator) { + if (any instanceof Decorator) { + Decorator decorator = (Decorator) any; return getActual((D) decorator.getActual()); } return any; diff --git a/cosid-core/src/test/java/me/ahoo/cosid/accessor/parser/CompositeFieldDefinitionParserTest.java b/cosid-core/src/test/java/me/ahoo/cosid/accessor/parser/CompositeFieldDefinitionParserTest.java index 9d4b4d5ec7..1ff3bfee10 100644 --- a/cosid-core/src/test/java/me/ahoo/cosid/accessor/parser/CompositeFieldDefinitionParserTest.java +++ b/cosid-core/src/test/java/me/ahoo/cosid/accessor/parser/CompositeFieldDefinitionParserTest.java @@ -13,6 +13,7 @@ package me.ahoo.cosid.accessor.parser; +import com.google.common.collect.Lists; import me.ahoo.cosid.accessor.IdDefinition; import me.ahoo.cosid.annotation.AnnotationDefinitionParser; import me.ahoo.cosid.annotation.entity.IdNotFoundEntity; @@ -31,9 +32,10 @@ class CompositeFieldDefinitionParserTest { @SneakyThrows @Test void parse() { - var compositeFieldDefinitionParser = new CompositeFieldDefinitionParser(List.of(AnnotationDefinitionParser.INSTANCE)); + + CompositeFieldDefinitionParser compositeFieldDefinitionParser = new CompositeFieldDefinitionParser(Lists.newArrayList(AnnotationDefinitionParser.INSTANCE)); Field idField = LongIdEntity.class.getDeclaredField("id"); - IdDefinition idDefinition = compositeFieldDefinitionParser.parse(List.of(LongIdEntity.class), idField); + IdDefinition idDefinition = compositeFieldDefinitionParser.parse(Lists.newArrayList(LongIdEntity.class), idField); Assertions.assertNotEquals(IdDefinition.NOT_FOUND, idDefinition); Assertions.assertEquals(IdGeneratorProvider.SHARE, idDefinition.getGeneratorName()); @@ -42,9 +44,9 @@ void parse() { @SneakyThrows @Test void parseIfNotFound() { - var compositeFieldDefinitionParser = new CompositeFieldDefinitionParser(List.of(AnnotationDefinitionParser.INSTANCE)); + CompositeFieldDefinitionParser compositeFieldDefinitionParser = new CompositeFieldDefinitionParser(Lists.newArrayList(AnnotationDefinitionParser.INSTANCE)); Field nameField = IdNotFoundEntity.class.getDeclaredField("name"); - IdDefinition idDefinition = compositeFieldDefinitionParser.parse(List.of(IdNotFoundEntity.class), nameField); + IdDefinition idDefinition = compositeFieldDefinitionParser.parse(Lists.newArrayList(IdNotFoundEntity.class), nameField); Assertions.assertEquals(IdDefinition.NOT_FOUND, idDefinition); } } \ No newline at end of file diff --git a/cosid-core/src/test/java/me/ahoo/cosid/stat/StatisticalTest.java b/cosid-core/src/test/java/me/ahoo/cosid/stat/StatisticalTest.java index 67b78559fc..3e09dfb2fa 100644 --- a/cosid-core/src/test/java/me/ahoo/cosid/stat/StatisticalTest.java +++ b/cosid-core/src/test/java/me/ahoo/cosid/stat/StatisticalTest.java @@ -16,6 +16,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; +import me.ahoo.cosid.IdGenerator; import me.ahoo.cosid.converter.Radix62IdConverter; import me.ahoo.cosid.cosid.Radix36CosIdGenerator; import me.ahoo.cosid.jvm.UuidGenerator; @@ -25,6 +26,7 @@ import me.ahoo.cosid.snowflake.MillisecondSnowflakeId; import me.ahoo.cosid.snowflake.StringSnowflakeId; import me.ahoo.cosid.stat.generator.CosIdGeneratorStat; +import me.ahoo.cosid.stat.generator.IdGeneratorStat; import me.ahoo.cosid.stat.generator.SegmentIdStat; import me.ahoo.cosid.stat.generator.SimpleIdGeneratorStat; @@ -37,51 +39,51 @@ class StatisticalTest { @Test void statUuidGenerator() { - var stat = UuidGenerator.INSTANCE.stat(); + IdGeneratorStat stat = UuidGenerator.INSTANCE.stat(); Assertions.assertNotNull(stat); } @Test void statSnowflakeId() { - var snowflakeId = new MillisecondSnowflakeId(0); - var stat = snowflakeId.stat(); + IdGenerator snowflakeId = new MillisecondSnowflakeId(0); + IdGeneratorStat stat = snowflakeId.stat(); Assertions.assertNotNull(stat); assertThat(stat, Matchers.instanceOf(SnowflakeIdStat.class)); - var snowflakeIdStat = (SnowflakeIdStat) stat; + SnowflakeIdStat snowflakeIdStat = (SnowflakeIdStat) stat; assertThat(snowflakeIdStat.getMachineId(), equalTo(0)); } @Test void statStringSnowflakeId() { - var snowflakeId = new StringSnowflakeId(new MillisecondSnowflakeId(0), Radix62IdConverter.INSTANCE); - var stat = snowflakeId.stat(); + IdGenerator snowflakeId = new StringSnowflakeId(new MillisecondSnowflakeId(0), Radix62IdConverter.INSTANCE); + IdGeneratorStat stat = snowflakeId.stat(); Assertions.assertNotNull(stat); assertThat(stat, Matchers.instanceOf(SimpleIdGeneratorStat.class)); } @Test void statSegmentId() { - var segmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock()); - var stat = segmentId.stat(); + IdGenerator segmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock()); + IdGeneratorStat stat = segmentId.stat(); Assertions.assertNotNull(stat); assertThat(stat, Matchers.instanceOf(SegmentIdStat.class)); } @Test void statStringSegmentId() { - var segmentId = new StringSegmentId(new DefaultSegmentId(new IdSegmentDistributor.Mock()), Radix62IdConverter.INSTANCE); - var stat = segmentId.stat(); + IdGenerator segmentId = new StringSegmentId(new DefaultSegmentId(new IdSegmentDistributor.Mock()), Radix62IdConverter.INSTANCE); + IdGeneratorStat stat = segmentId.stat(); Assertions.assertNotNull(stat); assertThat(stat, Matchers.instanceOf(SimpleIdGeneratorStat.class)); } @Test void statCosIdGenerator() { - var cosIdGenerator = new Radix36CosIdGenerator(0); - var stat = cosIdGenerator.stat(); + IdGenerator cosIdGenerator = new Radix36CosIdGenerator(0); + IdGeneratorStat stat = cosIdGenerator.stat(); Assertions.assertNotNull(stat); assertThat(stat, Matchers.instanceOf(CosIdGeneratorStat.class)); - var cosIdGeneratorStat = (CosIdGeneratorStat) stat; + CosIdGeneratorStat cosIdGeneratorStat = (CosIdGeneratorStat) stat; assertThat(cosIdGeneratorStat.getMachineId(), equalTo(0)); } } \ No newline at end of file diff --git a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java index 2f3a4d0db2..55275b0611 100644 --- a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java +++ b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java @@ -66,7 +66,7 @@ public void nextMaxIdWhenBack() { @Test public void nextMaxIdInParallel() { - var mono = Mono.fromRunnable(() -> { + Mono mono = Mono.fromRunnable(() -> { String namespace = MockIdGenerator.INSTANCE.generateAsString(); IdSegmentDistributorDefinition definition = new IdSegmentDistributorDefinition(namespace, "nextMaxIdIParallel", TEST_OFFSET, TEST_STEP); IdSegmentDistributor distributor = factory().create(definition); @@ -84,11 +84,11 @@ public void batchNextMaxId() { String namespace = MockIdGenerator.INSTANCE.generateAsString(); IdSegmentDistributorDefinition definition = new IdSegmentDistributorDefinition(namespace, "batchNextMaxId", 1, 1); IdSegmentDistributor distributor = factory().create(definition); - var segmentChainId = new SegmentChainId(distributor); + SegmentChainId segmentChainId = new SegmentChainId(distributor); for (int i = 0; i < 1000; i++) { segmentChainId.generateAsString(); } - var mono = Mono.fromRunnable(() -> { + Mono mono = Mono.fromRunnable(() -> { for (int i = 0; i < 1000; i++) { segmentChainId.generateAsString(); } diff --git a/cosid-mybatis/src/test/java/me/ahoo/cosid/mybatis/CosIdPluginTest.java b/cosid-mybatis/src/test/java/me/ahoo/cosid/mybatis/CosIdPluginTest.java index 3739d57d61..9f5bdd5d6f 100644 --- a/cosid-mybatis/src/test/java/me/ahoo/cosid/mybatis/CosIdPluginTest.java +++ b/cosid-mybatis/src/test/java/me/ahoo/cosid/mybatis/CosIdPluginTest.java @@ -1,5 +1,6 @@ package me.ahoo.cosid.mybatis; +import com.google.common.collect.Lists; import me.ahoo.cosid.accessor.parser.DefaultAccessorParser; import me.ahoo.cosid.accessor.registry.DefaultAccessorRegistry; import me.ahoo.cosid.annotation.AnnotationDefinitionParser; @@ -181,12 +182,12 @@ public int update(MappedStatement ms, Object parameter) throws SQLException { @Override public List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException { - return List.of(); + return Lists.newArrayList(); } @Override public List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException { - return List.of(); + return Lists.newArrayList(); } @Override @@ -196,7 +197,7 @@ public Cursor queryCursor(MappedStatement ms, Object parameter, RowBounds @Override public List flushStatements() throws SQLException { - return List.of(); + return Lists.newArrayList(); } @Override diff --git a/cosid-proxy/src/main/java/me/ahoo/cosid/proxy/ProxyMachineIdDistributor.java b/cosid-proxy/src/main/java/me/ahoo/cosid/proxy/ProxyMachineIdDistributor.java index c945dafcbe..9cf5630e7a 100644 --- a/cosid-proxy/src/main/java/me/ahoo/cosid/proxy/ProxyMachineIdDistributor.java +++ b/cosid-proxy/src/main/java/me/ahoo/cosid/proxy/ProxyMachineIdDistributor.java @@ -45,31 +45,31 @@ */ @Slf4j public class ProxyMachineIdDistributor extends AbstractMachineIdDistributor { - + private final OkHttpClient client; - + private final String proxyHost; - + public ProxyMachineIdDistributor(OkHttpClient client, String proxyHost, MachineStateStorage machineStateStorage, ClockBackwardsSynchronizer clockBackwardsSynchronizer) { super(machineStateStorage, clockBackwardsSynchronizer); this.client = client; this.proxyHost = proxyHost; } - + @SneakyThrows @Override protected MachineState distributeRemote(String namespace, int machineBit, InstanceId instanceId, Duration safeGuardDuration) { String apiUrl = - Strings.lenientFormat("%s/machines/%s?instanceId=%s&stable=%s&machineBit=%s&safeGuardDuration=%s", proxyHost, namespace, instanceId.getInstanceId(), instanceId.isStable(), machineBit, - safeGuardDuration); + Strings.lenientFormat("%s/machines/%s?instanceId=%s&stable=%s&machineBit=%s&safeGuardDuration=%s", proxyHost, namespace, instanceId.getInstanceId(), instanceId.isStable(), machineBit, + safeGuardDuration); if (log.isInfoEnabled()) { log.info("Distribute Remote instanceId:[{}] - machineBit:[{}] @ namespace:[{}] - apiUrl:[{}].", instanceId, machineBit, namespace, apiUrl); } - + Request request = new Request.Builder() - .url(apiUrl) - .post(Util.EMPTY_REQUEST) - .build(); + .url(apiUrl) + .post(Util.EMPTY_REQUEST) + .build(); try (Response response = client.newCall(request).execute()) { ResponseBody responseBody = response.body(); assert responseBody != null; @@ -77,7 +77,7 @@ protected MachineState distributeRemote(String namespace, int machineBit, Instan if (log.isInfoEnabled()) { log.info("Distribute Remote instanceId:[{}] - machineBit:[{}] @ namespace:[{}] - response:[{}].", instanceId, machineBit, namespace, bodyStr); } - + if (response.isSuccessful()) { return Jsons.OBJECT_MAPPER.readValue(bodyStr, MachineStateDto.class); } @@ -88,7 +88,7 @@ protected MachineState distributeRemote(String namespace, int machineBit, Instan throw new IllegalStateException(Strings.lenientFormat("Unexpected code:[%s] - message:[%s].", errorResponse.getCode(), errorResponse.getMsg())); } } - + @SneakyThrows @Override protected void revertRemote(String namespace, InstanceId instanceId, MachineState machineState) { @@ -96,11 +96,11 @@ protected void revertRemote(String namespace, InstanceId instanceId, MachineStat if (log.isInfoEnabled()) { log.info("Revert Remote [{}] instanceId:[{}] @ namespace:[{}] - apiUrl:[{}].", machineState, instanceId, namespace, apiUrl); } - + Request request = new Request.Builder() - .url(apiUrl) - .delete() - .build(); + .url(apiUrl) + .delete() + .build(); try (Response response = client.newCall(request).execute()) { if (log.isInfoEnabled()) { ResponseBody responseBody = response.body(); @@ -110,21 +110,21 @@ protected void revertRemote(String namespace, InstanceId instanceId, MachineStat } } } - + @SneakyThrows @Override protected void guardRemote(String namespace, InstanceId instanceId, MachineState machineState, Duration safeGuardDuration) { String apiUrl = - Strings.lenientFormat("%s/machines/%s?instanceId=%s&stable=%s&safeGuardDuration=%s", proxyHost, namespace, instanceId.getInstanceId(), instanceId.isStable(), safeGuardDuration); - + Strings.lenientFormat("%s/machines/%s?instanceId=%s&stable=%s&safeGuardDuration=%s", proxyHost, namespace, instanceId.getInstanceId(), instanceId.isStable(), safeGuardDuration); + if (log.isInfoEnabled()) { log.info("Guard Remote [{}] instanceId:[{}] @ namespace:[{}] - apiUrl:[{}].", machineState, instanceId, namespace, apiUrl); } - + Request request = new Request.Builder() - .url(apiUrl) - .patch(Util.EMPTY_REQUEST) - .build(); + .url(apiUrl) + .patch(Util.EMPTY_REQUEST) + .build(); try (Response response = client.newCall(request).execute()) { ResponseBody responseBody = response.body(); assert responseBody != null; @@ -135,14 +135,17 @@ protected void guardRemote(String namespace, InstanceId instanceId, MachineState if (response.isSuccessful()) { return; } - + ErrorResponse errorResponse = Jsons.OBJECT_MAPPER.readValue(bodyStr, ErrorResponse.class); switch (errorResponse.getCode()) { - case NOT_FOUND_MACHINE_STATE -> throw new NotFoundMachineStateException(namespace, instanceId); - case MACHINE_ID_LOST -> throw new MachineIdLostException(namespace, instanceId, machineState); - default -> throw new IllegalStateException("Unexpected value: " + errorResponse.getCode()); + case NOT_FOUND_MACHINE_STATE: + throw new NotFoundMachineStateException(namespace, instanceId); + case MACHINE_ID_LOST: + throw new MachineIdLostException(namespace, instanceId, machineState); + default: + throw new IllegalStateException("Unexpected value: " + errorResponse.getCode()); } } } - + } diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDecorator.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDecorator.java index 6eb22f7360..d2ee958a60 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDecorator.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDecorator.java @@ -40,12 +40,23 @@ protected IdConverterDecorator(T idGenerator, IdConverterDefinition converterDef public T decorate() { IdConverter idConverter = ToStringIdConverter.INSTANCE; switch (converterDefinition.getType()) { - case TO_STRING -> idConverter = newToString(idConverter); - case RADIX -> idConverter = newRadix(); - case RADIX36 -> idConverter = newRadix36(); - case SNOWFLAKE_FRIENDLY -> idConverter = newSnowflakeFriendly(); - case CUSTOM -> idConverter = newCustom(); - default -> throw new IllegalStateException("Unexpected value: " + converterDefinition.getType()); + case TO_STRING: + idConverter = newToString(idConverter); + break; + case RADIX: + idConverter = newRadix(); + break; + case RADIX36: + idConverter = newRadix36(); + break; + case SNOWFLAKE_FRIENDLY: + idConverter = newSnowflakeFriendly(); + break; + case CUSTOM: + idConverter = newCustom(); + break; + default: + throw new IllegalStateException("Unexpected value: " + converterDefinition.getType()); } IdConverterDefinition.GroupPrefix groupPrefix = converterDefinition.getGroupPrefix(); @@ -103,7 +114,8 @@ protected IdConverter newCustom() { IdConverterDefinition.Custom custom = converterDefinition.getCustom(); try { return custom.getType().getConstructor().newInstance(); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + } catch (InstantiationException | IllegalAccessException | InvocationTargetException + | NoSuchMethodException e) { throw new RuntimeException(e); } } diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdEndpoint.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdEndpoint.java index d70e03211f..8af59eb720 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdEndpoint.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdEndpoint.java @@ -38,7 +38,7 @@ public CosIdEndpoint(IdGeneratorProvider idGeneratorProvider) { public Map stat() { Map statMap = new HashMap<>(); for (Map.Entry entry : idGeneratorProvider.entries()) { - var stat = entry.getValue().stat(); + IdGeneratorStat stat = entry.getValue().stat(); statMap.put(entry.getKey(), stat); } return statMap; @@ -46,13 +46,13 @@ public Map stat() { @ReadOperation public IdGeneratorStat getStat(@Selector String name) { - var idGenerator = idGeneratorProvider.getRequired(name); + IdGenerator idGenerator = idGeneratorProvider.getRequired(name); return idGenerator.stat(); } @DeleteOperation public IdGeneratorStat remove(@Selector String name) { - var idGenerator = idGeneratorProvider.remove(name); + IdGenerator idGenerator = idGeneratorProvider.remove(name); return idGenerator.stat(); } diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/cosid/CosIdGeneratorAutoConfiguration.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/cosid/CosIdGeneratorAutoConfiguration.java index 7cceb0b721..358c3cb401 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/cosid/CosIdGeneratorAutoConfiguration.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/cosid/CosIdGeneratorAutoConfiguration.java @@ -49,13 +49,13 @@ public class CosIdGeneratorAutoConfiguration { private final CosIdProperties cosIdProperties; private final MachineProperties machineProperties; private final CosIdGeneratorProperties cosIdGeneratorProperties; - + public CosIdGeneratorAutoConfiguration(CosIdProperties cosIdProperties, MachineProperties machineProperties, CosIdGeneratorProperties cosIdGeneratorProperties) { this.cosIdProperties = cosIdProperties; this.machineProperties = machineProperties; this.cosIdGeneratorProperties = cosIdGeneratorProperties; } - + @Bean @Primary @ConditionalOnMissingBean @@ -63,29 +63,28 @@ public CosIdGenerator cosIdGenerator(MachineIdDistributor machineIdDistributor, ClockBackwardsSynchronizer clockBackwardsSynchronizer) { String namespace = Namespaces.firstNotBlank(cosIdGeneratorProperties.getNamespace(), cosIdProperties.getNamespace()); int machineId = - machineIdDistributor.distribute(namespace, cosIdGeneratorProperties.getMachineBit(), instanceId, machineProperties.getSafeGuardDuration()).getMachineId(); + machineIdDistributor.distribute(namespace, cosIdGeneratorProperties.getMachineBit(), instanceId, machineProperties.getSafeGuardDuration()).getMachineId(); machineIdGuarder.register(namespace, instanceId); CosIdGenerator cosIdGenerator = createCosIdGenerator(machineId); - + CosIdGenerator clockSyncCosIdGenerator = new ClockSyncCosIdGenerator(cosIdGenerator, clockBackwardsSynchronizer); idGeneratorProvider.set(CosId.COSID, clockSyncCosIdGenerator); return clockSyncCosIdGenerator; } - + @Nonnull private CosIdGenerator createCosIdGenerator(int machineId) { switch (cosIdGeneratorProperties.getType()) { - case RADIX62 -> { + case RADIX62: return new Radix62CosIdGenerator(cosIdGeneratorProperties.getTimestampBit(), - cosIdGeneratorProperties.getMachineBit(), cosIdGeneratorProperties.getSequenceBit(), machineId, - cosIdGeneratorProperties.getSequenceResetThreshold()); - } - case RADIX36 -> { + cosIdGeneratorProperties.getMachineBit(), cosIdGeneratorProperties.getSequenceBit(), machineId, + cosIdGeneratorProperties.getSequenceResetThreshold()); + case RADIX36: return new Radix36CosIdGenerator(cosIdGeneratorProperties.getTimestampBit(), - cosIdGeneratorProperties.getMachineBit(), cosIdGeneratorProperties.getSequenceBit(), machineId, - cosIdGeneratorProperties.getSequenceResetThreshold()); - } - default -> throw new IllegalStateException("Unexpected value: " + cosIdGeneratorProperties.getType()); + cosIdGeneratorProperties.getMachineBit(), cosIdGeneratorProperties.getSequenceBit(), machineId, + cosIdGeneratorProperties.getSequenceResetThreshold()); + default: + throw new IllegalStateException("Unexpected value: " + cosIdGeneratorProperties.getType()); } } } diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java index fa5c1ae01f..123b6c8286 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java @@ -85,10 +85,18 @@ private void registerIdDefinition(String name, SegmentIdProperties.IdDefinition SegmentIdProperties.IdDefinition.Group group = idDefinition.getGroup(); GroupBySupplier groupBySupplier; switch (group.getBy()) { - case YEAR -> groupBySupplier = new YearGroupBySupplier(group.getPattern()); - case YEAR_MONTH -> groupBySupplier = new YearMonthGroupBySupplier(group.getPattern()); - case YEAR_MONTH_DAY -> groupBySupplier = new YearMonthDayGroupBySupplier(group.getPattern()); - default -> groupBySupplier = null; + case YEAR: + groupBySupplier = new YearGroupBySupplier(group.getPattern()); + break; + case YEAR_MONTH: + groupBySupplier = new YearMonthGroupBySupplier(group.getPattern()); + break; + case YEAR_MONTH_DAY: + groupBySupplier = new YearMonthDayGroupBySupplier(group.getPattern()); + break; + default: + groupBySupplier = null; + break; } if (groupBySupplier != null) { diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/SnowflakeIdBeanRegistrar.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/SnowflakeIdBeanRegistrar.java index 07767aeea3..0e5baa9a0d 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/SnowflakeIdBeanRegistrar.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/SnowflakeIdBeanRegistrar.java @@ -44,7 +44,7 @@ public class SnowflakeIdBeanRegistrar implements InitializingBean { private final ConfigurableApplicationContext applicationContext; @Nullable private final CustomizeSnowflakeIdProperties customizeSnowflakeIdProperties; - + public SnowflakeIdBeanRegistrar(CosIdProperties cosIdProperties, MachineProperties machineProperties, SnowflakeIdProperties snowflakeIdProperties, @@ -64,12 +64,12 @@ public SnowflakeIdBeanRegistrar(CosIdProperties cosIdProperties, this.applicationContext = applicationContext; this.customizeSnowflakeIdProperties = customizeSnowflakeIdProperties; } - + @Override public void afterPropertiesSet() { register(); } - + public void register() { if (customizeSnowflakeIdProperties != null) { customizeSnowflakeIdProperties.customize(snowflakeIdProperties); @@ -80,34 +80,34 @@ public void register() { } snowflakeIdProperties.getProvider().forEach(this::registerIdDefinition); } - + private void registerIdDefinition(String name, SnowflakeIdProperties.IdDefinition idDefinition) { SnowflakeId idGenerator = createIdGen(idDefinition, clockBackwardsSynchronizer); registerSnowflakeId(name, idGenerator); } - + private void registerSnowflakeId(String name, SnowflakeId snowflakeId) { - if (idGeneratorProvider.get(name).isEmpty()) { + if (!idGeneratorProvider.get(name).isPresent()) { idGeneratorProvider.set(name, snowflakeId); } - + String beanName = name + "SnowflakeId"; applicationContext.getBeanFactory().registerSingleton(beanName, snowflakeId); } - + private SnowflakeId createIdGen(SnowflakeIdProperties.IdDefinition idDefinition, ClockBackwardsSynchronizer clockBackwardsSynchronizer) { long epoch = getEpoch(idDefinition); int machineBit = MoreObjects.firstNonNull(idDefinition.getMachineBit(), machineProperties.getMachineBit()); String namespace = Namespaces.firstNotBlank(idDefinition.getNamespace(), cosIdProperties.getNamespace()); int machineId = machineIdDistributor.distribute(namespace, machineBit, instanceId, machineProperties.getSafeGuardDuration()).getMachineId(); - + SnowflakeId snowflakeId; if (SnowflakeIdProperties.IdDefinition.TimestampUnit.SECOND.equals(idDefinition.getTimestampUnit())) { snowflakeId = new SecondSnowflakeId(epoch, idDefinition.getTimestampBit(), machineBit, idDefinition.getSequenceBit(), machineId, idDefinition.getSequenceResetThreshold()); } else { snowflakeId = - new MillisecondSnowflakeId(epoch, idDefinition.getTimestampBit(), machineBit, idDefinition.getSequenceBit(), machineId, idDefinition.getSequenceResetThreshold()); + new MillisecondSnowflakeId(epoch, idDefinition.getTimestampBit(), machineBit, idDefinition.getSequenceBit(), machineId, idDefinition.getSequenceResetThreshold()); } if (idDefinition.isClockSync()) { snowflakeId = new ClockSyncSnowflakeId(snowflakeId, clockBackwardsSynchronizer); @@ -116,12 +116,12 @@ private SnowflakeId createIdGen(SnowflakeIdProperties.IdDefinition idDefinition, final ZoneId zoneId = ZoneId.of(snowflakeIdProperties.getZoneId()); return new SnowflakeIdConverterDecorator(snowflakeId, converterDefinition, zoneId, idDefinition.isFriendly()).decorate(); } - + private long getEpoch(SnowflakeIdProperties.IdDefinition idDefinition) { if (idDefinition.getEpoch() > 0) { return idDefinition.getEpoch(); } return snowflakeIdProperties.getEpoch(); } - + } diff --git a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdEndpointTest.java b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdEndpointTest.java index 3c77b81e0f..8f930af94e 100644 --- a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdEndpointTest.java +++ b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdEndpointTest.java @@ -18,10 +18,13 @@ import me.ahoo.cosid.provider.DefaultIdGeneratorProvider; import me.ahoo.cosid.provider.IdGeneratorProvider; +import me.ahoo.cosid.stat.generator.IdGeneratorStat; import me.ahoo.cosid.test.MockIdGenerator; import org.junit.jupiter.api.Test; +import java.util.Map; + class CosIdEndpointTest { private final IdGeneratorProvider idGeneratorProvider = new DefaultIdGeneratorProvider(); private final CosIdEndpoint cosIdEndpoint = new CosIdEndpoint(idGeneratorProvider); @@ -32,21 +35,21 @@ public CosIdEndpointTest() { @Test void stat() { - var stat = cosIdEndpoint.stat(); + Map stat = cosIdEndpoint.stat(); assertThat(stat, notNullValue()); assertThat(stat.size(), equalTo(1)); } @Test void getStat() { - var stat = cosIdEndpoint.getStat(IdGeneratorProvider.SHARE); + IdGeneratorStat stat = cosIdEndpoint.getStat(IdGeneratorProvider.SHARE); assertThat(stat, notNullValue()); } @Test void remove() { - var idGeneratorProvider = new DefaultIdGeneratorProvider(); - var cosIdEndpoint = new CosIdEndpoint(idGeneratorProvider); + DefaultIdGeneratorProvider idGeneratorProvider = new DefaultIdGeneratorProvider(); + CosIdEndpoint cosIdEndpoint = new CosIdEndpoint(idGeneratorProvider); idGeneratorProvider.setShare(MockIdGenerator.INSTANCE); cosIdEndpoint.remove(IdGeneratorProvider.SHARE); assertThat(idGeneratorProvider.getShare(), nullValue()); diff --git a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdGeneratorEndpointTest.java b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdGeneratorEndpointTest.java index 32f40d11db..c7da669297 100644 --- a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdGeneratorEndpointTest.java +++ b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdGeneratorEndpointTest.java @@ -32,13 +32,13 @@ public CosIdGeneratorEndpointTest() { @Test void shareGenerate() { - var id = cosIdGeneratorEndpoint.shareGenerate(); + long id = cosIdGeneratorEndpoint.shareGenerate(); assertThat(id, greaterThan(0L)); } @Test void generate() { - var id = cosIdGeneratorEndpoint.generate(IdGeneratorProvider.SHARE); + long id = cosIdGeneratorEndpoint.generate(IdGeneratorProvider.SHARE); assertThat(id, greaterThan(0L)); } } \ No newline at end of file diff --git a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdStringGeneratorEndpointTest.java b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdStringGeneratorEndpointTest.java index 404743ef5b..06e80d8984 100644 --- a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdStringGeneratorEndpointTest.java +++ b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/actuate/CosIdStringGeneratorEndpointTest.java @@ -32,13 +32,13 @@ public CosIdStringGeneratorEndpointTest() { @Test void shareGenerate() { - var id = cosIdGeneratorEndpoint.shareGenerateAsString(); + String id = cosIdGeneratorEndpoint.shareGenerateAsString(); assertThat(id, notNullValue()); } @Test void generate() { - var id = cosIdGeneratorEndpoint.generateAsString(IdGeneratorProvider.SHARE); + String id = cosIdGeneratorEndpoint.generateAsString(IdGeneratorProvider.SHARE); assertThat(id, notNullValue()); } } \ No newline at end of file diff --git a/cosid-spring-data-jdbc/src/test/java/me/ahoo/cosid/spring/data/jdbc/CosIdBeforeConvertCallbackTest.java b/cosid-spring-data-jdbc/src/test/java/me/ahoo/cosid/spring/data/jdbc/CosIdBeforeConvertCallbackTest.java index 326e25a13e..26d1c4c3ae 100644 --- a/cosid-spring-data-jdbc/src/test/java/me/ahoo/cosid/spring/data/jdbc/CosIdBeforeConvertCallbackTest.java +++ b/cosid-spring-data-jdbc/src/test/java/me/ahoo/cosid/spring/data/jdbc/CosIdBeforeConvertCallbackTest.java @@ -34,7 +34,7 @@ class CosIdBeforeConvertCallbackTest { private final CosIdBeforeConvertCallback cosIdBeforeConvertCallback; public CosIdBeforeConvertCallbackTest() { - var fieldDefinitionParser = new CompositeFieldDefinitionParser(Arrays.asList(AnnotationDefinitionParser.INSTANCE, IdAnnotationDefinitionParser.INSTANCE)); + CompositeFieldDefinitionParser fieldDefinitionParser = new CompositeFieldDefinitionParser(Arrays.asList(AnnotationDefinitionParser.INSTANCE, IdAnnotationDefinitionParser.INSTANCE)); DefaultAccessorParser accessorParser = new DefaultAccessorParser(fieldDefinitionParser); CosIdAccessorRegistry accessorRegistry = new DefaultAccessorRegistry(accessorParser); cosIdBeforeConvertCallback = new CosIdBeforeConvertCallback(accessorRegistry); @@ -42,7 +42,7 @@ public CosIdBeforeConvertCallbackTest() { @Test void onBeforeConvertIfId() { - var entity = new IdEntity(); + IdEntity entity = new IdEntity(); DefaultIdGeneratorProvider.INSTANCE.setShare(MockIdGenerator.INSTANCE); cosIdBeforeConvertCallback.onBeforeConvert(entity); assertThat(entity.getId(), not(0)); @@ -50,7 +50,7 @@ void onBeforeConvertIfId() { @Test void onBeforeConvertIfCosId() { - var entity = new CosIdEntity(); + CosIdEntity entity = new CosIdEntity(); DefaultIdGeneratorProvider.INSTANCE.setShare(MockIdGenerator.INSTANCE); cosIdBeforeConvertCallback.onBeforeConvert(entity); assertThat(entity.getId(), not(0)); @@ -58,7 +58,7 @@ void onBeforeConvertIfCosId() { @Test void onBeforeConvertIfNotFound() { - var entity = new NotFoundEntity(); + NotFoundEntity entity = new NotFoundEntity(); DefaultIdGeneratorProvider.INSTANCE.setShare(MockIdGenerator.INSTANCE); cosIdBeforeConvertCallback.onBeforeConvert(entity); assertThat(entity.getId(), equalTo(0L)); diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9dead90ce0..26d8cca97f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,15 +1,15 @@ [versions] # libraries -springBoot = "3.3.1" -springCloud = "2023.0.2" +springBoot = "2.7.12" +springCloud = "2021.0.7" okhttp = "4.12.0" testcontainers = "1.19.8" guava = "33.2.1-jre" mybatis = "3.5.16" mybatisSpringBoot = "3.0.3" -junitPioneer = "2.2.0" +junitPioneer = "1.9.1" axon = "4.9.4" -flowable = "7.0.1" +flowable = "6.8.1" activiti = "7.0.0.SR1" springDoc = "2.6.0" hamcrest = "2.2"