-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial thoughts on how to allow type conversion to be extensible
- Loading branch information
1 parent
db25820
commit 60fc665
Showing
2 changed files
with
72 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/ElideConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2017, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
|
||
package com.yahoo.elide.utils.coerce.converters; | ||
|
||
import org.apache.commons.beanutils.ConvertUtilsBean; | ||
import org.apache.commons.beanutils.Converter; | ||
import org.apache.commons.lang3.ClassUtils; | ||
|
||
import java.util.Date; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.function.BiFunction; | ||
|
||
/** | ||
* A class that knows how to convert thing to other (different) things. | ||
*/ | ||
public class ElideConverter extends ConvertUtilsBean { | ||
private static final ToEnumConverter TO_ENUM_CONVERTER = new ToEnumConverter(); | ||
private static final FromMapConverter FROM_MAP_CONVERTER = new FromMapConverter(); | ||
private static final EpochToDateConverter EPOCH_TO_DATE_CONVERTER = new EpochToDateConverter(); | ||
|
||
private LinkedHashMap<BiFunction<Class<?>, Class<?>, Boolean>, Converter> converters; | ||
|
||
/** | ||
* Create a new ElideConverter with a set of converters. | ||
* | ||
* @param converters extra converters to consider before the default converters fire | ||
*/ | ||
public ElideConverter(LinkedHashMap<BiFunction<Class<?>, Class<?>, Boolean>, Converter> converters) { | ||
// yahoo/elide#260 - enable throwing exceptions when conversion fails | ||
register(true, false, 0); | ||
this.converters = converters; | ||
} | ||
|
||
/** | ||
* Create a new ElideConverter with the default set of converters. | ||
*/ | ||
public ElideConverter() { | ||
this(defaultConverters()); | ||
} | ||
|
||
@Override | ||
/* | ||
* Overriding lookup to execute enum converter if target is enum | ||
* or map convert if source is map | ||
*/ | ||
public Converter lookup(Class<?> sourceType, Class<?> targetType) { | ||
for (Map.Entry<BiFunction<Class<?>, Class<?>, Boolean>, Converter> entry : converters.entrySet()) { | ||
if (entry.getKey().apply(sourceType, targetType)) { | ||
return entry.getValue(); | ||
} | ||
} | ||
return super.lookup(sourceType, targetType); | ||
} | ||
|
||
public static LinkedHashMap<BiFunction<Class<?>, Class<?>, Boolean>, Converter> defaultConverters() { | ||
LinkedHashMap<BiFunction<Class<?>, Class<?>, Boolean>, Converter> converters = new LinkedHashMap<>(); | ||
converters.put((source, target) -> target.isEnum(), TO_ENUM_CONVERTER); | ||
converters.put((source, target) -> Map.class.isAssignableFrom(source), FROM_MAP_CONVERTER); | ||
converters.put( | ||
(source, target) -> (String.class.isAssignableFrom(source) || Number.class.isAssignableFrom(source)) | ||
&& ClassUtils.isAssignable(target, Date.class), | ||
EPOCH_TO_DATE_CONVERTER); | ||
return converters; | ||
} | ||
} |