|
2 | 2 |
|
3 | 3 | import org.slf4j.Logger; |
4 | 4 | import org.slf4j.LoggerFactory; |
| 5 | +import org.unify4j.model.c.Pair; |
5 | 6 |
|
6 | 7 | import java.lang.reflect.Array; |
7 | 8 | import java.util.*; |
| 9 | +import java.util.concurrent.ConcurrentHashMap; |
8 | 10 | import java.util.function.Predicate; |
9 | 11 | import java.util.stream.Collectors; |
10 | 12 | import java.util.stream.Stream; |
@@ -649,6 +651,29 @@ public static String toString(Collection<?> collections, String delimiter) { |
649 | 651 | return builder.substring(delimiter.length()); |
650 | 652 | } |
651 | 653 |
|
| 654 | + /** |
| 655 | + * Creates an unmodifiable map from an array of Pair objects. |
| 656 | + * <p> |
| 657 | + * This method takes a variable number of Pair objects and constructs |
| 658 | + * a thread-safe map (using ConcurrentHashMap) from these pairs. The |
| 659 | + * resulting map is then wrapped with Collections.unmodifiableMap to |
| 660 | + * ensure it cannot be modified after creation. |
| 661 | + * |
| 662 | + * @param <KeyT> the type of keys maintained by the returned map |
| 663 | + * @param <ValueT> the type of mapped values |
| 664 | + * @param pairs an array of Pair objects from which the map will be created |
| 665 | + * @return an unmodifiable map containing the key-value pairs from the pairs array |
| 666 | + * @throws NullPointerException if any of the keys or values in the pairs are null |
| 667 | + */ |
| 668 | + @SafeVarargs |
| 669 | + public static <KeyT, ValueT> Map<KeyT, ValueT> mapOf(Pair<KeyT, ValueT>... pairs) { |
| 670 | + Map<KeyT, ValueT> map = new ConcurrentHashMap<>(pairs.length); |
| 671 | + for (Pair<KeyT, ValueT> pair : pairs) { |
| 672 | + map.put(pair.getKey(), pair.getValue()); |
| 673 | + } |
| 674 | + return Collections.unmodifiableMap(map); |
| 675 | + } |
| 676 | + |
652 | 677 | /** |
653 | 678 | * Throws UnsupportedOperationException if the list is not of type ArrayList. |
654 | 679 | * |
|
0 commit comments