Skip to content

Commit dbb4d67

Browse files
committed
Replace raw collection instantiations with utils
Replace direct usages of new ArrayList/HashMap/HashSet/LinkedHashSet/LinkedList/ConcurrentHashMap/LinkedHashMap with io.microsphere.collection utility factories (newArrayList, newHashMap, newLinkedList, newLinkedHashMap, newConcurrentHashMap, newLinkedHashSet, newHashSet, etc.) across spring-cloud-commons and openfeign modules. Added corresponding static imports and minor import reorderings; moved a Nonnull import and adjusted some test imports. No functional behavior changes intended—this is a refactor to centralize collection creation and use consistent utility helpers.
1 parent 3f99a3d commit dbb4d67

22 files changed

Lines changed: 57 additions & 55 deletions

File tree

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/discovery/UnionDiscoveryClient.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
import org.springframework.context.ApplicationContext;
2626
import org.springframework.context.ApplicationContextAware;
2727

28-
import java.util.ArrayList;
29-
import java.util.LinkedHashSet;
30-
import java.util.LinkedList;
3128
import java.util.List;
29+
import java.util.Set;
3230

3331
import static io.microsphere.collection.CollectionUtils.isNotEmpty;
32+
import static io.microsphere.collection.ListUtils.newArrayList;
33+
import static io.microsphere.collection.ListUtils.newLinkedList;
34+
import static io.microsphere.collection.SetUtils.newLinkedHashSet;
3435
import static io.microsphere.reflect.TypeUtils.getClassName;
3536
import static io.microsphere.spring.beans.BeanUtils.getSortedBeans;
3637
import static io.microsphere.spring.cloud.client.discovery.constants.DiscoveryClientConstants.COMPOSITE_DISCOVERY_CLIENT_CLASS_NAME;
@@ -85,7 +86,7 @@ public String description() {
8586
*/
8687
@Override
8788
public List<ServiceInstance> getInstances(String serviceId) {
88-
List<ServiceInstance> serviceInstances = new LinkedList<>();
89+
List<ServiceInstance> serviceInstances = newLinkedList();
8990
List<DiscoveryClient> discoveryClients = getDiscoveryClients();
9091
for (DiscoveryClient discoveryClient : discoveryClients) {
9192
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
@@ -109,15 +110,15 @@ public List<ServiceInstance> getInstances(String serviceId) {
109110
*/
110111
@Override
111112
public List<String> getServices() {
112-
LinkedHashSet<String> services = new LinkedHashSet<>();
113+
Set<String> services = newLinkedHashSet();
113114
List<DiscoveryClient> discoveryClients = getDiscoveryClients();
114115
for (DiscoveryClient discoveryClient : discoveryClients) {
115116
List<String> serviceForClient = discoveryClient.getServices();
116117
if (isNotEmpty(serviceForClient)) {
117118
services.addAll(serviceForClient);
118119
}
119120
}
120-
return new ArrayList<>(services);
121+
return newArrayList(services);
121122
}
122123

123124
/**
@@ -140,7 +141,7 @@ public List<DiscoveryClient> getDiscoveryClients() {
140141
return discoveryClients;
141142
}
142143

143-
discoveryClients = new ArrayList<>();
144+
discoveryClients = newLinkedList();
144145
for (DiscoveryClient discoveryClient : getSortedBeans(this.context, DiscoveryClient.class)) {
145146
String className = getClassName(discoveryClient.getClass());
146147
if (COMPOSITE_DISCOVERY_CLIENT_CLASS_NAME.equals(className) || this.equals(discoveryClient)) {

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/InMemoryServiceRegistry.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
2121

2222
import java.util.Map;
23-
import java.util.concurrent.ConcurrentHashMap;
2423
import java.util.concurrent.ConcurrentMap;
2524

25+
import static io.microsphere.collection.MapUtils.newConcurrentHashMap;
26+
2627
/**
2728
* In-Memory {@link ServiceRegistry}
2829
*
@@ -33,7 +34,7 @@ public class InMemoryServiceRegistry implements ServiceRegistry {
3334

3435
private static final String STATUS_KEY = "_status_";
3536

36-
private final ConcurrentMap<String, Registration> storage = new ConcurrentHashMap<>(1);
37+
private final ConcurrentMap<String, Registration> storage = newConcurrentHashMap(2);
3738

3839
/**
3940
* Registers the given {@link Registration} instance in the in-memory storage,

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/MultipleRegistration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import java.net.URI;
66
import java.util.Collection;
7-
import java.util.HashMap;
87
import java.util.List;
98
import java.util.Map;
109

10+
import static io.microsphere.collection.MapUtils.newHashMap;
1111
import static io.microsphere.util.Assert.assertNotEmpty;
1212
import static io.microsphere.util.ClassUtils.findAllClasses;
1313
import static io.microsphere.util.ClassUtils.isAssignableFrom;
@@ -23,7 +23,7 @@
2323
*/
2424
public class MultipleRegistration implements Registration {
2525

26-
private Map<Class<? extends Registration>, Registration> registrationMap = new HashMap<>();
26+
private Map<Class<? extends Registration>, Registration> registrationMap = newHashMap();
2727

2828
private Registration defaultRegistration;
2929

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/MultipleServiceRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import org.springframework.cloud.client.serviceregistry.Registration;
44
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
55

6-
import java.util.HashMap;
76
import java.util.List;
87
import java.util.Map;
98
import java.util.function.BiConsumer;
109
import java.util.function.Consumer;
1110

11+
import static io.microsphere.collection.MapUtils.newHashMap;
1212
import static io.microsphere.util.Assert.assertNotEmpty;
1313
import static org.springframework.aop.framework.AopProxyUtils.ultimateTargetClass;
1414
import static org.springframework.core.ResolvableType.forClass;
@@ -56,7 +56,7 @@ public MultipleServiceRegistry(Map<String, ServiceRegistry> registriesMap) {
5656
assertNotEmpty(registriesMap, () -> "registrations cannot be empty");
5757

5858
this.registriesMap = registriesMap;
59-
this.beanNameToRegistrationTypesMap = new HashMap<>(registriesMap.size());
59+
this.beanNameToRegistrationTypesMap = newHashMap(registriesMap.size());
6060

6161
for (Map.Entry<String, ServiceRegistry> entry : registriesMap.entrySet()) {
6262
String beanName = entry.getKey();

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/RegistrationMetaData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import java.util.Collections;
88
import java.util.Map;
99
import java.util.Set;
10-
import java.util.concurrent.ConcurrentHashMap;
1110

11+
import static io.microsphere.collection.MapUtils.newConcurrentHashMap;
1212
import static io.microsphere.reflect.MethodUtils.invokeMethod;
1313
import static io.microsphere.spring.cloud.client.service.util.ServiceInstanceUtils.removeMetadata;
1414
import static io.microsphere.spring.cloud.client.service.util.ServiceInstanceUtils.setMetadata;
@@ -71,7 +71,7 @@ public final class RegistrationMetaData implements Map<String, String> {
7171
public RegistrationMetaData(Collection<Registration> registrations) {
7272
assertNotEmpty(registrations, () -> "registrations cannot be empty");
7373
this.registrations = registrations;
74-
this.applicationMetaData = new ConcurrentHashMap<>();
74+
this.applicationMetaData = newConcurrentHashMap();
7575
for (Registration registration : registrations) {
7676
initializeIfZookeeperRegistrationAvailable(registration);
7777

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/SimpleServiceRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryProperties;
2323
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
2424

25-
import java.util.ArrayList;
2625
import java.util.List;
2726
import java.util.Map;
2827

28+
import static io.microsphere.collection.ListUtils.newLinkedList;
2929
import static io.microsphere.spring.cloud.client.discovery.util.DiscoveryUtils.getInstancesMap;
3030
import static io.microsphere.spring.cloud.client.service.util.ServiceInstanceUtils.getMetadata;
3131
import static io.microsphere.spring.cloud.client.service.util.ServiceInstanceUtils.setMetadata;
@@ -217,6 +217,6 @@ List<DefaultServiceInstance> getInstances(DefaultRegistration registration) {
217217
* @return the list of instances for the service ID
218218
*/
219219
List<DefaultServiceInstance> getInstances(String serviceId) {
220-
return this.instancesMap.computeIfAbsent(serviceId, k -> new ArrayList<>());
220+
return this.instancesMap.computeIfAbsent(serviceId, k -> newLinkedList());
221221
}
222222
}

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebServiceRegistryAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
import org.springframework.context.annotation.Configuration;
3333

3434
import java.util.Collection;
35-
import java.util.HashSet;
3635
import java.util.Iterator;
3736
import java.util.Set;
3837

38+
import static io.microsphere.collection.SetUtils.newHashSet;
3939
import static io.microsphere.logging.LoggerFactory.getLogger;
4040
import static io.microsphere.spring.cloud.client.service.util.ServiceInstanceUtils.attachMetadata;
4141

@@ -84,7 +84,7 @@ public final void onApplicationEvent(WebEndpointMappingsReadyEvent event) {
8484
}
8585

8686
private void attachWebMappingsMetadata(Registration registration, Collection<WebEndpointMapping> webEndpointMappings) {
87-
Set<WebEndpointMapping> mappings = new HashSet<>(webEndpointMappings);
87+
Set<WebEndpointMapping> mappings = newHashSet(webEndpointMappings);
8888
excludeMappings(mappings);
8989
attachMetadata(getContextPath(), registration, mappings);
9090
}

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/endpoint/ServiceRegistrationEndpoint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
66
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
77

8-
import java.util.LinkedHashMap;
98
import java.util.Map;
109

10+
import static io.microsphere.collection.MapUtils.newLinkedHashMap;
1111
import static io.microsphere.lang.function.ThrowableSupplier.execute;
1212
import static io.microsphere.reflect.MethodUtils.invokeMethod;
1313

@@ -39,7 +39,7 @@ public class ServiceRegistrationEndpoint extends AbstractServiceRegistrationEndp
3939
*/
4040
@ReadOperation
4141
public Map<String, Object> metadata() {
42-
Map<String, Object> metadata = new LinkedHashMap<>(16);
42+
Map<String, Object> metadata = newLinkedHashMap(16);
4343
metadata.put("application-name", applicationName);
4444
metadata.put("registration", registration);
4545
metadata.put("port", port);

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
*/
1717
package io.microsphere.spring.cloud.client.service.registry.event;
1818

19+
import io.microsphere.annotation.Nonnull;
1920
import org.springframework.cloud.client.serviceregistry.Registration;
2021
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
2122
import org.springframework.context.ApplicationEvent;
22-
import io.microsphere.annotation.Nonnull;
2323
import org.springframework.util.Assert;
2424

2525
import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.DEREGISTERED;

microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/discovery/ReactiveDiscoveryClientAdapterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
import reactor.core.publisher.Flux;
3030
import reactor.core.scheduler.Scheduler;
3131

32-
import java.util.HashMap;
3332
import java.util.List;
3433
import java.util.Map;
3534
import java.util.concurrent.Callable;
3635

3736
import static io.microsphere.collection.Lists.ofList;
37+
import static io.microsphere.collection.MapUtils.newHashMap;
3838
import static io.microsphere.spring.cloud.client.discovery.ReactiveDiscoveryClientAdapter.toList;
3939
import static io.microsphere.spring.cloud.client.service.util.ServiceInstanceUtilsTest.createDefaultServiceInstance;
4040
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -65,7 +65,7 @@ class ReactiveDiscoveryClientAdapterTest {
6565

6666
@BeforeEach
6767
void setUp() {
68-
Map<String, List<DefaultServiceInstance>> instances = new HashMap<>();
68+
Map<String, List<DefaultServiceInstance>> instances = newHashMap();
6969
this.serviceInstance = createDefaultServiceInstance();
7070
this.appName = this.serviceInstance.getServiceId();
7171
instances.put(appName, ofList(this.serviceInstance));

0 commit comments

Comments
 (0)