Skip to content

Commit a0a4915

Browse files
committed
Update README.md.
1 parent 4335565 commit a0a4915

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Classes provided by the Kilo framework include:
2929
* [ElementAdapter](#elementadapter)
3030
* [Pipe](#pipe)
3131
* [Collections and Optionals](#collections-and-optionals)
32+
* [Streams](#streams)
3233

3334
Each is discussed in more detail below.
3435

@@ -790,7 +791,7 @@ try (var statement = queryBuilder.prepare(getConnection());
790791
The `ResultSetAdapter` type returned by `executeQuery()` provides access to the contents of a JDBC result set via the `Iterable` interface. Individual rows are represented by `Map` instances produced by the adapter's iterator. The results could be coerced to a list of `Pet` instances and returned to the caller, or used as the data dictionary for a template document:
791792

792793
```java
793-
return results.stream().map(result -> BeanAdapter.coerce(result, Pet.class)).toList();
794+
return results.stream().map(to(Pet.class)).toList();
794795
```
795796

796797
```java
@@ -990,7 +991,7 @@ var queryBuilder = QueryBuilder.select(Employee.class);
990991

991992
try (var statement = queryBuilder.prepare(getConnection());
992993
var results = queryBuilder.executeQuery(statement)) {
993-
return results.stream().map(result -> BeanAdapter.coerce(result, Employee.class)).toList();
994+
return results.stream().map(to(Employee.class)).toList();
994995
}
995996
```
996997

@@ -1006,7 +1007,7 @@ executorService.submit(() -> {
10061007

10071008
try (var statement = queryBuilder.prepare(connection);
10081009
var results = queryBuilder.executeQuery(statement)) {
1009-
pipe.submit(results.stream().map(result -> BeanAdapter.coerce(result, Employee.class)));
1010+
pipe.submit(results.stream().map(to(Employee.class)));
10101011
} catch (SQLException exception) {
10111012
throw new RuntimeException(exception);
10121013
}
@@ -1112,3 +1113,18 @@ var text = cast("abc", String.class); // abc
11121113

11131114
var number = cast("abc", Double.class); // null
11141115
```
1116+
1117+
## Streams
1118+
The `Streams` class defines a single method:
1119+
1120+
```java
1121+
public static <T> Function<Object, T> to(Class<T> type) { ... }
1122+
```
1123+
1124+
It returns a function that coerces a value to a given type and can be used to simplify stream processing code. For example:
1125+
1126+
```java
1127+
var strings = listOf("1", "2", "3");
1128+
1129+
var integers = strings.stream().map(to(Integer.class)).toList(); // 1, 2, 3
1130+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package org.httprpc.kilo.util.stream;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
import static org.httprpc.kilo.util.Collections.*;
20+
import static org.httprpc.kilo.util.stream.Streams.*;
21+
import static org.junit.jupiter.api.Assertions.*;
22+
23+
public class StreamsTest {
24+
@Test
25+
public void testTo() {
26+
var strings = listOf("1", "2", "3");
27+
28+
var integers = strings.stream().map(to(Integer.class)).toList(); // 1, 2, 3
29+
30+
assertEquals(listOf(1, 2, 3), integers);
31+
}
32+
}

0 commit comments

Comments
 (0)