Skip to content

Commit 761f0fb

Browse files
chore: merge main into release [skip ci]
2 parents 45b0198 + e73327b commit 761f0fb

10 files changed

Lines changed: 1146 additions & 16 deletions

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import io.microsphere.util.Utils;
2323

2424
import java.util.ArrayList;
25+
import java.util.Collection;
2526
import java.util.Enumeration;
2627
import java.util.Iterator;
2728
import java.util.LinkedList;
2829
import java.util.List;
30+
import java.util.concurrent.CopyOnWriteArrayList;
2931
import java.util.function.BiConsumer;
3032
import java.util.function.Consumer;
3133

@@ -687,6 +689,50 @@ public static <T> boolean addIfAbsent(List<T> values, T newValue) {
687689
return values.contains(newValue) ? false : values.add(newValue);
688690
}
689691

692+
/**
693+
* Creates a new empty {@link CopyOnWriteArrayList} instance.
694+
*
695+
* <p>This method provides a convenient way to create an empty copy-on-write array list.
696+
* The returned list is thread-safe and allows for safe concurrent reads and writes.</p>
697+
*
698+
* <h3>Example Usage</h3>
699+
* <pre>{@code
700+
* List<String> list = ListUtils.newCopyOnWriteArrayList();
701+
* System.out.println(list.isEmpty()); // Output: true
702+
*
703+
* list.add("Hello");
704+
* System.out.println(list); // Output: [Hello]
705+
* }</pre>
706+
*
707+
* @param <E> the type of elements in the list
708+
* @return a new, empty {@link CopyOnWriteArrayList}
709+
*/
710+
@Nonnull
711+
public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList() {
712+
return new CopyOnWriteArrayList<>();
713+
}
714+
715+
/**
716+
* Creates a new {@link CopyOnWriteArrayList} instance from the specified {@link Collection}.
717+
*
718+
* <p>This method converts the given {@link Collection} into a {@link CopyOnWriteArrayList}.</p>
719+
*
720+
* <h3>Example Usage</h3>
721+
* <pre>{@code
722+
* List<String> original = Arrays.asList("apple", "banana", "cherry");
723+
* List<String> list = ListUtils.newCopyOnWriteArrayList(original);
724+
* System.out.println(list); // Output: [apple, banana, cherry]
725+
* }</pre>
726+
*
727+
* @param elements the collection of elements to add to the list, may be null or empty
728+
* @param <E> the type of elements in the collection
729+
* @return a new {@link CopyOnWriteArrayList} containing all elements from the collection
730+
*/
731+
@Nonnull
732+
public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(Collection<? extends E> elements) {
733+
return new CopyOnWriteArrayList<>(elements);
734+
}
735+
690736
private ListUtils() {
691737
}
692738
}

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

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Map;
3030
import java.util.SortedMap;
3131
import java.util.TreeMap;
32+
import java.util.WeakHashMap;
3233
import java.util.concurrent.ConcurrentHashMap;
3334
import java.util.concurrent.ConcurrentMap;
3435
import java.util.concurrent.ConcurrentNavigableMap;
@@ -1255,6 +1256,189 @@ static Map<String, Object> extraProperties(Map<String, Object> map) {
12551256
return properties;
12561257
}
12571258

1259+
/**
1260+
* Creates a new empty {@link ConcurrentSkipListMap} instance.
1261+
*
1262+
* <p>This method provides a convenient way to create an empty concurrent skip list map.
1263+
* The returned map is thread-safe and allows for efficient concurrent operations.</p>
1264+
*
1265+
* <h3>Example Usage</h3>
1266+
* <pre>{@code
1267+
* Map<String, String> map = MapUtils.newConcurrentSkipListMap();
1268+
* System.out.println(map.isEmpty()); // Output: true
1269+
*
1270+
* map.put("key", "value");
1271+
* System.out.println(map); // Output: {key=value}
1272+
* }</pre>
1273+
*
1274+
* @param <K> the type of keys in the map
1275+
* @param <V> the type of values in the map
1276+
* @return a new, empty {@link ConcurrentSkipListMap}
1277+
*/
1278+
@Nonnull
1279+
public static <K, V> ConcurrentSkipListMap<K, V> newConcurrentSkipListMap() {
1280+
return new ConcurrentSkipListMap<>();
1281+
}
1282+
1283+
/**
1284+
* Creates a new {@link ConcurrentSkipListMap} instance from the specified {@link Map}.
1285+
*
1286+
* <p>This method converts the given {@link Map} into a {@link ConcurrentSkipListMap}.</p>
1287+
*
1288+
* <h3>Example Usage</h3>
1289+
* <pre>{@code
1290+
* Map<String, String> original = new HashMap<>();
1291+
* original.put("key", "value");
1292+
* Map<String, String> map = MapUtils.newConcurrentSkipListMap(original);
1293+
* System.out.println(map); // Output: {key=value}
1294+
* }</pre>
1295+
*
1296+
* @param map the map whose entries are to be copied, may be null or empty
1297+
* @param <K> the type of keys in the map
1298+
* @param <V> the type of values in the map
1299+
* @return a new {@link ConcurrentSkipListMap} containing all entries from the map
1300+
*/
1301+
@Nonnull
1302+
public static <K, V> ConcurrentSkipListMap<K, V> newConcurrentSkipListMap(Map<? extends K, ? extends V> map) {
1303+
return new ConcurrentSkipListMap<>(map);
1304+
}
1305+
1306+
/**
1307+
* Creates a new empty {@link WeakHashMap} instance.
1308+
*
1309+
* <p>This method provides a convenient way to create an empty weak hash map.
1310+
* The returned map allows weak reference keys that can be garbage collected.</p>
1311+
*
1312+
* <h3>Example Usage</h3>
1313+
* <pre>{@code
1314+
* Map<String, String> map = MapUtils.newWeakHashMap();
1315+
* System.out.println(map.isEmpty()); // Output: true
1316+
*
1317+
* map.put("key", "value");
1318+
* System.out.println(map); // Output: {key=value}
1319+
* }</pre>
1320+
*
1321+
* @param <K> the type of keys in the map
1322+
* @param <V> the type of values in the map
1323+
* @return a new, empty {@link WeakHashMap}
1324+
*/
1325+
@Nonnull
1326+
public static <K, V> WeakHashMap<K, V> newWeakHashMap() {
1327+
return new WeakHashMap<>();
1328+
}
1329+
1330+
/**
1331+
* Creates a new {@link WeakHashMap} instance with the specified initial capacity.
1332+
*
1333+
* <p>This method provides a convenient way to create a weak hash map with a predefined initial size.</p>
1334+
*
1335+
* <h3>Example Usage</h3>
1336+
* <pre>{@code
1337+
* Map<String, String> map = MapUtils.newWeakHashMap(16);
1338+
* System.out.println(map.isEmpty()); // Output: true
1339+
* }</pre>
1340+
*
1341+
* @param initialCapacity the initial capacity of the weak hash map
1342+
* @param <K> the type of keys in the map
1343+
* @param <V> the type of values in the map
1344+
* @return a new, empty {@link WeakHashMap} with the specified initial capacity
1345+
*/
1346+
@Nonnull
1347+
public static <K, V> WeakHashMap<K, V> newWeakHashMap(int initialCapacity) {
1348+
return new WeakHashMap<>(initialCapacity);
1349+
}
1350+
1351+
/**
1352+
* Creates a new {@link WeakHashMap} instance from the specified {@link Map}.
1353+
*
1354+
* <p>This method converts the given {@link Map} into a {@link WeakHashMap}.</p>
1355+
*
1356+
* <h3>Example Usage</h3>
1357+
* <pre>{@code
1358+
* Map<String, String> original = new HashMap<>();
1359+
* original.put("key", "value");
1360+
* Map<String, String> map = MapUtils.newWeakHashMap(original);
1361+
* System.out.println(map); // Output: {key=value}
1362+
* }</pre>
1363+
*
1364+
* @param map the map whose entries are to be copied, may be null or empty
1365+
* @param <K> the type of keys in the map
1366+
* @param <V> the type of values in the map
1367+
* @return a new {@link WeakHashMap} containing all entries from the map
1368+
*/
1369+
@Nonnull
1370+
public static <K, V> WeakHashMap<K, V> newWeakHashMap(Map<? extends K, ? extends V> map) {
1371+
return new WeakHashMap<>(map);
1372+
}
1373+
1374+
/**
1375+
* Creates a new empty {@link IdentityHashMap} instance.
1376+
*
1377+
* <p>This method provides a convenient way to create an empty identity hash map.
1378+
* The returned map uses reference equality instead of object equality for key comparisons.</p>
1379+
*
1380+
* <h3>Example Usage</h3>
1381+
* <pre>{@code
1382+
* Map<String, String> map = MapUtils.newIdentityHashMap();
1383+
* System.out.println(map.isEmpty()); // Output: true
1384+
*
1385+
* map.put("key", "value");
1386+
* System.out.println(map); // Output: {key=value}
1387+
* }</pre>
1388+
*
1389+
* @param <K> the type of keys in the map
1390+
* @param <V> the type of values in the map
1391+
* @return a new, empty {@link IdentityHashMap}
1392+
*/
1393+
@Nonnull
1394+
public static <K, V> IdentityHashMap<K, V> newIdentityHashMap() {
1395+
return new IdentityHashMap<>();
1396+
}
1397+
1398+
/**
1399+
* Creates a new {@link IdentityHashMap} instance with the specified expected maximum size.
1400+
*
1401+
* <p>This method provides a convenient way to create an identity hash map with a predefined initial size.</p>
1402+
*
1403+
* <h3>Example Usage</h3>
1404+
* <pre>{@code
1405+
* Map<String, String> map = MapUtils.newIdentityHashMap(16);
1406+
* System.out.println(map.isEmpty()); // Output: true
1407+
* }</pre>
1408+
*
1409+
* @param expectedMaxSize the expected maximum size of the map
1410+
* @param <K> the type of keys in the map
1411+
* @param <V> the type of values in the map
1412+
* @return a new, empty {@link IdentityHashMap} with the expected maximum size
1413+
*/
1414+
@Nonnull
1415+
public static <K, V> IdentityHashMap<K, V> newIdentityHashMap(int expectedMaxSize) {
1416+
return new IdentityHashMap<>(expectedMaxSize);
1417+
}
1418+
1419+
/**
1420+
* Creates a new {@link IdentityHashMap} instance from the specified {@link Map}.
1421+
*
1422+
* <p>This method converts the given {@link Map} into an {@link IdentityHashMap}.</p>
1423+
*
1424+
* <h3>Example Usage</h3>
1425+
* <pre>{@code
1426+
* Map<String, String> original = new HashMap<>();
1427+
* original.put("key", "value");
1428+
* Map<String, String> map = MapUtils.newIdentityHashMap(original);
1429+
* System.out.println(map); // Output: {key=value}
1430+
* }</pre>
1431+
*
1432+
* @param map the map whose entries are to be copied, may be null or empty
1433+
* @param <K> the type of keys in the map
1434+
* @param <V> the type of values in the map
1435+
* @return a new {@link IdentityHashMap} containing all entries from the map
1436+
*/
1437+
@Nonnull
1438+
public static <K, V> IdentityHashMap<K, V> newIdentityHashMap(Map<? extends K, ? extends V> map) {
1439+
return new IdentityHashMap<>(map);
1440+
}
1441+
12581442
private MapUtils() {
12591443
}
12601444

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,48 @@ private static String normalizePropertyName(String propertyNamePrefix, String pr
102102
return propertyNamePrefix == null ? propertyName : propertyNamePrefix + DOT + propertyName;
103103
}
104104

105+
/**
106+
* Creates a new empty {@link Properties} instance.
107+
*
108+
* <p>This method provides a convenient way to create an empty properties object.</p>
109+
*
110+
* <h3>Example Usage</h3>
111+
* <pre>{@code
112+
* Properties props = PropertiesUtils.newProperties();
113+
* System.out.println(props.isEmpty()); // Output: true
114+
*
115+
* props.setProperty("key", "value");
116+
* System.out.println(props); // Output: {key=value}
117+
* }</pre>
118+
*
119+
* @return a new, empty {@link Properties}
120+
*/
121+
@Nonnull
122+
public static Properties newProperties() {
123+
return new Properties();
124+
}
125+
126+
/**
127+
* Creates a new {@link Properties} instance with the specified defaults.
128+
*
129+
* <p>This method provides a convenient way to create a properties object with default properties.</p>
130+
*
131+
* <h3>Example Usage</h3>
132+
* <pre>{@code
133+
* Properties defaults = new Properties();
134+
* defaults.setProperty("default.key", "default.value");
135+
* Properties props = PropertiesUtils.newProperties(defaults);
136+
* System.out.println(props.getProperty("default.key")); // Output: default.value
137+
* }</pre>
138+
*
139+
* @param defaults the default properties
140+
* @return a new {@link Properties} with the specified defaults
141+
*/
142+
@Nonnull
143+
public static Properties newProperties(Properties defaults) {
144+
return new Properties(defaults);
145+
}
146+
105147
private PropertiesUtils() {
106148
}
107149
}

0 commit comments

Comments
 (0)