Skip to content

Commit 2d8f05f

Browse files
committed
Update Collections.
1 parent d64bc6d commit 2d8f05f

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,15 +1028,18 @@ public static <K, V> Map<K, V> mapOf(Map.Entry<K, V>... entries) { ... }
10281028
public static <E> Set<E> setOf(E... elements) { ... }
10291029
```
10301030

1031-
They offer an alternative to similar methods defined by the `List`, `Map`, and `Set` interfaces, which produce immutable instances and do not permit `null` values. The following immutable variants are provided as well:
1031+
They offer an alternative to similar methods defined by the `List`, `Map`, and `Set` interfaces, which produce immutable instances and do not permit `null` values. The following immutable and sorted variants are provided as well:
10321032

10331033
```java
10341034
public static <E> List<E> immutableListOf(E... elements) { ... }
10351035
public static <K, V> Map<K, V> immutableMapOf(Map.Entry<K, V>... entries) { ... }
10361036
public static <E> Set<E> immutableSetOf(E... elements) { ... }
1037+
1038+
public static <K extends Comparable<K>, V> SortedMap<K, V> sortedMapOf(Map.Entry<K, V>... entries) { ... }
1039+
public static <E extends Comparable<E>> SortedSet<E> sortedSetOf(E... elements) { ... }
10371040
```
10381041

1039-
This method can be used to declare entries for either `mapOf()` or `immutableMapOf()`:
1042+
This method can be used to declare map entries:
10401043

10411044
```java
10421045
public static <K, V> Map.Entry<K, V> entry(K key, V value) { ... }

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
subprojects {
1616
group = 'org.httprpc'
17-
version = '5.3'
17+
version = '5.3.1'
1818

1919
repositories {
2020
mavenCentral()

kilo-client/src/main/java/org/httprpc/kilo/util/Collections.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Set;
24+
import java.util.SortedMap;
25+
import java.util.SortedSet;
26+
import java.util.TreeMap;
27+
import java.util.TreeSet;
2428

2529
/**
2630
* Provides static utility methods for working with collections.
@@ -115,6 +119,32 @@ public static <K, V> Map<K, V> immutableMapOf(Map.Entry<K, V>... entries) {
115119
return java.util.Collections.unmodifiableMap(mapOf(entries));
116120
}
117121

122+
/**
123+
* Creates a sorted map of entries.
124+
*
125+
* @param <K>
126+
* The key type.
127+
*
128+
* @param <V>
129+
* The value type.
130+
*
131+
* @param entries
132+
* The map entries.
133+
*
134+
* @return
135+
* A map containing the provided entries sorted by key.
136+
*/
137+
@SafeVarargs
138+
public static <K extends Comparable<K>, V> SortedMap<K, V> sortedMapOf(Map.Entry<K, V>... entries) {
139+
var map = new TreeMap<K, V>();
140+
141+
for (var entry : entries) {
142+
map.put(entry.getKey(), entry.getValue());
143+
}
144+
145+
return map;
146+
}
147+
118148
/**
119149
* Creates an immutable map entry.
120150
*
@@ -175,6 +205,27 @@ public static <E> Set<E> immutableSetOf(E... elements) {
175205
return java.util.Collections.unmodifiableSet(setOf(elements));
176206
}
177207

208+
/**
209+
* Creates a sorted set of elements.
210+
*
211+
* @param <E>
212+
* The element type.
213+
*
214+
* @param elements
215+
* The set elements.
216+
*
217+
* @return
218+
* A sorted set containing the provided elements.
219+
*/
220+
@SafeVarargs
221+
public static <E extends Comparable<E>> SortedSet<E> sortedSetOf(E... elements) {
222+
var set = new TreeSet<E>();
223+
224+
java.util.Collections.addAll(set, elements);
225+
226+
return set;
227+
}
228+
178229
/**
179230
* Returns an empty list.
180231
*

kilo-client/src/test/java/org/httprpc/kilo/util/CollectionsTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ public void testImmutableMapOf() {
8080
assertThrows(UnsupportedOperationException.class, () -> map.put("d", 4));
8181
}
8282

83+
@Test
84+
public void testSortedMapOf() {
85+
var sortedMap = sortedMapOf(
86+
entry("c", 3),
87+
entry("b", 2),
88+
entry("a", 1)
89+
);
90+
91+
assertEquals(listOf(1, 2, 3), new ArrayList<>(sortedMap.values()));
92+
}
93+
8394
@Test
8495
public void testSetOf() {
8596
var expected = new HashSet<Integer>(3);
@@ -104,6 +115,14 @@ public void testImmutableSetOf() {
104115
assertThrows(UnsupportedOperationException.class, () -> set.add(4));
105116
}
106117

118+
@Test
119+
public void testSortedSetOf() {
120+
var sortedSet = sortedSetOf(3, 2, 1);
121+
122+
assertEquals(listOf(1, 2, 3), new ArrayList<>(sortedSet));
123+
124+
}
125+
107126
@Test
108127
public void testEmptyListOf() {
109128
var list1 = java.util.Collections.<Integer>emptyList();

0 commit comments

Comments
 (0)