Skip to content
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

Merged
merged 9 commits into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* This is an abstraction specially customized for the sequence Dubbo retrieves properties.
*/
public abstract class AbstractPrefixConfiguration extends AbstractConfiguration {
public abstract class AbstractPrefixConfiguration implements Configuration {
protected String id;
protected String prefix;

Expand All @@ -45,7 +45,10 @@ public Object getProperty(String key, Object defaultValue) {
value = getInternalProperty(prefix + key);
}
if (value == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified with

if (value == null) {
     value = getInternalProperty(key);
 }
 return value!=null? value:defaultValue;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

value = super.getProperty(key, defaultValue);
value = getInternalProperty(key);
if (value == null) {
value = defaultValue;
}
}
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
*
*/
public class CompositeConfiguration extends AbstractConfiguration {
public class CompositeConfiguration implements Configuration {

/**
* List holding all the configuration
Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the behaviour to print log.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be simplified with
return value!=null? value : defaultValue?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Expand All @@ -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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public EnvironmentConfiguration() {
}

@Override
protected Object getInternalProperty(String key) {
public Object getInternalProperty(String key) {
return System.getenv(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public InmemoryConfiguration() {
}

@Override
protected Object getInternalProperty(String key) {
public Object getInternalProperty(String key) {
return store.get(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public PropertiesConfiguration() {
}

@Override
protected Object getInternalProperty(String key) {
public Object getInternalProperty(String key) {
return ConfigUtils.getProperty(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public SystemConfiguration() {
}

@Override
protected Object getInternalProperty(String key) {
public Object getInternalProperty(String key) {
return System.getProperty(key);
}

Expand Down

This file was deleted.

Loading