Skip to content

Commit 79bb6bf

Browse files
committed
Update Collections and Iterables.
1 parent edf850f commit 79bb6bf

6 files changed

Lines changed: 46 additions & 98 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,8 +1006,6 @@ The `Collections` class provides a set of static utility methods for declarative
10061006
public static <E> List<E> listOf(E... elements) { ... }
10071007
public static <E> List<E> immutableListOf(E... elements) { ... }
10081008
public static <E> List<E> emptyListOf(Class<E> elementType) { ... }
1009-
1010-
public static <E extends Comparable<? super E>> List<E> sortedListOf(E... elements) { ... }
10111009
```
10121010

10131011
```java
@@ -1126,7 +1124,10 @@ public static <T, R> Iterable<R> mapAll(Iterable<T> iterable, Function<? super T
11261124
public static <T> Iterable<T> limit(Iterable<T> iterable, int count) { ... }
11271125

11281126
public static <T, R> List<R> flatten(Iterable<T> iterable, Function<? super T, ? extends Iterable<? extends R>> transform) { ... }
1129-
public static <T, R extends Comparable<? super R>> SortedSet<R> collect(Iterable<T> iterable, Function<? super T, ? extends Iterable<? extends R>> transform) { ... }
1127+
1128+
public static <T, V extends Comparable<? super V>> List<T> sortBy(Iterable<T> iterable, Function<? super T, ? extends V> identifier) { ... }
1129+
public static <T, V> List<T> sortBy(Iterable<T> iterable, Function<? super T, ? extends V> identifier, Comparator<? super V> comparator) { ... }
1130+
11301131
public static <T, K extends Comparable<? super K>> SortedMap<K, List<T>> groupBy(Iterable<T> iterable, Function<? super T, ? extends K> classifier) { ... }
11311132

11321133
public static <T> boolean exists(Iterable<T> iterable, Predicate<? super T> predicate) { ... }

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

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -158,47 +158,6 @@ public static <E> List<E> emptyListOf(Class<E> elementType) {
158158
return java.util.Collections.emptyList();
159159
}
160160

161-
/**
162-
* Creates a sorted list.
163-
*
164-
* @param <E>
165-
* The element type.
166-
*
167-
* @param elements
168-
* The list elements.
169-
*
170-
* @return
171-
* A sorted list of the provided elements.
172-
*/
173-
@SafeVarargs
174-
public static <E extends Comparable<? super E>> List<E> sortedListOf(E... elements) {
175-
var list = listOf(elements);
176-
177-
java.util.Collections.sort(list);
178-
179-
return list;
180-
}
181-
182-
/**
183-
* Creates a sorted list.
184-
*
185-
* @param <E>
186-
* The element type.
187-
*
188-
* @param elements
189-
* The list elements.
190-
*
191-
* @return
192-
* A sorted list of the provided elements.
193-
*/
194-
public static <E extends Comparable<? super E>> List<E> sortedListOf(Iterable<? extends E> elements) {
195-
List<E> list = listOf(elements);
196-
197-
java.util.Collections.sort(list);
198-
199-
return list;
200-
}
201-
202161
/**
203162
* Creates a map.
204163
*

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

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import java.util.List;
2222
import java.util.NoSuchElementException;
2323
import java.util.SortedMap;
24-
import java.util.SortedSet;
2524
import java.util.TreeMap;
26-
import java.util.TreeSet;
2725
import java.util.function.Function;
2826
import java.util.function.Predicate;
2927
import java.util.function.ToDoubleFunction;
@@ -235,37 +233,58 @@ public static <T, R> List<R> flatten(Iterable<T> iterable, Function<? super T, ?
235233
}
236234

237235
/**
238-
* Collects iterable contents.
236+
* Sorts iterable contents.
239237
*
240238
* @param <T>
241239
* The element type.
242240
*
243-
* @param <R>
244-
* The target type.
241+
* @param <V>
242+
* The value type.
245243
*
246244
* @param iterable
247-
* The iterable to collect.
245+
* The iterable to sort.
248246
*
249-
* @param transform
250-
* The transform function.
247+
* @param identifier
248+
* The identification function.
251249
*
252250
* @return
253-
* The collected contents.
251+
* The sorted contents.
254252
*/
255-
public static <T, R extends Comparable<? super R>> SortedSet<R> collect(Iterable<T> iterable, Function<? super T, ? extends Iterable<? extends R>> transform) {
256-
if (iterable == null || transform == null) {
257-
throw new IllegalArgumentException();
258-
}
253+
public static <T, V extends Comparable<? super V>> List<T> sortBy(Iterable<T> iterable, Function<? super T, ? extends V> identifier) {
254+
return sortBy(iterable, identifier, Comparator.naturalOrder());
255+
}
259256

260-
var set = new TreeSet<R>();
257+
/**
258+
* Sorts iterable contents.
259+
*
260+
* @param <T>
261+
* The element type.
262+
*
263+
* @param <V>
264+
* The value type.
265+
*
266+
* @param iterable
267+
* The iterable to sort.
268+
*
269+
* @param identifier
270+
* The identification function.
271+
*
272+
* @param comparator
273+
* The comparator.
274+
*
275+
* @return
276+
* The sorted contents.
277+
*/
278+
public static <T, V> List<T> sortBy(Iterable<T> iterable, Function<? super T, ? extends V> identifier, Comparator<? super V> comparator) {
279+
var list = new ArrayList<T>();
261280

262-
for (var elements : mapAll(iterable, transform)) {
263-
for (var element : elements) {
264-
set.add(element);
265-
}
281+
for (var element : iterable) {
282+
list.add(element);
266283
}
267284

268-
return set;
285+
list.sort((t1, t2) -> comparator.compare(identifier.apply(t1), identifier.apply(t2)));
286+
287+
return list;
269288
}
270289

271290
/**

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,6 @@ public void testEmptyListOf() {
7575
assertTrue(emptyListOf(Integer.class).isEmpty());
7676
}
7777

78-
@Test
79-
public void testSortedListOf() {
80-
var sortedList = sortedListOf(3, 2, 1);
81-
82-
assertEquals(listOf(1, 2, 3), sortedList);
83-
}
84-
85-
@Test
86-
public void testSortedListOfIterable() {
87-
var sortedList = sortedListOf(listOf(3, 2, 1));
88-
89-
assertEquals(listOf(1, 2, 3), sortedList);
90-
}
91-
9278
@Test
9379
public void testMapOf() {
9480
var expected = new HashMap<String, Integer>();

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.TreeMap;
24-
import java.util.TreeSet;
2524
import java.util.function.Supplier;
2625
import java.util.stream.Collectors;
2726

@@ -111,19 +110,8 @@ public void testFlatten() {
111110
}
112111

113112
@Test
114-
public void testCollect() {
115-
List<Supplier<List<Integer>>> values = listOf(
116-
() -> listOf(1),
117-
() -> listOf(1, 2),
118-
() -> listOf(1, 2, 3)
119-
);
120-
121-
var result = collect(values, Supplier::get); // 1, 2, 3
122-
123-
assertEquals(sortedSetOf(1, 2, 3), result);
124-
125-
assertEquals(result, values.stream().flatMap(value -> value.get().stream())
126-
.collect(Collectors.toCollection(TreeSet::new)));
113+
public void testSortBy() {
114+
// TODO
127115
}
128116

129117
@Test

kilo-server/src/main/java/org/httprpc/kilo/WebService.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public abstract class WebService extends HttpServlet {
8080
/**
8181
* Describes a service instance.
8282
*/
83-
public static class ServiceDescriptor implements Comparable<ServiceDescriptor> {
83+
public static class ServiceDescriptor {
8484
private String path;
8585
private String description;
8686

@@ -144,11 +144,6 @@ public List<EnumerationDescriptor> getEnumerations() {
144144
public List<StructureDescriptor> getStructures() {
145145
return new ArrayList<>(structures.values());
146146
}
147-
148-
@Override
149-
public int compareTo(ServiceDescriptor serviceDescriptor) {
150-
return path.compareTo(serviceDescriptor.path);
151-
}
152147
}
153148

154149
/**
@@ -820,7 +815,7 @@ public static synchronized <T extends WebService> T getInstance(Class<T> type) {
820815
* A list of active service descriptors.
821816
*/
822817
public static synchronized List<ServiceDescriptor> getServiceDescriptors() {
823-
return sortedListOf(mapAll(instances.values(), WebService::getServiceDescriptor));
818+
return sortBy(mapAll(instances.values(), WebService::getServiceDescriptor), ServiceDescriptor::getPath);
824819
}
825820

826821
@Override

0 commit comments

Comments
 (0)