format: 规范化代码风格

This commit is contained in:
2024-05-21 03:13:30 +08:00
parent c0ebee6976
commit 1c00489cb7
24 changed files with 64 additions and 64 deletions

View File

@@ -14,16 +14,16 @@ import org.slf4j.MDC;
*/ */
public final class BootStrap { public final class BootStrap {
public static void main(String[] args){ public static void main(String[] args) {
beforeInit(); beforeInit();
init(); init();
} }
private static void beforeInit(){ private static void beforeInit() {
MDC.put("pid", SystemSupport.getPid()); MDC.put("pid", SystemSupport.getPid());
} }
private static void init(){ private static void init() {
SystemInitializer systemInitializer = SystemInitializer SystemInitializer systemInitializer = SystemInitializer
.configurer() .configurer()
.configuration(new PropertiesConfiguration(SystemConstants.USER_SETTINGS_PROPERTIES_PATH)) .configuration(new PropertiesConfiguration(SystemConstants.USER_SETTINGS_PROPERTIES_PATH))

View File

@@ -81,7 +81,7 @@ public abstract class AbstractConfiguration implements Configuration {
// 刷新配置信息 // 刷新配置信息
refresh0(); refresh0();
final Boolean needPrint = getBoolean(ConfigurationKeys.KEY_CFG_LOG_ONSTART); final Boolean needPrint = getBoolean(ConfigurationKeys.KEY_CFG_LOG_ONSTART);
if(needPrint) if (needPrint)
printDetails(); printDetails();
} }
@@ -106,7 +106,7 @@ public abstract class AbstractConfiguration implements Configuration {
/** /**
* 载入配置信息请加锁 * 载入配置信息请加锁
*/ */
protected void load(){ protected void load() {
try { try {
loadLock.lock(); loadLock.lock();
// 清空原有的配置信息 // 清空原有的配置信息

View File

@@ -16,18 +16,18 @@ public final class InstanceClasses {
private InstanceClasses(){throw new UnsupportedOperationException();} private InstanceClasses(){throw new UnsupportedOperationException();}
private static final Map<InstanceType, Class<? extends Instance>> instanceTypeMap = private static final Map<InstanceType, Class<? extends Instance>> instanceTypeMap =
new HashMap<InstanceType, Class<? extends Instance>>(){ new HashMap<InstanceType, Class<? extends Instance>>() {
{ {
put(InstanceType.ALI_YUN, AliyunInstance.class); put(InstanceType.ALI_YUN, AliyunInstance.class);
put(InstanceType.TENCENT_CLOUD, TencentInstance.class); put(InstanceType.TENCENT_CLOUD, TencentInstance.class);
} }
}; };
public static Class<? extends Instance> match(InstanceType type){ public static Class<? extends Instance> match(InstanceType type) {
return instanceTypeMap.get(type); return instanceTypeMap.get(type);
} }
public static Class<? extends Instance> match(String type){ public static Class<? extends Instance> match(String type) {
return instanceTypeMap.get(InstanceType.valueOf(type)); return instanceTypeMap.get(InstanceType.valueOf(type));
} }
} }

View File

@@ -19,10 +19,10 @@ public final class InstanceFileFilter implements FileFilter {
@Override @Override
public boolean accept(File pathname) { public boolean accept(File pathname) {
if(!pathname.isFile()) if (!pathname.isFile())
return false; return false;
for (String suffix : fileSuffix) { for (String suffix : fileSuffix) {
if(pathname.getName().endsWith(suffix)){ if (pathname.getName().endsWith(suffix)) {
return true; return true;
} }
} }

View File

@@ -39,7 +39,7 @@ public abstract class AbstractInstanceContext implements InstanceContext, Multip
@Override @Override
public void refresh() { public void refresh() {
if(listableInstanceFactories.isEmpty()) if (listableInstanceFactories.isEmpty())
return; return;
// 初始化所有实例工厂 // 初始化所有实例工厂
@@ -68,7 +68,7 @@ public abstract class AbstractInstanceContext implements InstanceContext, Multip
public boolean addInstance(Instance instance, boolean override) { public boolean addInstance(Instance instance, boolean override) {
validateInstance(instance); validateInstance(instance);
Instance i = instanceMap.get(instance.getName()); Instance i = instanceMap.get(instance.getName());
if(override && i != null){ if (override && i != null) {
return false; return false;
} }
instanceMap.put(instance.getName(), instance); instanceMap.put(instance.getName(), instance);
@@ -120,8 +120,8 @@ public abstract class AbstractInstanceContext implements InstanceContext, Multip
/** /**
* 缓存清理 * 缓存清理
*/ */
protected void clearCache(){ protected void clearCache() {
if(cacheInstanceMap != null if (cacheInstanceMap != null
&& !cacheInstanceMap.isEmpty()){ && !cacheInstanceMap.isEmpty()){
int size = cacheInstanceMap.size(); int size = cacheInstanceMap.size();
cacheInstanceMap.clear(); cacheInstanceMap.clear();
@@ -136,15 +136,15 @@ public abstract class AbstractInstanceContext implements InstanceContext, Multip
* @param instances 实例信息 * @param instances 实例信息
* @return 属性设置完整的实例 * @return 属性设置完整的实例
*/ */
private Set<Instance> buildInstances(Collection<Instance> instances){ private Set<Instance> buildInstances(Collection<Instance> instances) {
//设置实例信息, 如果需要从父类继承 //设置实例信息, 如果需要从父类继承
return instances.stream() return instances.stream()
.filter(i -> !InstanceType.INHERITED.equals(i.getType())) .filter(i -> !InstanceType.INHERITED.equals(i.getType()))
.peek(i -> { .peek(i -> {
String fatherName = i.getFatherName(); String fatherName = i.getFatherName();
if(fatherName != null && !fatherName.isEmpty()){ if (fatherName != null && !fatherName.isEmpty()) {
Instance fatherInstance = cacheInstanceMap.get(fatherName); Instance fatherInstance = cacheInstanceMap.get(fatherName);
if(fatherInstance != null){ if (fatherInstance != null) {
try { try {
ReflectionUtils.copyField(fatherInstance, i, true); ReflectionUtils.copyField(fatherInstance, i, true);
} catch (Exception e) { } catch (Exception e) {

View File

@@ -50,7 +50,7 @@ public abstract class AbstractInstanceFactory implements InstanceFactory, Listab
public boolean addInstance(Instance instance, boolean override) { public boolean addInstance(Instance instance, boolean override) {
validateInstance(instance); validateInstance(instance);
Instance i = instanceMap.get(instance.getName()); Instance i = instanceMap.get(instance.getName());
if(override && i != null){ if (override && i != null) {
return false; return false;
} }
instanceMap.put(instance.getName(), instance); instanceMap.put(instance.getName(), instance);
@@ -65,7 +65,7 @@ public abstract class AbstractInstanceFactory implements InstanceFactory, Listab
@Override @Override
public void refresh() { public void refresh() {
Set<Instance> instances = load(); Set<Instance> instances = load();
if(instances != null && !instances.isEmpty()) if (instances != null && !instances.isEmpty())
instanceMap = new HashMap<>(instances.stream() instanceMap = new HashMap<>(instances.stream()
.collect(Collectors.toMap(Instance::getName, i -> i))); .collect(Collectors.toMap(Instance::getName, i -> i)));
} }
@@ -77,7 +77,7 @@ public abstract class AbstractInstanceFactory implements InstanceFactory, Listab
@Override @Override
public void afterRefresh() { public void afterRefresh() {
if(instanceMap != null if (instanceMap != null
&& !instanceMap.isEmpty()){ && !instanceMap.isEmpty()){
int size = instanceMap.size(); int size = instanceMap.size();
instanceMap.clear(); instanceMap.clear();

View File

@@ -10,7 +10,7 @@ import java.util.Set;
* @author SerLiunx * @author SerLiunx
* @since 1.0 * @since 1.0
*/ */
public abstract class DatabaseInstanceFactory extends AbstractInstanceFactory{ public abstract class DatabaseInstanceFactory extends AbstractInstanceFactory {
@Override @Override
protected Set<Instance> load() { protected Set<Instance> load() {

View File

@@ -25,7 +25,7 @@ public abstract class FileInstanceFactory extends AbstractInstanceFactory {
@Override @Override
protected Set<Instance> load() { protected Set<Instance> load() {
Set<File> files = loadFiles(); Set<File> files = loadFiles();
if(files != null && !files.isEmpty()){ if (files != null && !files.isEmpty()) {
return files.stream() return files.stream()
.map(this::loadInstance) .map(this::loadInstance)
.filter(Objects::nonNull) .filter(Objects::nonNull)
@@ -56,19 +56,19 @@ public abstract class FileInstanceFactory extends AbstractInstanceFactory {
/** /**
* 载入目录下所有符合条件的文件 * 载入目录下所有符合条件的文件
*/ */
private Set<File> loadFiles(){ private Set<File> loadFiles() {
File pathFile = new File(instanceDir); File pathFile = new File(instanceDir);
if(!pathFile.exists()){ if (!pathFile.exists()) {
boolean result = pathFile.mkdirs(); boolean result = pathFile.mkdirs();
if(!result){ if (!result) {
throw new IllegalArgumentException("create path failed"); throw new IllegalArgumentException("create path failed");
} }
} }
if(!pathFile.isDirectory()){ if (!pathFile.isDirectory()) {
throw new IllegalArgumentException("path is not a directory"); throw new IllegalArgumentException("path is not a directory");
} }
File[] files = pathFile.listFiles(new InstanceFileFilter(fileSuffix())); File[] files = pathFile.listFiles(new InstanceFileFilter(fileSuffix()));
if(files == null || files.length == 0){ if (files == null || files.length == 0) {
return Collections.emptySet(); return Collections.emptySet();
} }
return Arrays.stream(files).collect(Collectors.toCollection(HashSet::new)); return Arrays.stream(files).collect(Collectors.toCollection(HashSet::new));

View File

@@ -18,7 +18,7 @@ import static com.serliunx.ddns.constant.InstanceClasses.match;
* @see com.fasterxml.jackson.dataformat.xml.XmlMapper * @see com.fasterxml.jackson.dataformat.xml.XmlMapper
* @see com.fasterxml.jackson.databind.json.JsonMapper * @see com.fasterxml.jackson.databind.json.JsonMapper
*/ */
public abstract class JacksonFileInstanceFactory extends FileInstanceFactory{ public abstract class JacksonFileInstanceFactory extends FileInstanceFactory {
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;

View File

@@ -9,7 +9,7 @@ import com.serliunx.ddns.core.instance.Instance;
* @author SerLiunx * @author SerLiunx
* @since 1.0 * @since 1.0
*/ */
public class JsonFileInstanceFactory extends JacksonFileInstanceFactory{ public class JsonFileInstanceFactory extends JacksonFileInstanceFactory {
public JsonFileInstanceFactory(String instanceDir, JsonMapper jsonMapper) { public JsonFileInstanceFactory(String instanceDir, JsonMapper jsonMapper) {
super(instanceDir, jsonMapper); super(instanceDir, jsonMapper);

View File

@@ -9,7 +9,7 @@ import com.serliunx.ddns.core.instance.Instance;
* @author SerLiunx * @author SerLiunx
* @since 1.0 * @since 1.0
*/ */
public class XmlFileInstanceFactory extends JacksonFileInstanceFactory{ public class XmlFileInstanceFactory extends JacksonFileInstanceFactory {
public XmlFileInstanceFactory(String instanceDir, XmlMapper xmlMapper) { public XmlFileInstanceFactory(String instanceDir, XmlMapper xmlMapper) {
super(instanceDir, xmlMapper); super(instanceDir, xmlMapper);

View File

@@ -78,7 +78,7 @@ public class YamlFileInstanceFactory extends FileInstanceFactory {
} }
@SuppressWarnings(value = {"unchecked", "rawtypes"}) @SuppressWarnings(value = {"unchecked", "rawtypes"})
protected Instance buildInstance(Instance instance, Map<String, Object> valueMap){ protected Instance buildInstance(Instance instance, Map<String, Object> valueMap) {
Field[] declaredFields = ReflectionUtils.getDeclaredFields(instance.getClass(), true); Field[] declaredFields = ReflectionUtils.getDeclaredFields(instance.getClass(), true);
for (Field f : declaredFields) { for (Field f : declaredFields) {
if (Modifier.isStatic(f.getModifiers())) { if (Modifier.isStatic(f.getModifiers())) {

View File

@@ -71,9 +71,9 @@ public abstract class AbstractInstance implements Instance {
} }
value = ipAddress; value = ipAddress;
run0(); run0();
}catch (Exception e){ } catch (Exception e) {
log.error(e.getMessage()); log.error(e.getMessage());
}finally { } finally {
this.value = null; this.value = null;
} }
} }

View File

@@ -115,9 +115,9 @@ public class AliyunInstance extends AbstractInstance {
CompletableFuture<UpdateDomainRecordResponse> requestResponse = client.updateDomainRecord(request); CompletableFuture<UpdateDomainRecordResponse> requestResponse = client.updateDomainRecord(request);
try { try {
requestResponse.whenComplete((v, t) -> { requestResponse.whenComplete((v, t) -> {
if(t != null){ //出现异常 if (t != null) { //出现异常
handleThrowable(t); handleThrowable(t);
}else{ } else {
String result = null; String result = null;
try { try {
result = jsonMapper.writeValueAsString(v.getBody()); result = jsonMapper.writeValueAsString(v.getBody());
@@ -141,7 +141,7 @@ public class AliyunInstance extends AbstractInstance {
try { try {
DescribeDomainRecordInfoResponse response = responseCompletableFuture.get(5, TimeUnit.SECONDS); DescribeDomainRecordInfoResponse response = responseCompletableFuture.get(5, TimeUnit.SECONDS);
DescribeDomainRecordInfoResponseBody body = response.getBody(); DescribeDomainRecordInfoResponseBody body = response.getBody();
if(body != null){ if (body != null) {
return body.getValue(); return body.getValue();
} }
return null; return null;
@@ -241,17 +241,17 @@ public class AliyunInstance extends AbstractInstance {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
private void log(String msg, Object...params){ private void log(String msg, Object...params) {
log.info("[实例活动][" + name + "]" + msg, params); log.info("[实例活动][" + name + "]" + msg, params);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
private void debug(String msg, Object...params){ private void debug(String msg, Object...params) {
log.debug("[实例活动][" + name + "]" + msg, params); log.debug("[实例活动][" + name + "]" + msg, params);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
private void error(String msg, Object...params){ private void error(String msg, Object...params) {
log.error("[实例异常][" + name + "]" + msg, params); log.error("[实例异常][" + name + "]" + msg, params);
} }
} }

View File

@@ -11,40 +11,40 @@ public final class Assert {
private Assert(){throw new UnsupportedOperationException();} private Assert(){throw new UnsupportedOperationException();}
public static void notNull(Object object){ public static void notNull(Object object) {
notNull(object, null); notNull(object, null);
} }
public static void notNull(Object object, String msg){ public static void notNull(Object object, String msg) {
if(object == null) if(object == null)
throw new NullPointerException(msg); throw new NullPointerException(msg);
} }
public static void notNull(Object...objects){ public static void notNull(Object...objects) {
for (Object object : objects) { for (Object object : objects) {
notNull(object); notNull(object);
} }
} }
public static void isPositive(int i){ public static void isPositive(int i) {
if(i <= 0){ if(i <= 0) {
throw new IllegalArgumentException("指定参数必须大于0!"); throw new IllegalArgumentException("指定参数必须大于0!");
} }
} }
public static void isLargerThan(int source, int target){ public static void isLargerThan(int source, int target) {
if(source <= target){ if(source <= target) {
throw new IllegalArgumentException(String.format("%s太小了, 它必须大于%s", source, target)); throw new IllegalArgumentException(String.format("%s太小了, 它必须大于%s", source, target));
} }
} }
public static void notEmpty(Collection<?> collection){ public static void notEmpty(Collection<?> collection) {
notNull(collection); notNull(collection);
if(collection.isEmpty()) if(collection.isEmpty())
throw new IllegalArgumentException("参数不能为空!"); throw new IllegalArgumentException("参数不能为空!");
} }
public static void notEmpty(CharSequence charSequence){ public static void notEmpty(CharSequence charSequence) {
notNull(charSequence); notNull(charSequence);
if(charSequence.length() == 0) if(charSequence.length() == 0)
throw new IllegalArgumentException("参数不能为空!"); throw new IllegalArgumentException("参数不能为空!");

View File

@@ -14,19 +14,19 @@ public final class Configurer {
Configurer(){} Configurer(){}
public Configurer configuration(Configuration configuration){ public Configurer configuration(Configuration configuration) {
Assert.notNull(configuration); Assert.notNull(configuration);
this.configuration = configuration; this.configuration = configuration;
return this; return this;
} }
public Configurer instanceContext(MultipleSourceInstanceContext instanceContext){ public Configurer instanceContext(MultipleSourceInstanceContext instanceContext) {
Assert.notNull(instanceContext); Assert.notNull(instanceContext);
this.instanceContext = instanceContext; this.instanceContext = instanceContext;
return this; return this;
} }
public SystemInitializer done(){ public SystemInitializer done() {
Assert.notNull(configuration, instanceContext); Assert.notNull(configuration, instanceContext);
return new SystemInitializer(configuration, instanceContext); return new SystemInitializer(configuration, instanceContext);
} }

View File

@@ -25,7 +25,7 @@ public final class NetworkContextHolder {
private NetworkContextHolder(){throw new UnsupportedOperationException();} private NetworkContextHolder(){throw new UnsupportedOperationException();}
public static void setIpAddress(String i){ public static void setIpAddress(String i) {
try { try {
IP_LOCK.lock(); IP_LOCK.lock();
IP_ADDRESS = i; IP_ADDRESS = i;
@@ -37,11 +37,11 @@ public final class NetworkContextHolder {
} }
} }
public static String getIpAddress(){ public static String getIpAddress() {
if(IP_ADDRESS != null) if(IP_ADDRESS != null)
return IP_ADDRESS; return IP_ADDRESS;
try { try {
if(!IP_CONTEXT_WAIT_LATCH.await(IP_CONTEXT_TIME_OUT, TimeUnit.SECONDS)){ if(!IP_CONTEXT_WAIT_LATCH.await(IP_CONTEXT_TIME_OUT, TimeUnit.SECONDS)) {
log.error("IP地址获取超时."); log.error("IP地址获取超时.");
return null; return null;
} }

View File

@@ -30,7 +30,7 @@ import static com.serliunx.ddns.config.ConfigurationKeys.KEY_THREAD_POOL_CORE_SI
* @author SerLiunx * @author SerLiunx
* @since 1.0 * @since 1.0
*/ */
public final class SystemInitializer implements Refreshable{ public final class SystemInitializer implements Refreshable {
private static final Logger log = LoggerFactory.getLogger(SystemInitializer.class); private static final Logger log = LoggerFactory.getLogger(SystemInitializer.class);

View File

@@ -10,13 +10,13 @@ public final class SystemSupport {
private static final String PID; private static final String PID;
static{ static {
PID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; PID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
} }
private SystemSupport(){throw new UnsupportedOperationException();} private SystemSupport(){throw new UnsupportedOperationException();}
public static String getPid(){ public static String getPid() {
return PID; return PID;
} }
} }

View File

@@ -38,7 +38,7 @@ public class JacksonDecoder implements Decoder {
this.mapper = mapper; this.mapper = mapper;
} }
public static Decoder getInstance(){ public static Decoder getInstance() {
return decoder; return decoder;
} }

View File

@@ -39,7 +39,7 @@ public class JacksonEncoder implements Encoder {
this.mapper = mapper; this.mapper = mapper;
} }
public static Encoder getInstance(){ public static Encoder getInstance() {
return encoder; return encoder;
} }

View File

@@ -28,7 +28,7 @@ public interface IPAddressClient {
@RequestLine("GET /json") @RequestLine("GET /json")
IPAddressResponse getIPAddress(); IPAddressResponse getIPAddress();
static IPAddressClient getInstance(){ static IPAddressClient getInstance() {
return Feign.builder() return Feign.builder()
.encoder(JacksonEncoder.getInstance()) .encoder(JacksonEncoder.getInstance())
.decoder(JacksonDecoder.getInstance()) .decoder(JacksonDecoder.getInstance())

View File

@@ -12,7 +12,7 @@ public final class InstanceUtils {
private InstanceUtils(){throw new UnsupportedOperationException();} private InstanceUtils(){throw new UnsupportedOperationException();}
public static void validateInstance(Instance instance){ public static void validateInstance(Instance instance) {
Assert.notNull(instance); Assert.notNull(instance);
String instanceName = instance.getName(); String instanceName = instance.getName();
if(instanceName == null || instanceName.isEmpty()){ if(instanceName == null || instanceName.isEmpty()){

View File

@@ -20,7 +20,7 @@ public final class ReflectionUtils {
* @param setAccessible 是否将字段的可访问性 * @param setAccessible 是否将字段的可访问性
* @return 字段列表 * @return 字段列表
*/ */
public static Field[] getDeclaredFields(Class<?> clazz, boolean setAccessible){ public static Field[] getDeclaredFields(Class<?> clazz, boolean setAccessible) {
if(clazz == null){ if(clazz == null){
return null; return null;
} }
@@ -47,7 +47,7 @@ public final class ReflectionUtils {
* @param setAccessible 是否将字段的可访问性 * @param setAccessible 是否将字段的可访问性
* @return 字段列表 * @return 字段列表
*/ */
public static List<Field> getDeclaredFieldList(Class<?> clazz, boolean setAccessible){ public static List<Field> getDeclaredFieldList(Class<?> clazz, boolean setAccessible) {
return Arrays.asList(getDeclaredFields(clazz, setAccessible)); return Arrays.asList(getDeclaredFields(clazz, setAccessible));
} }
@@ -57,7 +57,7 @@ public final class ReflectionUtils {
* @param dest 目标对象 * @param dest 目标对象
* @param onlyNull 是否仅复制源对象不为空的属性 * @param onlyNull 是否仅复制源对象不为空的属性
*/ */
public static void copyField(Object src, Object dest,boolean onlyNull){ public static void copyField(Object src, Object dest,boolean onlyNull) {
Class<?> srcClass = src.getClass(); Class<?> srcClass = src.getClass();
Class<?> destClass = dest.getClass(); Class<?> destClass = dest.getClass();
List<Field> srcField = getDeclaredFieldList(srcClass, true); List<Field> srcField = getDeclaredFieldList(srcClass, true);