Skip to content

Commit

Permalink
[ISSUE #8622] 使用尾递归优化循环
Browse files Browse the repository at this point in the history
- SYS更名为ENV
- SHARED更改为PROTOTYPE
  • Loading branch information
onewe committed Sep 21, 2022
1 parent 6ec405b commit cd3765a
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

class JvmArgsPropertySource extends AbstractPropertySource {

private final Properties properties = new Properties(System.getProperties());
private final Properties properties;

JvmArgsPropertySource() {
this.properties = System.getProperties();
}

@Override
SourceType getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@SuppressWarnings("PMD.ClassNamingShouldBeCamelRule")
public interface NProperties {

NProperties SHARED = SearchableProperties.INSTANCE;
NProperties PROTOTYPE = SearchableProperties.INSTANCE;

/**
* get property, if the value can not be got by the special key, the null will be returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class PropertiesPropertySource extends AbstractPropertySource {

private final Properties properties = new Properties();

private PropertiesPropertySource parent;
private final PropertiesPropertySource parent;

PropertiesPropertySource(){}
PropertiesPropertySource() {
this.parent = null;
}

PropertiesPropertySource(PropertiesPropertySource parent) {
this.parent = parent;
Expand All @@ -40,51 +42,44 @@ SourceType getType() {

@Override
String getProperty(String key) {
return getProperty(this, key);
}

String value = properties.getProperty(key);
private String getProperty(PropertiesPropertySource propertiesPropertySource, String key) {
final String value = propertiesPropertySource.properties.getProperty(key);
if (value != null) {
return value;
}
PropertiesPropertySource parent = this.parent;
while (parent != null) {
value = parent.properties.getProperty(key);
if (value != null) {
return value;
}
parent = parent.parent;
final PropertiesPropertySource parent = propertiesPropertySource.parent;
if (parent == null) {
return null;
}

return null;
return getProperty(parent, key);
}

@Override
boolean containsKey(String key) {
boolean exist = properties.containsKey(key);
return containsKey(this, key);
}

boolean containsKey(PropertiesPropertySource propertiesPropertySource, String key) {
final boolean exist = propertiesPropertySource.properties.containsKey(key);
if (exist) {
return true;
}
PropertiesPropertySource parent = this.parent;
while (parent != null) {
exist = parent.properties.containsKey(key);
if (exist) {
return true;
}
parent = parent.parent;
final PropertiesPropertySource parent = propertiesPropertySource.parent;
if (parent == null) {
return false;
}
return false;
return containsKey(parent, key);
}

@Override
Properties asProperties() {
List<Properties> propertiesList = new ArrayList<>(8);
propertiesList.add(properties);

PropertiesPropertySource parent = this.parent;
while (parent != null) {
propertiesList.add(parent.properties);
parent = parent.parent;
}


propertiesList = lookingForProperties(this, propertiesList);

Properties ret = new Properties();
final ListIterator<Properties> iterator = propertiesList.listIterator(propertiesList.size());
while (iterator.hasPrevious()) {
Expand All @@ -94,6 +89,15 @@ Properties asProperties() {
return ret;
}

List<Properties> lookingForProperties(PropertiesPropertySource propertiesPropertySource, List<Properties> propertiesList) {
propertiesList.add(propertiesPropertySource.properties);
final PropertiesPropertySource parent = propertiesPropertySource.parent;
if (parent == null) {
return propertiesList;
}
return lookingForProperties(parent, propertiesList);
}

synchronized void setProperty(String key, String value) {
properties.setProperty(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class SearchableProperties implements NProperties {
private static final DefaultSettingPropertySource DEFAULT_SETTING_PROPERTY_SOURCE = new DefaultSettingPropertySource();

private static final List<SourceType> DEFAULT_ORDER = Arrays.asList(SourceType.PROPERTIES, SourceType.JVM,
SourceType.SYS);
SourceType.ENV, SourceType.DEFAULT_SETTING);

private static final CompositeConverter CONVERTER = new CompositeConverter();

Expand All @@ -64,10 +64,8 @@ private SearchableProperties() {

private SearchableProperties(PropertiesPropertySource propertiesPropertySource) {
this.propertiesPropertySource = propertiesPropertySource;
final List<AbstractPropertySource> sourceList = build(propertiesPropertySource,
JVM_ARGS_PROPERTY_SOURCE, SYSTEM_ENV_PROPERTY_SOURCE);
sourceList.add(DEFAULT_SETTING_PROPERTY_SOURCE);
this.propertySources = sourceList;
this.propertySources = build(propertiesPropertySource,
JVM_ARGS_PROPERTY_SOURCE, SYSTEM_ENV_PROPERTY_SOURCE, DEFAULT_SETTING_PROPERTY_SOURCE);
}

@Override
Expand Down Expand Up @@ -184,7 +182,7 @@ private List<AbstractPropertySource> build(AbstractPropertySource... propertySou
}
return sortPropertySource(sourceType, propertySources);
} catch (Exception e) {
LOGGER.error("first source type parse error, it will be used default order!");
LOGGER.error("first source type parse error, it will be used default order!", e);
return sortPropertySourceDefaultOrder(propertySources);
}
}
Expand All @@ -195,7 +193,7 @@ private List<AbstractPropertySource> sortPropertySourceDefaultOrder(
.collect(Collectors.toMap(AbstractPropertySource::getType, propertySource -> propertySource));
final List<AbstractPropertySource> collect = DEFAULT_ORDER.stream().map(sourceMap::get)
.collect(Collectors.toList());
LOGGER.info("properties search order:PROPERTIES->JVM->SYS->DEFAULT_SETTING");
LOGGER.info("properties search order:PROPERTIES->JVM->ENV->DEFAULT_SETTING");
return collect;
}

Expand All @@ -211,11 +209,13 @@ private List<AbstractPropertySource> sortPropertySource(SourceType firstType,
.stream().map(sourceMap::get).filter(Objects::nonNull).collect(Collectors.toList());

StringBuilder orderInfo = new StringBuilder("properties search order:");
for (AbstractPropertySource abstractPropertySource : collect) {
orderInfo.append(abstractPropertySource.getType().toString()).append("->");
for (int i = 0; i < collect.size(); i++) {
final AbstractPropertySource abstractPropertySource = collect.get(i);
orderInfo.append(abstractPropertySource.getType().toString());
if (i < collect.size() - 1) {
orderInfo.append("->");
}
}
orderInfo.append("DEFAULT_SETTING");

LOGGER.info(orderInfo.toString());

return collect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@

enum SourceType {
/**
* get value from system environment.
* get value from properties.
*/
SYS,
PROPERTIES,
/**
* get value from jvm args.
*/
JVM,
/**
* get value from properties.
* get value from system environment.
*/
PROPERTIES,
ENV,
/**
* get value from default setting.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SystemEnvPropertySource extends AbstractPropertySource {

@Override
SourceType getType() {
return SourceType.SYS;
return SourceType.ENV;
}

@Override
Expand Down
Loading

0 comments on commit cd3765a

Please sign in to comment.