Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import io.microsphere.util.Utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand Down Expand Up @@ -687,6 +689,50 @@
return values.contains(newValue) ? false : values.add(newValue);
}

/**
* Creates a new empty {@link CopyOnWriteArrayList} instance.
*
* <p>This method provides a convenient way to create an empty copy-on-write array list.
* The returned list is thread-safe and allows for safe concurrent reads and writes.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* List<String> list = ListUtils.newCopyOnWriteArrayList();
* System.out.println(list.isEmpty()); // Output: true
*
* list.add("Hello");
* System.out.println(list); // Output: [Hello]
* }</pre>
*
* @param <E> the type of elements in the list
* @return a new, empty {@link CopyOnWriteArrayList}
*/
@Nonnull
public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList() {

Check warning on line 711 in microsphere-java-core/src/main/java/io/microsphere/collection/ListUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The return type of this method should be an interface such as "List" rather than the implementation "CopyOnWriteArrayList".

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ54IDfSyyFq8UYE0zJm&open=AZ54IDfSyyFq8UYE0zJm&pullRequest=286
return new CopyOnWriteArrayList<>();
}

/**
* Creates a new {@link CopyOnWriteArrayList} instance from the specified {@link Collection}.
*
* <p>This method converts the given {@link Collection} into a {@link CopyOnWriteArrayList}.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* List<String> original = Arrays.asList("apple", "banana", "cherry");
* List<String> list = ListUtils.newCopyOnWriteArrayList(original);
* System.out.println(list); // Output: [apple, banana, cherry]
* }</pre>
*
* @param elements the collection of elements to add to the list, may be null or empty
* @param <E> the type of elements in the collection
* @return a new {@link CopyOnWriteArrayList} containing all elements from the collection
*/
@Nonnull
public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(Collection<? extends E> elements) {

Check warning on line 732 in microsphere-java-core/src/main/java/io/microsphere/collection/ListUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The return type of this method should be an interface such as "List" rather than the implementation "CopyOnWriteArrayList".

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ54IDfSyyFq8UYE0zJn&open=AZ54IDfSyyFq8UYE0zJn&pullRequest=286
return new CopyOnWriteArrayList<>(elements);
}

private ListUtils() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentNavigableMap;
Expand Down Expand Up @@ -1255,6 +1256,189 @@
return properties;
}

/**
* Creates a new empty {@link ConcurrentSkipListMap} instance.
*
* <p>This method provides a convenient way to create an empty concurrent skip list map.
* The returned map is thread-safe and allows for efficient concurrent operations.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* Map<String, String> map = MapUtils.newConcurrentSkipListMap();
* System.out.println(map.isEmpty()); // Output: true
*
* map.put("key", "value");
* System.out.println(map); // Output: {key=value}
* }</pre>
*
* @param <K> the type of keys in the map
* @param <V> the type of values in the map
* @return a new, empty {@link ConcurrentSkipListMap}
*/
@Nonnull
public static <K, V> ConcurrentSkipListMap<K, V> newConcurrentSkipListMap() {

Check warning on line 1279 in microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The return type of this method should be an interface such as "ConcurrentMap" rather than the implementation "ConcurrentSkipListMap".

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ54IDcGyyFq8UYE0zJZ&open=AZ54IDcGyyFq8UYE0zJZ&pullRequest=286
return new ConcurrentSkipListMap<>();
}

/**
* Creates a new {@link ConcurrentSkipListMap} instance from the specified {@link Map}.
*
* <p>This method converts the given {@link Map} into a {@link ConcurrentSkipListMap}.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* Map<String, String> original = new HashMap<>();
* original.put("key", "value");
* Map<String, String> map = MapUtils.newConcurrentSkipListMap(original);
* System.out.println(map); // Output: {key=value}
* }</pre>
*
* @param map the map whose entries are to be copied, may be null or empty
* @param <K> the type of keys in the map
* @param <V> the type of values in the map
* @return a new {@link ConcurrentSkipListMap} containing all entries from the map
*/
@Nonnull
public static <K, V> ConcurrentSkipListMap<K, V> newConcurrentSkipListMap(Map<? extends K, ? extends V> map) {

Check warning on line 1302 in microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The return type of this method should be an interface such as "ConcurrentMap" rather than the implementation "ConcurrentSkipListMap".

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ54IDcGyyFq8UYE0zJa&open=AZ54IDcGyyFq8UYE0zJa&pullRequest=286
return new ConcurrentSkipListMap<>(map);
}

/**
* Creates a new empty {@link WeakHashMap} instance.
*
* <p>This method provides a convenient way to create an empty weak hash map.
* The returned map allows weak reference keys that can be garbage collected.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* Map<String, String> map = MapUtils.newWeakHashMap();
* System.out.println(map.isEmpty()); // Output: true
*
* map.put("key", "value");
* System.out.println(map); // Output: {key=value}
* }</pre>
*
* @param <K> the type of keys in the map
* @param <V> the type of values in the map
* @return a new, empty {@link WeakHashMap}
*/
@Nonnull
public static <K, V> WeakHashMap<K, V> newWeakHashMap() {

Check warning on line 1326 in microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The return type of this method should be an interface such as "Map" rather than the implementation "WeakHashMap".

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ54IDcGyyFq8UYE0zJb&open=AZ54IDcGyyFq8UYE0zJb&pullRequest=286
return new WeakHashMap<>();
}

/**
* Creates a new {@link WeakHashMap} instance with the specified initial capacity.
*
* <p>This method provides a convenient way to create a weak hash map with a predefined initial size.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* Map<String, String> map = MapUtils.newWeakHashMap(16);
* System.out.println(map.isEmpty()); // Output: true
* }</pre>
*
* @param initialCapacity the initial capacity of the weak hash map
* @param <K> the type of keys in the map
* @param <V> the type of values in the map
* @return a new, empty {@link WeakHashMap} with the specified initial capacity
*/
@Nonnull
public static <K, V> WeakHashMap<K, V> newWeakHashMap(int initialCapacity) {

Check warning on line 1347 in microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The return type of this method should be an interface such as "Map" rather than the implementation "WeakHashMap".

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ54IDcGyyFq8UYE0zJc&open=AZ54IDcGyyFq8UYE0zJc&pullRequest=286
return new WeakHashMap<>(initialCapacity);
}

/**
* Creates a new {@link WeakHashMap} instance from the specified {@link Map}.
*
* <p>This method converts the given {@link Map} into a {@link WeakHashMap}.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* Map<String, String> original = new HashMap<>();
* original.put("key", "value");
* Map<String, String> map = MapUtils.newWeakHashMap(original);
* System.out.println(map); // Output: {key=value}
* }</pre>
*
* @param map the map whose entries are to be copied, may be null or empty
* @param <K> the type of keys in the map
* @param <V> the type of values in the map
* @return a new {@link WeakHashMap} containing all entries from the map
*/
@Nonnull
public static <K, V> WeakHashMap<K, V> newWeakHashMap(Map<? extends K, ? extends V> map) {

Check warning on line 1370 in microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The return type of this method should be an interface such as "Map" rather than the implementation "WeakHashMap".

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ54IDcGyyFq8UYE0zJd&open=AZ54IDcGyyFq8UYE0zJd&pullRequest=286
return new WeakHashMap<>(map);
}

/**
* Creates a new empty {@link IdentityHashMap} instance.
*
* <p>This method provides a convenient way to create an empty identity hash map.
* The returned map uses reference equality instead of object equality for key comparisons.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* Map<String, String> map = MapUtils.newIdentityHashMap();
* System.out.println(map.isEmpty()); // Output: true
*
* map.put("key", "value");
* System.out.println(map); // Output: {key=value}
* }</pre>
*
* @param <K> the type of keys in the map
* @param <V> the type of values in the map
* @return a new, empty {@link IdentityHashMap}
*/
@Nonnull
public static <K, V> IdentityHashMap<K, V> newIdentityHashMap() {

Check warning on line 1394 in microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The return type of this method should be an interface such as "Map" rather than the implementation "IdentityHashMap".

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ54IDcGyyFq8UYE0zJe&open=AZ54IDcGyyFq8UYE0zJe&pullRequest=286
return new IdentityHashMap<>();
}

/**
* Creates a new {@link IdentityHashMap} instance with the specified expected maximum size.
*
* <p>This method provides a convenient way to create an identity hash map with a predefined initial size.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* Map<String, String> map = MapUtils.newIdentityHashMap(16);
* System.out.println(map.isEmpty()); // Output: true
* }</pre>
*
* @param expectedMaxSize the expected maximum size of the map
* @param <K> the type of keys in the map
* @param <V> the type of values in the map
* @return a new, empty {@link IdentityHashMap} with the expected maximum size
*/
@Nonnull
public static <K, V> IdentityHashMap<K, V> newIdentityHashMap(int expectedMaxSize) {

Check warning on line 1415 in microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The return type of this method should be an interface such as "Map" rather than the implementation "IdentityHashMap".

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ54IDcGyyFq8UYE0zJf&open=AZ54IDcGyyFq8UYE0zJf&pullRequest=286
return new IdentityHashMap<>(expectedMaxSize);
}

/**
* Creates a new {@link IdentityHashMap} instance from the specified {@link Map}.
*
* <p>This method converts the given {@link Map} into an {@link IdentityHashMap}.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* Map<String, String> original = new HashMap<>();
* original.put("key", "value");
* Map<String, String> map = MapUtils.newIdentityHashMap(original);
* System.out.println(map); // Output: {key=value}
* }</pre>
*
* @param map the map whose entries are to be copied, may be null or empty
* @param <K> the type of keys in the map
* @param <V> the type of values in the map
* @return a new {@link IdentityHashMap} containing all entries from the map
*/
@Nonnull
public static <K, V> IdentityHashMap<K, V> newIdentityHashMap(Map<? extends K, ? extends V> map) {

Check warning on line 1438 in microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The return type of this method should be an interface such as "Map" rather than the implementation "IdentityHashMap".

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ54IDcGyyFq8UYE0zJg&open=AZ54IDcGyyFq8UYE0zJg&pullRequest=286
return new IdentityHashMap<>(map);
}

private MapUtils() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,48 @@ private static String normalizePropertyName(String propertyNamePrefix, String pr
return propertyNamePrefix == null ? propertyName : propertyNamePrefix + DOT + propertyName;
}

/**
* Creates a new empty {@link Properties} instance.
*
* <p>This method provides a convenient way to create an empty properties object.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* Properties props = PropertiesUtils.newProperties();
* System.out.println(props.isEmpty()); // Output: true
*
* props.setProperty("key", "value");
* System.out.println(props); // Output: {key=value}
* }</pre>
*
* @return a new, empty {@link Properties}
*/
@Nonnull
public static Properties newProperties() {
return new Properties();
}

/**
* Creates a new {@link Properties} instance with the specified defaults.
*
* <p>This method provides a convenient way to create a properties object with default properties.</p>
*
* <h3>Example Usage</h3>
* <pre>{@code
* Properties defaults = new Properties();
* defaults.setProperty("default.key", "default.value");
* Properties props = PropertiesUtils.newProperties(defaults);
* System.out.println(props.getProperty("default.key")); // Output: default.value
* }</pre>
*
* @param defaults the default properties
* @return a new {@link Properties} with the specified defaults
*/
@Nonnull
public static Properties newProperties(Properties defaults) {
return new Properties(defaults);
}

private PropertiesUtils() {
}
}
Loading
Loading