Skip to content

Commit c46cf19

Browse files
authored
Refactor collection constructors to use factory methods (excluding utility classes)
1 parent d475a3b commit c46cf19

31 files changed

Lines changed: 84 additions & 61 deletions

microsphere-java-core/src/main/java/io/microsphere/classloading/ArtifactDetector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static io.microsphere.util.SystemUtils.JAVA_HOME;
2626
import static java.util.Collections.emptyList;
2727
import static java.util.Collections.unmodifiableList;
28+
import static io.microsphere.collection.SetUtils.*;
2829

2930
/**
3031
* The {@code ArtifactDetector} class is responsible for detecting and resolving artifacts from the classpath.
@@ -149,7 +150,7 @@ public Artifact detect(@Nonnull URL classPathURL) {
149150

150151
protected Set<URL> getClassPathURLs(boolean includedJdkLibraries) {
151152
Set<URL> urls = findAllClassPathURLs(classLoader);
152-
LinkedHashSet<URL> classPathURLs = new LinkedHashSet<>(urls);
153+
LinkedHashSet<URL> classPathURLs = newLinkedHashSet(urls);
153154
if (!includedJdkLibraries) {
154155
removeJdkClassPathURLs(classPathURLs);
155156
}

microsphere-java-core/src/main/java/io/microsphere/classloading/BannedArtifactClassLoadingExecutor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static io.microsphere.util.StringUtils.isBlank;
1919
import static io.microsphere.util.StringUtils.split;
2020
import static io.microsphere.util.SystemUtils.FILE_ENCODING;
21+
import static io.microsphere.collection.ListUtils.*;
2122

2223
/**
2324
* The executor for the banned artifacts that are loading by {@link ClassLoader}.
@@ -94,7 +95,7 @@ public void execute() {
9495
}
9596

9697
static List<MavenArtifact> loadBannedArtifactConfigs(ClassLoader classLoader) {
97-
LinkedList<MavenArtifact> bannedArtifactConfigs = new LinkedList<>();
98+
LinkedList<MavenArtifact> bannedArtifactConfigs = newLinkedList();
9899
try {
99100
Enumeration<URL> configResources = classLoader.getResources(CONFIG_LOCATION);
100101
while (configResources.hasMoreElements()) {
@@ -109,7 +110,7 @@ static List<MavenArtifact> loadBannedArtifactConfigs(ClassLoader classLoader) {
109110
}
110111

111112
static List<MavenArtifact> loadBannedArtifactConfigs(URL configResource) throws IOException {
112-
LinkedList<MavenArtifact> bannedArtifactConfigs = new LinkedList<>();
113+
LinkedList<MavenArtifact> bannedArtifactConfigs = newLinkedList();
113114
try (InputStream inputStream = configResource.openStream();
114115
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, ENCODING))
115116
) {

microsphere-java-core/src/main/java/io/microsphere/collection/AbstractDeque.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.AbstractQueue;
2020
import java.util.Deque;
2121
import java.util.NoSuchElementException;
22+
import static io.microsphere.collection.ListUtils.*;
2223

2324
/**
2425
* Abstract {@link Deque} implementation that provides default implementations for some of the
@@ -34,7 +35,7 @@
3435
* <h3>Example Usage</h3>
3536
* <pre>{@code
3637
* public class SimpleDeque<E> extends AbstractDeque<E> {
37-
* private final List<E> list = new ArrayList<>();
38+
* private final List<E> list = newArrayList();
3839
*
3940
* public void addFirst(E e) {
4041
* list.add(0, e);

microsphere-java-core/src/main/java/io/microsphere/collection/CollectionUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public static boolean equals(@Nullable Collection<?> one, @Nullable Collection<?
440440
*
441441
* <h3>Example Usage</h3>
442442
* <pre>{@code
443-
* Collection<String> collection = new ArrayList<>();
443+
* Collection<String> collection = newArrayList();
444444
* int count1 = CollectionUtils.addAll(collection, "a", "b", "c"); // returns 3
445445
*
446446
* int count2 = CollectionUtils.addAll(null, "a", "b"); // returns 0
@@ -486,7 +486,7 @@ public static <T> int addAll(@Nullable Collection<T> collection, T... newValues)
486486
*
487487
* <h3>Example Usage</h3>
488488
* <pre>{@code
489-
* Collection<String> collection = new ArrayList<>();
489+
* Collection<String> collection = newArrayList();
490490
* Iterable<String> values = Arrays.asList("a", "b", "c");
491491
* int count1 = CollectionUtils.addAll(collection, values); // returns 3
492492
*

microsphere-java-core/src/main/java/io/microsphere/collection/UnmodifiableDeque.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Iterator;
2525

2626
import static io.microsphere.collection.CollectionUtils.unmodifiableIterator;
27+
import static io.microsphere.collection.ListUtils.*;
2728

2829
/**
2930
* An unmodifiable view of a {@link Deque}. This implementation decorates a given deque and prevents any
@@ -40,7 +41,7 @@
4041
* </p>
4142
*
4243
* <pre>{@code
43-
* Deque<String> mutableDeque = new LinkedList<>();
44+
* Deque<String> mutableDeque = newLinkedList();
4445
* mutableDeque.addFirst("World");
4546
* Deque<String> unmodifiableDeque = new UnmodifiableDeque<>(mutableDeque);
4647
* unmodifiableDeque.addFirst("Hello"); // throws UnsupportedOperationException

microsphere-java-core/src/main/java/io/microsphere/collection/UnmodifiableQueue.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.stream.Stream;
3030

3131
import static io.microsphere.collection.CollectionUtils.unmodifiableIterator;
32+
import static io.microsphere.collection.ListUtils.*;
3233

3334
/**
3435
* An unmodifiable view of a {@link Queue}. This implementation decorates a given queue and prevents any
@@ -45,7 +46,7 @@
4546
* </p>
4647
*
4748
* <pre>{@code
48-
* Queue<String> mutableQueue = new LinkedList<>();
49+
* Queue<String> mutableQueue = newLinkedList();
4950
* mutableQueue.add("Hello");
5051
* Queue<String> unmodifiableQueue = new UnmodifiableQueue<>(mutableQueue);
5152
* unmodifiableQueue.add("World"); // throws UnsupportedOperationException

microsphere-java-core/src/main/java/io/microsphere/event/AbstractEventDispatcher.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static io.microsphere.util.ServiceLoaderUtils.loadServicesList;
4141
import static java.util.Collections.sort;
4242
import static java.util.Collections.unmodifiableList;
43+
import static io.microsphere.collection.ListUtils.*;
4344

4445
/**
4546
* Abstract implementation of {@link EventDispatcher} that provides common functionality for dispatching events to
@@ -127,7 +128,7 @@ public void removeEventListener(EventListener<?> listener) throws NullPointerExc
127128
@Override
128129
@Immutable
129130
public List<EventListener<?>> getAllEventListeners() {
130-
LinkedList<EventListener<?>> listeners = new LinkedList<>();
131+
LinkedList<EventListener<?>> listeners = newLinkedList();
131132

132133
sortedListeners().forEach(listener -> {
133134
addIfAbsent(listeners, listener);
@@ -192,7 +193,7 @@ protected void doInListener(EventListener<?> listener, Consumer<Collection<Event
192193
void doInListener(Class<? extends Event> eventType, Consumer<Collection<EventListener>> consumer) {
193194
if (eventType != null) {
194195
synchronized (mutex) {
195-
List<EventListener> listeners = listenersCache.computeIfAbsent(eventType, e -> new LinkedList<>());
196+
List<EventListener> listeners = listenersCache.computeIfAbsent(eventType, e -> newLinkedList());
196197
// consume
197198
consumer.accept(listeners);
198199
// sort

microsphere-java-core/src/main/java/io/microsphere/event/GenericEventListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static io.microsphere.lang.function.ThrowableConsumer.execute;
3030
import static java.util.Collections.emptySet;
3131
import static java.util.stream.Stream.of;
32+
import static io.microsphere.collection.SetUtils.*;
3233

3334
/**
3435
* A generic implementation of the {@link EventListener} interface that supports multiple event handling methods.
@@ -97,7 +98,7 @@ private Map<Class<?>, Set<Method>> findHandleEventMethods() {
9798
HashMap<Class<?>, Set<Method>> eventMethods = newHashMap();
9899
of(getClass().getMethods()).filter(this::isHandleEventMethod).forEach(method -> {
99100
Class<?> paramType = method.getParameterTypes()[0];
100-
Set<Method> methods = eventMethods.computeIfAbsent(paramType, key -> new LinkedHashSet<>());
101+
Set<Method> methods = eventMethods.computeIfAbsent(paramType, key -> newLinkedHashSet());
101102
methods.add(method);
102103
});
103104
return eventMethods;

microsphere-java-core/src/main/java/io/microsphere/event/Listenable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static io.microsphere.util.ClassUtils.getTypeName;
2828
import static java.lang.reflect.Modifier.isFinal;
2929
import static java.util.stream.StreamSupport.stream;
30+
import static io.microsphere.collection.ListUtils.*;
3031

3132
/**
3233
* A component that allows registration and management of {@link EventListener} instances.
@@ -111,7 +112,7 @@ static void assertListener(EventListener<?> listener) throws IllegalArgumentExce
111112
*/
112113
default void addEventListeners(E listener, E... others) throws NullPointerException,
113114
IllegalArgumentException {
114-
ArrayList<E> listeners = new ArrayList<>(1 + others.length);
115+
ArrayList<E> listeners = newArrayList(1 + others.length);
115116
listeners.add(listener);
116117
addAll(listeners, others);
117118
addEventListeners(listeners);

microsphere-java-core/src/main/java/io/microsphere/filter/FilterUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.List;
1313

1414
import static java.util.Collections.unmodifiableList;
15+
import static io.microsphere.collection.ListUtils.*;
1516

1617
/**
1718
* Utility class for working with {@link Filter} instances.
@@ -74,7 +75,7 @@ public static <E> List<E> filter(Iterable<E> iterable, Filter<E> filter) {
7475
@Nonnull
7576
@Immutable
7677
public static <E> List<E> filter(Iterable<E> iterable, FilterOperator filterOperator, Filter<E>... filters) {
77-
ArrayList<E> list = new ArrayList<E>();
78+
ArrayList<E> list = newArrayList();
7879
Iterator<E> iterator = iterable.iterator();
7980
while (iterator.hasNext()) {
8081
E element = iterator.next();

0 commit comments

Comments
 (0)