-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
thunderpay-lib: config-magic[config(DefaultCoercibles)]
- Loading branch information
1 parent
a2636a4
commit ae2b42b
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
thunderpay-lib/config-magic/src/main/java/org/thunderpay/config/DefaultCoercibles.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,106 @@ | ||
/** | ||
* @file DefaultCoercibles.java | ||
* @author Krisna Pranav | ||
* @brief Default Coercibles | ||
* @version 1.0 | ||
* @date 2024-11-25 | ||
* | ||
* @copyright Copyright (c) 2024 ThunderPayment Developers, Krisna Pranav | ||
* | ||
*/ | ||
|
||
package org.thunderpay.config; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.net.URI; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
final class DefaultCoercibles { | ||
public static final Coercible<?> CASE_INSENSITIVE_ENUM_COERCIBLE = new CaseInsensitiveEnumCoercible(); | ||
|
||
static final Coercible<?> VALUE_OF_COERCIBLE = new Coercible<Object>() { | ||
private final Map<Class<?>, Coercer<Object>> coercerMap = new HashMap<Class<?>, Coercer<Object>>(); | ||
|
||
public Coercer<Object> accept(final Class<?> type) { | ||
if (coercerMap.containsKey(type)) { | ||
return coercerMap.get(type); | ||
} | ||
|
||
Coercer<Object> coercer = null; | ||
|
||
try { | ||
Method candidate = type.getMethod("valueOf", String.class); | ||
|
||
if (!Modifier.isStatic(candidate.getModifiers())) { | ||
candidate = null; | ||
} else if (!candidate.getReturnType().isAssignableFrom(type)) { | ||
candidate = null; | ||
} | ||
|
||
if (candidate != null) { | ||
final Method valueOfMethod = candidate; | ||
|
||
coercer = new Coercer<Object>() { | ||
public Object coerce(final String value) { | ||
try { | ||
return value == null ? null : valueOfMethod.invoke(null, value); | ||
} catch (final Exception e) { | ||
throw convertException(e); | ||
} | ||
} | ||
}; | ||
} | ||
} catch(final NoSuchMethodException nsme) { | ||
} | ||
|
||
coercerMap.put(type, coercer); | ||
return coercer; | ||
} | ||
}; | ||
|
||
static final Coercible<?> STRING_CTOR_COERCIBLE = new Coercible<Object>() { | ||
private final Map<Class<?>, Coercer<Object>> coercerMap = new HashMap<Class<?>, Coercer<Object>>(); | ||
|
||
public Coercer<Object> accept(Class<?> type) { | ||
if (coercerMap.containsKey(type)) { | ||
return coercerMap.get(type); | ||
} | ||
|
||
Coercer<Object> coercer = null; | ||
|
||
try { | ||
final Constructor<?> ctor = type.getConstructor(String.class); | ||
|
||
coercer = new Coercer<Object>() { | ||
public Object coerce(final String value) { | ||
try { | ||
return value == null ? null : ctor.newInstance(value); | ||
} catch (final Exception e) { | ||
throw convertException(e); | ||
} | ||
} | ||
}; | ||
} catch (final NoSuchMethodException nsme) { } | ||
|
||
coercerMap.put(type, coercer); | ||
return coercer; | ||
} | ||
}; | ||
|
||
private DefaultCoercibles() {} | ||
|
||
public static final RuntimeException convertException(final Throwable t) { | ||
if (t instanceof RuntimeException) { | ||
return (RuntimeException) t; | ||
} else if (t instanceof InvocationTargetException) { | ||
return convertException(((InvocationTargetException) t).getTargetException()); | ||
} else { | ||
return new RuntimeException(t); | ||
} | ||
} | ||
} |