Skip to content

Commit 4335565

Browse files
committed
Add Streams class.
1 parent cbe78c3 commit 4335565

File tree

8 files changed

+69
-25
lines changed

8 files changed

+69
-25
lines changed

kilo-client/src/main/java/org/httprpc/kilo/beans/BeanAdapter.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public BeanAdapter(Object bean) {
410410
var interfaces = type.getInterfaces();
411411

412412
if (interfaces.length == 0) {
413-
throw new UnsupportedOperationException("Type does not implement any interfaces.");
413+
throw new IllegalArgumentException("Type does not implement any interfaces.");
414414
}
415415

416416
type = interfaces[0];
@@ -683,8 +683,7 @@ public static <T> T coerce(Object value, Class<T> type) {
683683
* The coerced list.
684684
*
685685
* @deprecated
686-
* This method will be removed in a future release. Use streams and
687-
* {@link #coerce(Object, Class)} instead.
686+
* This method will be removed in a future release. Use streams instead.
688687
*/
689688
@Deprecated
690689
@SuppressWarnings("unchecked")
@@ -729,8 +728,7 @@ public Type getOwnerType() {
729728
* The coerced map.
730729
*
731730
* @deprecated
732-
* This method will be removed in a future release. Use streams and
733-
* {@link #coerce(Object, Class)} instead.
731+
* This method will be removed in a future release. Use streams instead.
734732
*/
735733
@Deprecated
736734
@SuppressWarnings("unchecked")
@@ -769,8 +767,7 @@ public Type getOwnerType() {
769767
* The coerced set.
770768
*
771769
* @deprecated
772-
* This method will be removed in a future release. Use streams and
773-
* {@link #coerce(Object, Class)} instead.
770+
* This method will be removed in a future release. Use streams instead.
774771
*/
775772
@Deprecated
776773
@SuppressWarnings("unchecked")
@@ -862,10 +859,10 @@ public static Object toGenericType(Object value, Type type) {
862859
throw new IllegalArgumentException("Value is not a collection.");
863860
}
864861
} else {
865-
throw new UnsupportedOperationException("Unsupported parameterized type.");
862+
throw new IllegalArgumentException();
866863
}
867864
} else {
868-
throw new UnsupportedOperationException("Unsupported type.");
865+
throw new IllegalArgumentException();
869866
}
870867
}
871868

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.httprpc.kilo.beans.BeanAdapter;
18+
19+
import java.util.function.Function;
20+
21+
/**
22+
* Provides static utility methods for working with streams.
23+
*/
24+
public class Streams {
25+
private Streams() {
26+
}
27+
28+
/**
29+
* Returns a function that coerces a value to a given type.
30+
*
31+
* @param <T>
32+
* The target type.
33+
*
34+
* @param type
35+
* The target type.
36+
*
37+
* @return
38+
* The coercion function.
39+
*/
40+
public static <T> Function<Object, T> to(Class<T> type) {
41+
return value -> BeanAdapter.coerce(value, type);
42+
}
43+
}

kilo-client/src/test/java/org/httprpc/kilo/sql/ResultSetAdapterTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Map;
3131

3232
import static org.httprpc.kilo.util.Collections.*;
33+
import static org.httprpc.kilo.util.stream.Streams.*;
3334
import static org.junit.jupiter.api.Assertions.*;
3435

3536
public class ResultSetAdapterTest {
@@ -74,7 +75,7 @@ private TemporalAccessorTest selectTemporalAccessorTest(int id) throws SQLExcept
7475
var results = queryBuilder.executeQuery(statement, mapOf(
7576
entry("id", id)
7677
))) {
77-
return results.stream().findFirst().map(result -> BeanAdapter.coerce(result, TemporalAccessorTest.class)).orElseThrow();
78+
return results.stream().findFirst().map(to(TemporalAccessorTest.class)).orElseThrow();
7879
}
7980
}
8081

@@ -151,7 +152,7 @@ private JSONTest selectJSONTest(int id) throws SQLException {
151152
var results = queryBuilder.executeQuery(statement, mapOf(
152153
entry("id", id)
153154
))) {
154-
return results.stream().findFirst().map(result -> BeanAdapter.coerce(result, JSONTest.class)).orElseThrow();
155+
return results.stream().findFirst().map(to(JSONTest.class)).orElseThrow();
155156
}
156157
}
157158

@@ -210,7 +211,7 @@ private XMLTest selectXMLTest(int id) throws SQLException {
210211
var results = queryBuilder.executeQuery(statement, mapOf(
211212
entry("id", id)
212213
))) {
213-
return results.stream().findFirst().map(result -> BeanAdapter.coerce(result, XMLTest.class)).orElseThrow();
214+
return results.stream().findFirst().map(to(XMLTest.class)).orElseThrow();
214215
}
215216
}
216217

kilo-test/src/main/java/org/httprpc/kilo/test/CatalogService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.List;
2828

2929
import static org.httprpc.kilo.util.Collections.*;
30+
import static org.httprpc.kilo.util.stream.Streams.*;
3031

3132
@WebServlet(urlPatterns = {"/catalog/*"}, loadOnStartup = 1)
3233
@Description("Catalog example service.")
@@ -39,7 +40,7 @@ public List<Item> getItems() throws SQLException {
3940

4041
try (var statement = queryBuilder.prepare(getConnection());
4142
var results = queryBuilder.executeQuery(statement)) {
42-
return results.stream().map(result -> BeanAdapter.coerce(result, Item.class)).toList();
43+
return results.stream().map(to(Item.class)).toList();
4344
}
4445
}
4546

@@ -55,7 +56,7 @@ public ItemDetail getItem(
5556
var results = queryBuilder.executeQuery(statement, mapOf(
5657
entry("itemID", itemID)
5758
))) {
58-
return results.stream().findFirst().map(result -> BeanAdapter.coerce(result, ItemDetail.class)).orElse(null);
59+
return results.stream().findFirst().map(to(ItemDetail.class)).orElse(null);
5960
}
6061
}
6162

kilo-test/src/main/java/org/httprpc/kilo/test/EmployeeService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.httprpc.kilo.RequestMethod;
2121
import org.httprpc.kilo.ResourcePath;
2222
import org.httprpc.kilo.WebService;
23-
import org.httprpc.kilo.beans.BeanAdapter;
2423
import org.httprpc.kilo.sql.QueryBuilder;
2524
import org.httprpc.kilo.util.concurrent.Pipe;
2625

@@ -29,6 +28,8 @@
2928
import java.util.concurrent.ExecutorService;
3029
import java.util.concurrent.Executors;
3130

31+
import static org.httprpc.kilo.util.stream.Streams.*;
32+
3233
@WebServlet(urlPatterns = {"/employees/*"}, loadOnStartup = 1)
3334
public class EmployeeService extends WebService {
3435
private static ExecutorService executorService = null;
@@ -58,7 +59,7 @@ public List<Employee> getEmployees() throws SQLException {
5859

5960
try (var statement = queryBuilder.prepare(getConnection());
6061
var results = queryBuilder.executeQuery(statement)) {
61-
return results.stream().map(result -> BeanAdapter.coerce(result, Employee.class)).toList();
62+
return results.stream().map(to(Employee.class)).toList();
6263
}
6364
}
6465

@@ -74,7 +75,7 @@ public Iterable<Employee> getEmployeesStream() {
7475

7576
try (var statement = queryBuilder.prepare(connection);
7677
var results = queryBuilder.executeQuery(statement)) {
77-
pipe.submit(results.stream().map(result -> BeanAdapter.coerce(result, Employee.class)));
78+
pipe.submit(results.stream().map(to(Employee.class)));
7879
} catch (SQLException exception) {
7980
throw new RuntimeException(exception);
8081
}

kilo-test/src/main/java/org/httprpc/kilo/test/FilmService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
import org.httprpc.kilo.RequestMethod;
2020
import org.httprpc.kilo.ResourcePath;
2121
import org.httprpc.kilo.WebService;
22-
import org.httprpc.kilo.beans.BeanAdapter;
2322
import org.httprpc.kilo.sql.QueryBuilder;
2423

2524
import java.sql.SQLException;
2625
import java.util.List;
2726

2827
import static org.httprpc.kilo.util.Collections.*;
2928
import static org.httprpc.kilo.util.Optionals.*;
29+
import static org.httprpc.kilo.util.stream.Streams.*;
3030

3131
@WebServlet(urlPatterns = {"/films/*"}, loadOnStartup = 1)
3232
@Description("Film example service.")
@@ -53,7 +53,7 @@ public List<Film> getFilms(
5353
var results = queryBuilder.executeQuery(statement, mapOf(
5454
entry("match", map(match, value -> value.replace('*', '%')))
5555
))) {
56-
return results.stream().map(result -> BeanAdapter.coerce(result, Film.class)).toList();
56+
return results.stream().map(to(Film.class)).toList();
5757
}
5858
}
5959

@@ -70,7 +70,7 @@ public FilmDetail getFilm(
7070
var results = queryBuilder.executeQuery(statement, mapOf(
7171
entry("filmID", filmID)
7272
))) {
73-
film = results.stream().findFirst().map(result -> BeanAdapter.coerce(result, FilmDetail.class)).orElseThrow();
73+
film = results.stream().findFirst().map(to(FilmDetail.class)).orElseThrow();
7474
}
7575

7676
film.setActors(getActors(filmID));
@@ -89,7 +89,7 @@ private List<Actor> getActors(Integer filmID) throws SQLException {
8989
var results = queryBuilder.executeQuery(statement, mapOf(
9090
entry("filmID", filmID)
9191
))) {
92-
return results.stream().map(result -> BeanAdapter.coerce(result, Actor.class)).toList();
92+
return results.stream().map(to(Actor.class)).toList();
9393
}
9494
}
9595

@@ -103,7 +103,7 @@ private List<Category> getCategories(Integer filmID) throws SQLException {
103103
var results = queryBuilder.executeQuery(statement, mapOf(
104104
entry("filmID", filmID)
105105
))) {
106-
return results.stream().map(result -> BeanAdapter.coerce(result, Category.class)).toList();
106+
return results.stream().map(to(Category.class)).toList();
107107
}
108108
}
109109
}

kilo-test/src/main/java/org/httprpc/kilo/test/PetService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.httprpc.kilo.RequestMethod;
1919
import org.httprpc.kilo.Required;
2020
import org.httprpc.kilo.ResourcePath;
21-
import org.httprpc.kilo.beans.BeanAdapter;
2221
import org.httprpc.kilo.io.CSVEncoder;
2322
import org.httprpc.kilo.io.JSONEncoder;
2423
import org.httprpc.kilo.io.TemplateEncoder;
@@ -30,6 +29,7 @@
3029
import java.util.ResourceBundle;
3130

3231
import static org.httprpc.kilo.util.Collections.*;
32+
import static org.httprpc.kilo.util.stream.Streams.*;
3333

3434
@WebServlet(urlPatterns = {"/pets/*"}, loadOnStartup = 1)
3535
public class PetService extends AbstractDatabaseService {
@@ -43,7 +43,7 @@ public List<Pet> getPets(@Required String owner) throws SQLException {
4343
var results = queryBuilder.executeQuery(statement, mapOf(
4444
entry("owner", owner)
4545
))) {
46-
return results.stream().map(result -> BeanAdapter.coerce(result, Pet.class)).toList();
46+
return results.stream().map(to(Pet.class)).toList();
4747
}
4848
}
4949

kilo-test/src/test/java/org/httprpc/kilo/test/WebServiceProxyTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.UUID;
4545

4646
import static org.httprpc.kilo.util.Collections.*;
47+
import static org.httprpc.kilo.util.stream.Streams.*;
4748
import static org.junit.jupiter.api.Assertions.*;
4849

4950
public class WebServiceProxyTest {
@@ -257,7 +258,7 @@ public void testListPost() throws IOException {
257258
webServiceProxy.setBody(body);
258259

259260
var result = ((List<?>)webServiceProxy.invoke()).stream()
260-
.map(element -> BeanAdapter.coerce(element, Integer.class))
261+
.map(to(Integer.class))
261262
.toList();
262263

263264
assertEquals(body, result);

0 commit comments

Comments
 (0)