|
7 | 7 | import java.util.ArrayList; |
8 | 8 | import java.util.Arrays; |
9 | 9 | import java.util.Collection; |
10 | | -import java.util.Collections; |
11 | 10 | import java.util.HashMap; |
12 | 11 | import java.util.HashSet; |
13 | 12 | import java.util.Iterator; |
14 | 13 | import java.util.LinkedHashSet; |
15 | 14 | import java.util.List; |
16 | 15 | import java.util.Map; |
17 | | -import java.util.Map.Entry; |
18 | 16 | import java.util.Set; |
19 | 17 | import java.util.concurrent.ConcurrentHashMap; |
20 | 18 |
|
@@ -95,38 +93,47 @@ public static <T> Set<T> asSet(T... ts) { |
95 | 93 | return new HashSet<>( Arrays.asList( ts ) ); |
96 | 94 | } |
97 | 95 |
|
| 96 | + /** |
| 97 | + * Converts the provided list to an {@code java.util.ImmutableCollections}. |
| 98 | + * <p> |
| 99 | + * <b>NOTE:</b> the resulting list does not allow {@code null} values. |
| 100 | + * Attempt to convert a list with {@code null}s will result in an exception! |
| 101 | + * |
| 102 | + * @param list the list to convert. |
| 103 | + * @return the converted list. |
| 104 | + * @param <T> the type of the list elements. |
| 105 | + */ |
98 | 106 | public static <T> List<T> toImmutableList(List<? extends T> list) { |
99 | | - switch ( list.size() ) { |
100 | | - case 0: |
101 | | - return Collections.emptyList(); |
102 | | - case 1: |
103 | | - return Collections.singletonList( list.get( 0 ) ); |
104 | | - default: |
105 | | - return Collections.unmodifiableList( list ); |
106 | | - } |
| 107 | + return List.copyOf( list ); |
107 | 108 | } |
108 | 109 |
|
| 110 | + /** |
| 111 | + * Converts the provided set to an {@code java.util.ImmutableCollections}. |
| 112 | + * <p> |
| 113 | + * <b>NOTE:</b> the resulting set does not allow {@code null} values. |
| 114 | + * Attempt to convert a set with {@code null}s will result in an exception! |
| 115 | + * |
| 116 | + * @param set the set to convert. |
| 117 | + * @return the converted set. |
| 118 | + * @param <T> the type of the set elements. |
| 119 | + */ |
109 | 120 | public static <T> Set<T> toImmutableSet(Set<? extends T> set) { |
110 | | - switch ( set.size() ) { |
111 | | - case 0: |
112 | | - return Collections.emptySet(); |
113 | | - case 1: |
114 | | - return Collections.singleton( set.iterator().next() ); |
115 | | - default: |
116 | | - return Collections.unmodifiableSet( set ); |
117 | | - } |
| 121 | + return Set.copyOf( set ); |
118 | 122 | } |
119 | 123 |
|
| 124 | + /** |
| 125 | + * Converts the provided map to an {@code java.util.ImmutableCollections}. |
| 126 | + * <p> |
| 127 | + * <b>NOTE:</b> the resulting map does not allow {@code null} keys and values . |
| 128 | + * Attempt to convert a map with {@code null}s (either as key or a value) will result in an exception! |
| 129 | + * |
| 130 | + * @param map the map to convert. |
| 131 | + * @param <K> the type of the map keys. |
| 132 | + * @param <V> the type of the map values. |
| 133 | + * @return the converted map. |
| 134 | + */ |
120 | 135 | public static <K, V> Map<K, V> toImmutableMap(Map<K, V> map) { |
121 | | - switch ( map.size() ) { |
122 | | - case 0: |
123 | | - return Collections.emptyMap(); |
124 | | - case 1: |
125 | | - Entry<K, V> entry = map.entrySet().iterator().next(); |
126 | | - return Collections.singletonMap( entry.getKey(), entry.getValue() ); |
127 | | - default: |
128 | | - return Collections.unmodifiableMap( map ); |
129 | | - } |
| 136 | + return Map.copyOf( map ); |
130 | 137 | } |
131 | 138 |
|
132 | 139 | /** |
|
0 commit comments