Skip to content

Commit

Permalink
Update BeanAdapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
gk-brown committed Oct 12, 2024
1 parent 4691ab6 commit 6b50ef5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
17 changes: 10 additions & 7 deletions kilo-client/src/main/java/org/httprpc/kilo/beans/BeanAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -745,34 +745,37 @@ public static <T> T coerce(Object value, Class<T> type) {
* The target element type.
*
* @return
* A list containing the coerced elements.
* The coerced list.
*/
@SuppressWarnings("unchecked")
public static <E> List<E> coerceList(Collection<?> collection, Class<E> elementType) {
return (List<E>)toGenericType(collection, new ContainerType(new Type[] {elementType}, List.class));
}

/**
* Coerces map values.
* Coerces a map.
*
* @param <K>
* The key type.
* The target key type.
*
* @param <V>
* The target value type.
*
* @param map
* The map to coerce.
*
* @param keyType
* The target key type.
*
* @param valueType
* The target value type.
*
* @return
* A map containing the coerced values.
* The coerced map.
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> coerceMap(Map<K, ?> map, Class<V> valueType) {
return (Map<K, V>)toGenericType(map, new ContainerType(new Type[] {Object.class, valueType}, Map.class));
public static <K, V> Map<K, V> coerceMap(Map<?, ?> map, Class<K> keyType, Class<V> valueType) {
return (Map<K, V>)toGenericType(map, new ContainerType(new Type[] {keyType, valueType}, Map.class));
}

/**
Expand All @@ -788,7 +791,7 @@ public static <K, V> Map<K, V> coerceMap(Map<K, ?> map, Class<V> valueType) {
* The target element type.
*
* @return
* A set containing the coerced elements.
* The coerced set.
*/
@SuppressWarnings("unchecked")
public static <E> Set<E> coerceSet(Collection<?> collection, Class<E> elementType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,19 +333,19 @@ public void testMapCoercion() {
entry("a", "1.0"),
entry("b", "2.0"),
entry("c", "3.0")
), Double.class));
), String.class, Double.class));

assertEquals(mapOf(
entry(1, 1.0),
entry(2, 2.0),
entry(3, 3.0)
), BeanAdapter.coerceMap(mapOf(
entry(1, "1.0"),
entry(2, "2.0"),
entry(3, "3.0")
), Double.class));
entry("1", "1.0"),
entry("2", "2.0"),
entry("3", "3.0")
), Integer.class, Double.class));

assertNull(BeanAdapter.coerceMap(null, Object.class));
assertNull(BeanAdapter.coerceMap(null, Object.class, Object.class));

assertInstanceOf(Map.class, BeanAdapter.coerce(mapOf(), Map.class));
assertThrows(IllegalArgumentException.class, () -> BeanAdapter.coerce(123, Map.class));
Expand Down Expand Up @@ -400,7 +400,7 @@ public void testMutableMapCoercion() {
strings.put(3, "3.0");
strings.put(4, null);

var doubles = BeanAdapter.coerceMap(strings, Double.class);
var doubles = BeanAdapter.coerceMap(strings, Integer.class, Double.class);

assertEquals(mapOf(
entry(1, 1.0),
Expand Down

0 comments on commit 6b50ef5

Please sign in to comment.