From 6859fe13e255a7fc868a5be99445512e7a295f16 Mon Sep 17 00:00:00 2001 From: jason <2353220944@qq.com> Date: Sun, 8 Dec 2024 19:30:09 +0800 Subject: [PATCH] code format --- .../internals/RemoteConfigRepository.java | 18 +++---- .../internals/RemoteConfigRepositoryTest.java | 26 +++++----- .../apollo/core/dto/ConfigurationChange.java | 6 +-- .../core/enums/ConfigurationChangeType.java | 12 ++++- .../enums/ConfigurationChangeTypeUtils.java | 50 +++++++++++++++++++ 5 files changed, 86 insertions(+), 26 deletions(-) create mode 100644 apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeTypeUtils.java diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java index 3ab7091f..58984015 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java @@ -26,6 +26,7 @@ import com.ctrip.framework.apollo.core.dto.ConfigurationChange; import com.ctrip.framework.apollo.core.dto.ServiceDTO; import com.ctrip.framework.apollo.core.enums.ConfigSyncType; +import com.ctrip.framework.apollo.core.enums.ConfigurationChangeType; import com.ctrip.framework.apollo.core.schedule.ExponentialSchedulePolicy; import com.ctrip.framework.apollo.core.schedule.SchedulePolicy; import com.ctrip.framework.apollo.core.signature.Signature; @@ -254,23 +255,21 @@ private ApolloConfig loadApolloConfig() { } ApolloConfig result = response.getBody(); + if(result!=null){ - ConfigSyncType configSyncType=ConfigSyncType.fromString(result.getConfigSyncType()); - if(configSyncType!=null&&configSyncType.equals(ConfigSyncType.INCREMENTALSYNC)){ + ConfigSyncType configSyncType=ConfigSyncType.fromString(result.getConfigSyncType()); - Map previousConfigurations=null; + if (configSyncType == ConfigSyncType.INCREMENTALSYNC) { ApolloConfig previousConfig = m_configCache.get(); - if(previousConfig!=null){ - previousConfigurations=previousConfig.getConfigurations(); - } + Map previousConfigurations = + (previousConfig != null) ? previousConfig.getConfigurations() : null; result.setConfigurations(mergeConfigurations(previousConfigurations,result.getConfigurationChanges())); } - } logger.debug("Loaded config for {}: {}", m_namespace, result); @@ -382,7 +381,8 @@ private List getConfigServices() { return services; } - public Map mergeConfigurations(Map previousConfigurations,List configurationChanges) { + Map mergeConfigurations(Map previousConfigurations, + List configurationChanges) { Map newConfigurations = new HashMap<>(); if(previousConfigurations!=null){ @@ -394,7 +394,7 @@ public Map mergeConfigurations(Map previousConfi } for (ConfigurationChange change : configurationChanges) { - switch (change.getConfigurationChangeType()) { + switch (ConfigurationChangeType.fromString(change.getConfigurationChangeType())) { case ADDED: case MODIFIED: newConfigurations.put(change.getKey(), change.getNewValue()); diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java index 77079b27..e0334ccb 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java @@ -169,18 +169,18 @@ public void testLoadConfigWithIncrementalSync() throws Exception { when(someResponse.getStatusCode()).thenReturn(200); when(someResponse.getBody()).thenReturn(someApolloConfig); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,someNamespace); remoteConfigRepository.sync(); List configurationChanges=new ArrayList<>(); String someNewValue = "someNewValue"; - configurationChanges.add(new ConfigurationChange(someKey, someNewValue, ConfigurationChangeType.MODIFIED)); - configurationChanges.add(new ConfigurationChange(someKey1, null, ConfigurationChangeType.DELETED)); + configurationChanges.add(new ConfigurationChange(someKey, someNewValue, "MODIFIED")); + configurationChanges.add(new ConfigurationChange(someKey1, null, "DELETED")); String someKey2 = "someKey2"; String someValue2 = "someValue2"; - configurationChanges.add(new ConfigurationChange(someKey2, someValue2, ConfigurationChangeType.ADDED)); + configurationChanges.add(new ConfigurationChange(someKey2, someValue2,"ADDED")); ApolloConfig someApolloConfigWithIncrementalSync = assembleApolloConfigWithIncrementalSync(configurationChanges); when(someResponse.getStatusCode()).thenReturn(200); @@ -208,13 +208,13 @@ public void testMergeConfigurations() throws Exception { Map previousConfigurations = ImmutableMap.of(key1, value1,key3,value3); List configurationChanges=new ArrayList<>(); - configurationChanges.add(new ConfigurationChange(key1, anotherValue1, ConfigurationChangeType.MODIFIED)); + configurationChanges.add(new ConfigurationChange(key1, anotherValue1, "MODIFIED")); String key2 = "key2"; String value2 = "value2"; - configurationChanges.add(new ConfigurationChange(key2, value2, ConfigurationChangeType.ADDED)); - configurationChanges.add(new ConfigurationChange(key3, null, ConfigurationChangeType.DELETED)); + configurationChanges.add(new ConfigurationChange(key2, value2, "ADDED")); + configurationChanges.add(new ConfigurationChange(key3, null, "DELETED")); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,someNamespace); Map result=remoteConfigRepository.mergeConfigurations(previousConfigurations, configurationChanges); assertEquals(2, result.size()); @@ -232,7 +232,7 @@ public void testMergeConfigurationWithPreviousConfigurationsIsNULL() throws Exce Map previousConfigurations = ImmutableMap.of(key1, value1,key3,value3); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,someNamespace); Map result=remoteConfigRepository.mergeConfigurations(previousConfigurations, null); assertEquals(2, result.size()); @@ -250,13 +250,13 @@ public void testMergeConfigurationWithChangesIsNULL() throws Exception { String value3 = "value3"; List configurationChanges=new ArrayList<>(); - configurationChanges.add(new ConfigurationChange(key1, anotherValue1, ConfigurationChangeType.MODIFIED)); + configurationChanges.add(new ConfigurationChange(key1, anotherValue1, "MODIFIED")); String key2 = "key2"; String value2 = "value2"; - configurationChanges.add(new ConfigurationChange(key2, value2, ConfigurationChangeType.ADDED)); - configurationChanges.add(new ConfigurationChange(key3, null, ConfigurationChangeType.DELETED)); + configurationChanges.add(new ConfigurationChange(key2, value2, "ADDED")); + configurationChanges.add(new ConfigurationChange(key3, null, "DELETED")); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,someNamespace); Map result=remoteConfigRepository.mergeConfigurations(null, configurationChanges); assertEquals(2, result.size()); diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java index 506ea5a1..e4595a59 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java @@ -26,7 +26,7 @@ public class ConfigurationChange { private final String key; private final String newValue; - private final ConfigurationChangeType configurationChangeType; + private final String configurationChangeType; /** * Constructor. @@ -34,7 +34,7 @@ public class ConfigurationChange { * @param newValue the value after change * @param configurationChangeType the change type */ - public ConfigurationChange(String key, String newValue, ConfigurationChangeType configurationChangeType) { + public ConfigurationChange(String key, String newValue, String configurationChangeType) { this.key = key; this.newValue = newValue; this.configurationChangeType = configurationChangeType; @@ -47,7 +47,7 @@ public String getNewValue() { return newValue; } - public ConfigurationChangeType getConfigurationChangeType() { + public String getConfigurationChangeType() { return configurationChangeType; } diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeType.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeType.java index 7edb47af..d3ac703d 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeType.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeType.java @@ -17,9 +17,19 @@ package com.ctrip.framework.apollo.core.enums; +import com.google.common.base.Preconditions; + /** * @author jason */ public enum ConfigurationChangeType { - ADDED, MODIFIED, DELETED + ADDED, MODIFIED, DELETED, UNKNOWN; + + public static ConfigurationChangeType fromString(String changeType) { + ConfigurationChangeType configurationChangeType = ConfigurationChangeTypeUtils.transformChangeType( + changeType); + Preconditions.checkArgument(configurationChangeType != UNKNOWN, + String.format("ConfigurationChangeType %s is invalid", changeType)); + return configurationChangeType; + } } diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeTypeUtils.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeTypeUtils.java new file mode 100644 index 00000000..4a5ef93c --- /dev/null +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeTypeUtils.java @@ -0,0 +1,50 @@ +/* + * Copyright 2022 Apollo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.ctrip.framework.apollo.core.enums; + +import com.ctrip.framework.apollo.core.utils.StringUtils; + +/** + * A utility class for the {@link ConfigurationChangeType} enum. + *

+ * The class provides simple functionalities that extend the capabilities of {@link ConfigurationChangeType} + * + * @author json + */ +public final class ConfigurationChangeTypeUtils { + + /** + * Transforms a given String to its matching {@link ConfigurationChangeType} + * + * @param changeType the String to convert + * @return the matching {@link ConfigurationChangeType} for the given String + */ + public static ConfigurationChangeType transformChangeType(String changeType) { + if (StringUtils.isBlank(changeType)) { + return ConfigurationChangeType.UNKNOWN; + } + + String cleanedChangeType = changeType.trim().toUpperCase(); + + try { + return ConfigurationChangeType.valueOf(cleanedChangeType); + } catch (IllegalArgumentException e) { + + return ConfigurationChangeType.UNKNOWN; + } + } +}