This repository has been archived by the owner on Nov 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes for release 2.5.0 are merged from develop into master branch
- Loading branch information
Showing
97 changed files
with
4,942 additions
and
574 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
language: java | ||
dist: precise | ||
jdk: | ||
- oraclejdk7 | ||
script: mvn install | ||
sudo: false | ||
sudo: false |
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
119 changes: 119 additions & 0 deletions
119
modules/basic/src/main/java/io/oasp/module/basic/common/api/config/ConfigProperties.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,119 @@ | ||
package io.oasp.module.basic.common.api.config; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Simple abstraction interface for generic access to configuration properties (from spring-boot | ||
* {@code application.properties}). | ||
* | ||
* @since 3.0.0 | ||
*/ | ||
public interface ConfigProperties { | ||
|
||
/** The separator charactor '.' for hierarchical keys such as "spring.datasource.url". */ | ||
char KEY_SEPARATOR = '.'; | ||
|
||
/** An immutable instance of {@link ConfigProperties} that is always {@link #isEmpty() empty} */ | ||
ConfigProperties EMPTY = new EmptyConfigProperties(); | ||
|
||
/** | ||
* @return the {@link Set} of the {@link #getChild(String) direct child keys} available in the | ||
* {@link ConfigProperties}-node. | ||
*/ | ||
Set<String> getChildKeys(); | ||
|
||
/** | ||
* @param key the {@link #getChildKeys() child key} of the requested configuration value. | ||
* @return the child {@link ConfigProperties}. Will be an {@link #isEmpty() empty} child if undefined. | ||
*/ | ||
ConfigProperties getChild(String key); | ||
|
||
/** | ||
* Recursive variant of {@link #getChild(String)} such that | ||
* <code>{@link ConfigProperties config}.{@link #getChild(String...) getChild}(key1, ..., keyN)</code> is the same as | ||
* <code>{@link ConfigProperties config}.{@link #getChild(String) getChild}(key1)...{@link #getChild(String) getChild}(keyN)</code>. | ||
* | ||
* @param keys the keys to traverse recursively. | ||
* @return the descendant {@link #getChild(String) child} reached from recursively traversing the given {@code keys}. | ||
*/ | ||
ConfigProperties getChild(String... keys); | ||
|
||
/** | ||
* Shortcut for {@link #getChild(String) getChild(key)}.{@link #getValue()}. | ||
* | ||
* @param key the {@link #getChild(String) key of the child} | ||
* @return the value of this {@link ConfigProperties}-node. May be {@code null}. | ||
*/ | ||
String getChildValue(String key); | ||
|
||
/** | ||
* Shortcut for {@link #getChild(String...) getChild(keys)}.{@link #getValue()}. | ||
* | ||
* @param keys the keys to traverse recursively. | ||
* @return the {@link #getValue() value} of the {@link #getChild(String...) descendant child} traversed by | ||
* {@code keys}. May be {@code null}. | ||
*/ | ||
String getChildValue(String... keys); | ||
|
||
/** | ||
* @return the value of this {@link ConfigProperties}-node. May be {@code null}. | ||
*/ | ||
String getValue(); | ||
|
||
/** | ||
* @param <T> the requested {@code type} | ||
* @param type the {@link Class} reflecting the requested result type. | ||
* @return the value of this {@link ConfigProperties}-node converted to the given {@code type}. Will be {@code null} | ||
* if undefined. | ||
*/ | ||
<T> T getValue(Class<T> type); | ||
|
||
/** | ||
* @param <T> the requested {@code type} | ||
* @param type the {@link Class} reflecting the requested result type. | ||
* @param defaultValue the value returned as default if the actual {@link #getValue() value} is undefined. | ||
* @return the value of this {@link ConfigProperties}-node converted to the given {@code type}. Will be | ||
* {@code defaultValue} if undefined. | ||
*/ | ||
<T> T getValue(Class<T> type, T defaultValue); | ||
|
||
/** | ||
* @return the {@link #getValue(Class, Object)} as {@code boolean} with {@code false} as default. | ||
*/ | ||
boolean getValueAsBoolean(); | ||
|
||
/** | ||
* @return {@code true} if this is an empty {@link ConfigProperties}-node that neither has a {@link #getValue() value} | ||
* nor {@link #getChildKeys() any} {@link #getChild(String) child}. | ||
*/ | ||
boolean isEmpty(); | ||
|
||
/** | ||
* @return this {@link ConfigProperties} converted to a {@link ConfigProperties#toFlatMap() flat} {@link Map}. | ||
*/ | ||
Map<String, String> toFlatMap(); | ||
|
||
/** | ||
* @param rootKey the root key used as prefix for the {@link java.util.Map.Entry#getKey() keys} separated with a dot | ||
* if not {@link String#isEmpty() empty}. Typically the empty {@link String}. | ||
* @return this {@link ConfigProperties} converted to a {@link ConfigProperties#toFlatMap() flat} {@link Map}. | ||
*/ | ||
Map<String, String> toFlatMap(String rootKey); | ||
|
||
/** | ||
* @return this {@link ConfigProperties} converted to a {@link ConfigProperties#toHierarchicalMap() hierarchical} | ||
* {@link Map}. | ||
*/ | ||
Map<String, Object> toHierarchicalMap(); | ||
|
||
/** | ||
* @param parent the parent {@link ConfigProperties} to extend. | ||
* @return a new instance of {@link ConfigProperties} with all {@link #getChild(String) children} and | ||
* {@link #getValue() value}(s) from this {@link ConfigProperties}-tree and all {@link #getChild(String) | ||
* children} and {@link #getValue() value}(s) inherited from the given {@code parent} | ||
* {@link ConfigProperties}-tree if they are undefined in this tree. | ||
*/ | ||
MutableConfigProperties inherit(ConfigProperties parent); | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
modules/basic/src/main/java/io/oasp/module/basic/common/api/config/ConfigValueUtil.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,72 @@ | ||
package io.oasp.module.basic.common.api.config; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.util.Currency; | ||
|
||
/** | ||
* Utility that helps to deal with configuration values. | ||
*/ | ||
public final class ConfigValueUtil { | ||
|
||
private ConfigValueUtil() { | ||
} | ||
|
||
/** | ||
* @param <T> the generic {@code type}. | ||
* @param object the {@link Object} to convert. Will not be {@code null}. | ||
* @param type the {@link Class} reflecting the requested type. | ||
* @return the given {@link Object} converted to the given {@code type}. | ||
*/ | ||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public static <T> T convertValue(Object object, Class<T> type) { | ||
|
||
try { | ||
Object result; | ||
if (type.isInstance(object)) { | ||
result = object; | ||
} else if (type.isEnum()) { | ||
result = Enum.valueOf((Class<? extends Enum>) type, object.toString()); | ||
} else if (type.isAssignableFrom(String.class)) { | ||
result = object.toString(); | ||
} else if ((type == boolean.class) || (type == Boolean.class)) { | ||
result = Boolean.valueOf(object.toString()); | ||
} else if ((type == int.class) || (type == Integer.class)) { | ||
result = Integer.valueOf(object.toString()); | ||
} else if ((type == long.class) || (type == Long.class)) { | ||
result = Long.valueOf(object.toString()); | ||
} else if ((type == double.class) || (type == Double.class)) { | ||
result = Double.valueOf(object.toString()); | ||
} else if (type == Class.class) { | ||
result = Class.forName(object.toString()); | ||
} else if ((type == float.class) || (type == Float.class)) { | ||
result = Float.valueOf(object.toString()); | ||
} else if ((type == short.class) || (type == Short.class)) { | ||
result = Short.valueOf(object.toString()); | ||
} else if ((type == byte.class) || (type == Byte.class)) { | ||
result = Byte.valueOf(object.toString()); | ||
} else if (type == BigDecimal.class) { | ||
result = new BigDecimal(object.toString()); | ||
} else if (type == BigInteger.class) { | ||
result = new BigInteger(object.toString()); | ||
} else if (type == Number.class) { | ||
result = Double.parseDouble(object.toString()); | ||
} else if ((type == Character.class) || ((type == char.class))) { | ||
String value = object.toString(); | ||
if (value.length() == 1) { | ||
result = Character.valueOf(value.charAt(0)); | ||
} else { | ||
throw new IllegalArgumentException(value); | ||
} | ||
} else if (type == Currency.class) { | ||
result = Currency.getInstance(object.toString()); | ||
} else { | ||
throw new IllegalArgumentException(object.toString()); | ||
} | ||
return (T) result; | ||
} catch (NumberFormatException | ClassNotFoundException e) { | ||
throw new IllegalArgumentException(object.toString(), e); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.