This commit is contained in:
Looly
2023-12-19 02:45:22 +08:00
parent 4b83504e50
commit 494c70b9e6
27 changed files with 796 additions and 1032 deletions

View File

@@ -383,20 +383,20 @@ public class Setting extends AbsSetting implements Map<String, String> {
}
/**
* 转换为Properties对象,原分组变为前缀
* 转换为{@link Props}对象,原分组变为前缀
*
* @return Properties对象
* @return {@link Props}对象
*/
public Properties toProperties() {
final Properties properties = new Properties();
public Props toProps() {
final Props props = new Props();
String group;
for (final Entry<String, LinkedHashMap<String, String>> groupEntry : this.groupedMap.entrySet()) {
group = groupEntry.getKey();
for (final Entry<String, String> entry : groupEntry.getValue().entrySet()) {
properties.setProperty(StrUtil.isEmpty(group) ? entry.getKey() : group + CharUtil.DOT + entry.getKey(), entry.getValue());
props.setProperty(StrUtil.isEmpty(group) ? entry.getKey() : group + CharUtil.DOT + entry.getKey(), entry.getValue());
}
}
return properties;
return props;
}
/**

View File

@@ -13,20 +13,19 @@
package org.dromara.hutool.setting.props;
import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.resource.Resource;
import org.dromara.hutool.core.io.resource.ResourceUtil;
import org.dromara.hutool.core.io.resource.UrlResource;
import org.dromara.hutool.core.io.watch.SimpleWatcher;
import org.dromara.hutool.core.io.watch.WatchMonitor;
import org.dromara.hutool.core.io.watch.WatchUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.func.LambdaInfo;
import org.dromara.hutool.core.func.LambdaUtil;
import org.dromara.hutool.core.func.SerFunction;
import org.dromara.hutool.core.func.SerSupplier;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.io.resource.Resource;
import org.dromara.hutool.core.io.resource.ResourceUtil;
import org.dromara.hutool.core.io.watch.SimpleWatcher;
import org.dromara.hutool.core.io.watch.WatchMonitor;
import org.dromara.hutool.core.io.watch.WatchUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.getter.TypeGetter;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.reflect.ConstructorUtil;
@@ -102,6 +101,16 @@ public final class Props extends Properties implements TypeGetter<CharSequence>
return new Props(resource, charset);
}
/**
* {@link Properties}转为Props
*
* @param properties {@link Properties}
* @return Props
*/
public static Props of(final Properties properties) {
return new Props(properties);
}
// ----------------------------------------------------------------------- 构造方法 start
/**
@@ -272,6 +281,37 @@ public final class Props extends Properties implements TypeGetter<CharSequence>
return (String) value;
}
/**
* 获取一个新的子属性,子属性键值对拥有公共前缀,以.分隔。
* <pre>
* a.b
* a.c
* b.a
* </pre>
* 则调用getSubProps("a");得到
* <pre>
* a.b
* a.c
* </pre>
*
* @param prefix 前缀,可以不以.结尾
* @return 子属性
*/
public Props getSubProps(final String prefix) {
final Props subProps = new Props();
final String finalPrefix = StrUtil.addSuffixIfNot(prefix, StrUtil.DOT);
final int prefixLength = finalPrefix.length();
forEach((key, value) -> {
final String keyStr = key.toString();
if (StrUtil.startWith(keyStr, finalPrefix)) {
subProps.set(StrUtil.subSuf(keyStr, prefixLength), value);
}
});
return subProps;
}
/**
* 转换为标准的{@link Properties}对象
*
@@ -325,7 +365,28 @@ public final class Props extends Properties implements TypeGetter<CharSequence>
*/
public <T> T toBean(final Class<T> beanClass, final String prefix) {
final T bean = ConstructorUtil.newInstanceIfPossible(beanClass);
return fillBean(bean, prefix);
return toBean(bean, prefix);
}
/**
* 将配置文件转换为Bean支持嵌套Bean<br>
* 支持的表达式:
*
* <pre>
* persion
* persion.name
* persons[3]
* person.friends[5].name
* ['person']['friends'][5]['name']
* </pre>
*
* @param <T> Bean类型
* @param bean Bean对象
* @return Bean对象
* @since 4.6.3
*/
public <T> T toBean(final T bean) {
return toBean(bean, null);
}
/**
@@ -346,7 +407,7 @@ public final class Props extends Properties implements TypeGetter<CharSequence>
* @return Bean对象
* @since 4.6.3
*/
public <T> T fillBean(final T bean, String prefix) {
public <T> T toBean(final T bean, String prefix) {
prefix = StrUtil.emptyIfNull(StrUtil.addSuffixIfNot(prefix, StrUtil.DOT));
String key;

View File

@@ -88,7 +88,7 @@ public class PropsTest {
Assertions.assertEquals(DateUtil.parse("2020-01-01"), systemConfig.getCreateTime());
Assertions.assertEquals(true, systemConfig.getIsInit());
Assertions.assertEquals("1", systemConfig.getStairPlan());
Assertions.assertEquals(new Integer(2), systemConfig.getStageNum());
Assertions.assertEquals(Integer.valueOf(2), systemConfig.getStageNum());
Assertions.assertEquals("3", systemConfig.getVersion());
}
@@ -124,5 +124,16 @@ public class PropsTest {
private Date nextStageTime;//当前阶段结束日期/下一阶段开始日期
}
@Test
void getSubTest() {
final Props props = new Props();
props.set("a.b", "1");
props.set("a.c", "2");
props.set("b.a", "3");
final Props subProps = props.getSubProps("a");
Assertions.assertEquals(2, subProps.size());
Assertions.assertEquals("1", subProps.getStr("b"));
Assertions.assertEquals("2", subProps.getStr("c"));
}
}