-
Notifications
You must be signed in to change notification settings - Fork 26.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code review config center #3096
Changes from 8 commits
4b50a23
4464731
9d40d74
1141dfb
dcfe092
e16df51
427f8a6
e661d42
cd15713
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
/** | ||
* | ||
*/ | ||
public class CompositeConfiguration extends AbstractConfiguration { | ||
public class CompositeConfiguration implements Configuration { | ||
|
||
/** | ||
* List holding all the configuration | ||
|
@@ -56,7 +56,7 @@ public void addConfiguration(int pos, Configuration configuration) { | |
} | ||
|
||
@Override | ||
protected Object getInternalProperty(String key) { | ||
public Object getInternalProperty(String key) { | ||
Configuration firstMatchingConfiguration = null; | ||
for (Configuration config : configList) { | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it better to do a non-empty check and remove the try block? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed the behaviour to print log. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,9 @@ public interface Configuration { | |
* @param key The configuration key. | ||
* @return The associated string. | ||
*/ | ||
String getString(String key); | ||
default String getString(String key) { | ||
return convert(String.class, key, null); | ||
} | ||
|
||
/** | ||
* Get a string associated with the given configuration key. | ||
|
@@ -38,7 +40,9 @@ public interface Configuration { | |
* @return The associated string if key is found and has valid | ||
* format, default value otherwise. | ||
*/ | ||
String getString(String key, String defaultValue); | ||
default String getString(String key, String defaultValue) { | ||
return convert(String.class, key, defaultValue); | ||
} | ||
|
||
/** | ||
* Gets a property from the configuration. This is the most basic get | ||
|
@@ -56,7 +60,9 @@ public interface Configuration { | |
* @return the value to which this configuration maps the specified key, or | ||
* null if the configuration contains no mapping for this key. | ||
*/ | ||
Object getProperty(String key); | ||
default Object getProperty(String key) { | ||
return getProperty(key, null); | ||
} | ||
|
||
/** | ||
* Gets a property from the configuration. The default value will return if the configuration doesn't contain | ||
|
@@ -67,7 +73,15 @@ public interface Configuration { | |
* @return the value to which this configuration maps the specified key, or default value if the configuration | ||
* contains no mapping for this key. | ||
*/ | ||
Object getProperty(String key, Object defaultValue); | ||
default Object getProperty(String key, Object defaultValue) { | ||
Object value = getInternalProperty(key); | ||
if (value == null) { | ||
value = defaultValue; | ||
} | ||
return value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be simplified with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
Object getInternalProperty(String key); | ||
|
||
/** | ||
* Check if the configuration contains the specified key. | ||
|
@@ -76,7 +90,50 @@ public interface Configuration { | |
* @return {@code true} if the configuration contains a value for this | ||
* key, {@code false} otherwise | ||
*/ | ||
boolean containsKey(String key); | ||
default boolean containsKey(String key) { | ||
return getProperty(key) != null; | ||
} | ||
|
||
|
||
default <T> T convert(Class<T> cls, String key, T defaultValue) { | ||
// we only process String properties for now | ||
String value = (String) getProperty(key); | ||
|
||
if (value == null) { | ||
return defaultValue; | ||
} | ||
|
||
Object obj = value; | ||
if (cls.isInstance(value)) { | ||
return cls.cast(value); | ||
} | ||
|
||
if (String.class.equals(cls)) { | ||
return cls.cast(value); | ||
} | ||
|
||
if (Boolean.class.equals(cls) || Boolean.TYPE.equals(cls)) { | ||
obj = Boolean.valueOf(value); | ||
} else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive()) { | ||
if (Integer.class.equals(cls) || Integer.TYPE.equals(cls)) { | ||
obj = Integer.valueOf(value); | ||
} else if (Long.class.equals(cls) || Long.TYPE.equals(cls)) { | ||
obj = Long.valueOf(value); | ||
} else if (Byte.class.equals(cls) || Byte.TYPE.equals(cls)) { | ||
obj = Byte.valueOf(value); | ||
} else if (Short.class.equals(cls) || Short.TYPE.equals(cls)) { | ||
obj = Short.valueOf(value); | ||
} else if (Float.class.equals(cls) || Float.TYPE.equals(cls)) { | ||
obj = Float.valueOf(value); | ||
} else if (Double.class.equals(cls) || Double.TYPE.equals(cls)) { | ||
obj = Double.valueOf(value); | ||
} | ||
} else if (cls.isEnum()) { | ||
obj = Enum.valueOf(cls.asSubclass(Enum.class), value); | ||
} | ||
|
||
return cls.cast(obj); | ||
} | ||
|
||
|
||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be simplified with
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done