diff --git a/pom.xml b/pom.xml index d8bd51a..e29df33 100644 --- a/pom.xml +++ b/pom.xml @@ -6,22 +6,21 @@ com.irrigation common-lib - 1.2.4 + 2.0.6 UTF-8 1.8 - 1.5.10.RELEASE + 2.0.5.RELEASE 2.7.0 - 1.16.20 + 1.18.10 1.11 - 20.0 + 28.1-jre 1.7.24 1.2.49 - 4.3.14.RELEASE - 4.2.4.RELEASE + 5.0.9.RELEASE + 5.0.9.RELEASE 2.6.11 - @@ -116,6 +115,12 @@ ${spring-boot.version} + + + + + + @@ -123,6 +128,7 @@ org.apache.maven.plugins maven-compiler-plugin + 3.7.0 1.8 1.8 @@ -150,12 +156,12 @@ - nexus-releases - http://39.108.110.2:8081/repository/maven-releases/ + rdc-releases + https://repo.rdc.aliyun.com/repository/24784-release-oRxGgU/ - nexus-snapshot - http://39.108.110.2:8081/repository/maven-snapshots/ + rdc-snapshots + https://repo.rdc.aliyun.com/repository/24784-snapshot-Dsv8kn/ \ No newline at end of file diff --git a/src/main/java/com/irrigation/icl/cache/Command.java b/src/main/java/com/irrigation/icl/cache/Command.java index 6e12468..3173560 100644 --- a/src/main/java/com/irrigation/icl/cache/Command.java +++ b/src/main/java/com/irrigation/icl/cache/Command.java @@ -5,7 +5,8 @@ import lombok.Data; import java.io.Serializable; /** - * Command + * @author + * @Description 缓存 命令 类 */ @Data public class Command implements Serializable { diff --git a/src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java b/src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java index e8dcbb1..088b746 100644 --- a/src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java +++ b/src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java @@ -9,6 +9,8 @@ import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import org.springframework.data.redis.connection.RedisConnectionFactory; @@ -17,6 +19,8 @@ import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; +import java.time.Duration; + /** * @Author: boni * @Date: 2018/8/24-下午2:37 @@ -29,11 +33,19 @@ public class LayeringCacheAutoFactory { @Bean public LayeringCacheManager cacheManager(RedisTemplate redisTemplate, LayeringCacheProperties layeringCacheProperties) { - LayeringCacheManager cacheManager = new LayeringCacheManager(redisTemplate, layeringCacheProperties); - cacheManager.setUsePrefix(true); - long expire = layeringCacheProperties.getExpire() > 0L ? layeringCacheProperties.getExpire() : 120L; - cacheManager.setExpires(layeringCacheProperties.getExpires()); - cacheManager.setDefaultExpiration(expire); + + RedisCacheWriter redisCacheWriter = RedisCacheWriter + .nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory()); + long expire = layeringCacheProperties.getExpire() > 0L ? layeringCacheProperties.getExpire() : 1800L; + RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration + .defaultCacheConfig() + .entryTtl(Duration.ofSeconds(expire)); + LayeringCacheManager cacheManager = + new LayeringCacheManager( + redisTemplate, + redisCacheWriter, + redisCacheConfiguration, + layeringCacheProperties); return cacheManager; } @@ -43,8 +55,10 @@ public class LayeringCacheAutoFactory { MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); - container.addMessageListener(listenerAdapter, new PatternTopic(layeringCacheProperties.getCacheL1().getTopic())); - + LayeringCacheProperties.L1 l1 = layeringCacheProperties.getCacheL1(); + if (l1 != null) { + container.addMessageListener(listenerAdapter, new PatternTopic(layeringCacheProperties.getCacheL1().getTopic())); + } return container; } diff --git a/src/main/java/com/irrigation/icl/cache/config/CacheConfig.java b/src/main/java/com/irrigation/icl/cache/config/CacheConfig.java index cd80740..de308b6 100644 --- a/src/main/java/com/irrigation/icl/cache/config/CacheConfig.java +++ b/src/main/java/com/irrigation/icl/cache/config/CacheConfig.java @@ -25,7 +25,7 @@ public class CacheConfig { // om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); // jackson2JsonRedisSerializer.setObjectMapper(om); -// + template.setValueSerializer(new JdkSerializationRedisSerializer()); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); diff --git a/src/main/java/com/irrigation/icl/cache/core/LayeringCache.java b/src/main/java/com/irrigation/icl/cache/core/LayeringCache.java index 3917077..ead7b28 100644 --- a/src/main/java/com/irrigation/icl/cache/core/LayeringCache.java +++ b/src/main/java/com/irrigation/icl/cache/core/LayeringCache.java @@ -14,11 +14,13 @@ import net.sf.ehcache.store.MemoryStoreEvictionPolicy; import org.springframework.cache.Cache; import org.springframework.cache.support.SimpleValueWrapper; import org.springframework.data.redis.cache.RedisCache; -import org.springframework.data.redis.core.RedisOperations; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.util.StopWatch; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Optional; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Future; @@ -36,17 +38,16 @@ public class LayeringCache extends RedisCache { private CacheManager layeringL1CacheManager; private boolean layerL1Enable; private String ID; -// private String HOST; -// private String CACHE_STORE; -// private String CACHE_STORE_SYNC; - - public LayeringCache(LayeringCacheManager layeringCacheManager, String name, byte[] prefix, - RedisOperations redisOperations, long expiration) { - super(name, prefix, redisOperations, expiration); + public LayeringCache(LayeringCacheManager layeringCacheManager, String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfig) { + super(name, cacheWriter, cacheConfig); this.layeringCacheManager = layeringCacheManager; this.layeringCacheProperties = layeringCacheManager.getLayeringCacheProperties(); - this.layerL1Enable = layeringCacheProperties.getCacheL1().isEnable(); + this.layerL1Enable = + Optional.ofNullable(layeringCacheProperties.getCacheL1()) + .orElse(new LayeringCacheProperties.L1()) + .isEnable(); + initEhcache(); } @@ -54,6 +55,7 @@ public class LayeringCache extends RedisCache { public Cache.ValueWrapper get(Object key) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); + Ehcache ehCache = null; if (layerL1Enable) { ehCache = getL1Cache(super.getName()); @@ -157,7 +159,7 @@ public class LayeringCache extends RedisCache { Callable callable = () -> { Ehcache cache = layeringL1CacheManager.getCache(name); if (cache == null) { - CacheConfiguration cacheConfiguration = getCacheConfiguration(); + CacheConfiguration cacheConfiguration = getEncacheCacheConfiguration(); cacheConfiguration.setName(name); Ehcache ehcache = new net.sf.ehcache.Cache(cacheConfiguration); layeringL1CacheManager.addCache(ehcache); @@ -188,7 +190,7 @@ public class LayeringCache extends RedisCache { return simpleDateFormat.format(new Date()); } - private CacheConfiguration getCacheConfiguration() { + private CacheConfiguration getEncacheCacheConfiguration() { // Cache CacheConfiguration cacheConfiguration = new CacheConfiguration(); cacheConfiguration.setEternal(false); @@ -205,12 +207,6 @@ public class LayeringCache extends RedisCache { cacheConfiguration.copyOnRead(true); cacheConfiguration.copyOnWrite(true); -// CopyStrategyConfiguration copyStrategyConfiguration = new CopyStrategyConfiguration(); -// copyStrategyConfiguration.setClass(MyCopyStrategy.class.getName()); -// copyStrategyConfiguration.setCopyStrategyInstance(new MyCopyStrategy()); - -// cacheConfiguration.addCopyStrategy(copyStrategyConfiguration); - cacheConfiguration.setTimeToIdleSeconds(layeringCacheProperties.getCacheL1().getLocalTimeToIdleSeconds()); cacheConfiguration.setTimeToLiveSeconds(layeringCacheProperties.getCacheL1().getLocalTimeToLiveSeconds()); @@ -220,13 +216,6 @@ public class LayeringCache extends RedisCache { private void initEhcache() { if (layerL1Enable) { this.ID = layeringCacheProperties.getCacheL1().getKey() + "." + getDate(); -// this.HOST = Utils.getLocalHostIP(); -// this.CACHE_STORE = layeringCacheProperties.getCacheL1().getKey() -// + layeringCacheProperties.getCacheL1().getSeparator() -// + "cache" -// + layeringCacheProperties.getCacheL1().getSeparator() -// + "store"; -// this.CACHE_STORE_SYNC = this.CACHE_STORE + layeringCacheProperties.getCacheL1().getSeparator() + "sync"; Configuration configuration = new Configuration(); configuration.setName(this.ID); @@ -238,7 +227,7 @@ public class LayeringCache extends RedisCache { dsc.setPath(layeringCacheProperties.getCacheL1().getLocalStoreLocation() + this.ID); configuration.diskStore(dsc); - CacheConfiguration cacheConfiguration = getCacheConfiguration(); + CacheConfiguration cacheConfiguration = getEncacheCacheConfiguration(); configuration.setDefaultCacheConfiguration(cacheConfiguration); configuration.setDynamicConfig(false); @@ -248,25 +237,4 @@ public class LayeringCache extends RedisCache { layeringL1CacheManager = new CacheManager(configuration); } } -// -// class MyCopyStrategy implements ReadWriteCopyStrategy { -// @Override -// public Element copyForWrite(Element value) { -// if (value != null) { -// Object temp = (Serializable) value.getObjectValue(); -// return new Element(value.getObjectKey(), temp); -// } -// return value; -// } -// -// @Override -// public Element copyForRead(Element storedValue) { -// if (storedValue != null) { -// Object temp = (Serializable) storedValue.getObjectValue(); -// return new Element(storedValue.getObjectKey(), temp); -// } -// return storedValue; -// -// } -// } } diff --git a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheLoadCondition.java b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheLoadCondition.java index a8cfbe6..8dbc834 100644 --- a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheLoadCondition.java +++ b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheLoadCondition.java @@ -1,6 +1,6 @@ package com.irrigation.icl.cache.core; -import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.bind.Binder; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; @@ -12,13 +12,10 @@ import org.springframework.core.type.AnnotatedTypeMetadata; public class LayeringCacheLoadCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { - RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( - context.getEnvironment(), "springext.cache."); - - Boolean enable = resolver.getProperty("enable",Boolean.class); - if(enable == null){ - return false; - } + Boolean enable = Binder + .get(context.getEnvironment()) + .bind("springext.cache.enable", Boolean.class) + .orElse(false); return enable.booleanValue(); } diff --git a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheManager.java b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheManager.java index dd7fedd..328afba 100644 --- a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheManager.java +++ b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheManager.java @@ -1,47 +1,42 @@ package com.irrigation.icl.cache.core; import com.irrigation.icl.cache.Command; +import lombok.Getter; import org.springframework.data.redis.cache.RedisCache; +import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; -import org.springframework.data.redis.core.RedisOperations; +import org.springframework.data.redis.cache.RedisCacheWriter; +import org.springframework.data.redis.core.RedisTemplate; /** * @Author: boni * @Date: 2018/8/24-下午2:29 */ +@Getter public class LayeringCacheManager extends RedisCacheManager { + final RedisCacheWriter cacheWriter; + final RedisCacheConfiguration redisCacheConfiguration; private LayeringCacheProperties layeringCacheProperties; - - public LayeringCacheProperties getLayeringCacheProperties() { - return layeringCacheProperties; - } - - public void setLayeringCacheProperties(LayeringCacheProperties layeringCacheProperties) { + private RedisTemplate redisTemplate; + + public LayeringCacheManager(RedisTemplate redisTemplate, RedisCacheWriter cacheWriter, RedisCacheConfiguration redisCacheConfiguration, + LayeringCacheProperties layeringCacheProperties) { + super(cacheWriter, redisCacheConfiguration); + this.cacheWriter = cacheWriter; + this.redisCacheConfiguration = redisCacheConfiguration; + this.redisTemplate = redisTemplate; this.layeringCacheProperties = layeringCacheProperties; } - public LayeringCacheManager(RedisOperations redisOperations, LayeringCacheProperties layeringCacheProperties) { - super(redisOperations); - this.layeringCacheProperties = layeringCacheProperties; - } - - @SuppressWarnings("unchecked") @Override - protected RedisCache createCache(String cacheName) { - long expiration = computeExpiration(cacheName); - return new LayeringCache( - this, - cacheName, - (this.isUsePrefix() ? this.getCachePrefix().prefix(cacheName) : null), - this.getRedisOperations(), - expiration); + protected RedisCache getMissingCache(String name) { + return new LayeringCache(this, name, this.cacheWriter, this.redisCacheConfiguration); } - //notify other redis clent to update L1 public void publishMessage(Command command) { - this.getRedisOperations().convertAndSend(this.getLayeringCacheProperties().getCacheL1().getTopic(), command); + this.getRedisTemplate().convertAndSend(this.getLayeringCacheProperties().getCacheL1().getTopic(), command); } diff --git a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java index 80db5a1..c0bebd0 100644 --- a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java +++ b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java @@ -73,10 +73,10 @@ public class LayeringCacheProperties { /** * 本地缓存过期时间,单位秒,默认10分钟 */ - private int localTimeToLiveSeconds = 1 * 60; - private int localTimeToIdleSeconds = 0; + private int localTimeToLiveSeconds = 10 * 60; + private int localTimeToIdleSeconds = 30; /** - * 本地缓存清理周期,单位秒,默认3分钟 + * 本地缓存清理周期,单位秒,默认90秒 */ private int localDiskExpiryThreadIntervalSeconds = 90; } diff --git a/src/main/java/com/irrigation/icl/entity/PageList.java b/src/main/java/com/irrigation/icl/entity/PageList.java index b403dc8..31dc3ed 100644 --- a/src/main/java/com/irrigation/icl/entity/PageList.java +++ b/src/main/java/com/irrigation/icl/entity/PageList.java @@ -10,8 +10,10 @@ import lombok.NoArgsConstructor; import java.util.List; /** - * 分页返回类 * @param + * @author zhangchuanxi + * 分页返回类 + * @version 1.0.0 */ @Data @NoArgsConstructor diff --git a/src/main/java/com/irrigation/icl/entity/PageParam.java b/src/main/java/com/irrigation/icl/entity/PageParam.java index 973b29d..ba2b376 100644 --- a/src/main/java/com/irrigation/icl/entity/PageParam.java +++ b/src/main/java/com/irrigation/icl/entity/PageParam.java @@ -7,6 +7,8 @@ import lombok.Data; import lombok.NoArgsConstructor; /** + * @author zhangchuanxi + * @version 1.0.0 * 分页参数类 */ @Data diff --git a/src/main/java/com/irrigation/icl/entity/RestResult.java b/src/main/java/com/irrigation/icl/entity/RestResult.java index f06d1d1..dafcb87 100644 --- a/src/main/java/com/irrigation/icl/entity/RestResult.java +++ b/src/main/java/com/irrigation/icl/entity/RestResult.java @@ -11,25 +11,29 @@ import lombok.Data; import lombok.NoArgsConstructor; /** - * 公共返回类 * @param + * @author zhangchuanxi + * 公共返回类 + * @version 1.0.0 */ @Data @AllArgsConstructor @NoArgsConstructor @ApiModel(value = "RestResult") public class RestResult { - @ApiModelProperty(value = "返回编码类型",example = "200",allowableValues = "200,400") + @ApiModelProperty(value = "返回编码类型", example = "200", allowableValues = "200,400") private int code; - @ApiModelProperty(value = "返回提示消息",example = "成功") + @ApiModelProperty(value = "返回提示消息", example = "成功") private String message; @ApiModelProperty(value = "返回请求数据对象") private T data; public String getMessage() { - return message != null?message: ResultEnum.getCode(code).getMessage(); + return message != null ? message : ResultEnum.getCode(code).getMessage(); } - public String toString(){ + + @Override + public String toString() { return JSON.toJSONString(this); } } diff --git a/src/main/java/com/irrigation/icl/enums/ResultEnum.java b/src/main/java/com/irrigation/icl/enums/ResultEnum.java index 2d0e818..2af1cdc 100644 --- a/src/main/java/com/irrigation/icl/enums/ResultEnum.java +++ b/src/main/java/com/irrigation/icl/enums/ResultEnum.java @@ -3,11 +3,18 @@ package com.irrigation.icl.enums; import java.util.Arrays; import java.util.Optional; +/** + * @author zhangchuanxi + * @Description api 返回结果code 类型 + */ + public enum ResultEnum { - SUCCESS(200, "success"), - SUCCESS_NO_DATA(202,"查询成功,无数据"), - ERROR(400, "failed"), + SUCCESS(200, "操作成功"), + + SUCCESS_NO_DATA(202, "查询成功,无数据"), + + ERROR(400, "操作失败"), PARAMETER_MISMATCH(402, "参数不匹配!"), @@ -19,14 +26,14 @@ public enum ResultEnum { DATA_OPT(502, "数据库操作异常!"), - REMOTE_CALL_FAILED(503,"远程调用失败"), + REMOTE_CALL_FAILED(503, "远程调用失败"), SESSION_EXPIRE(401, "会话失效,请重新登录!"); private final int code; private final String message; - ResultEnum(int code, String message) { + ResultEnum(int code, String message) { this.code = code; this.message = message; } diff --git a/src/main/java/com/irrigation/icl/exception/AbstractException.java b/src/main/java/com/irrigation/icl/exception/AbstractException.java index 50a07bb..01c1c41 100644 --- a/src/main/java/com/irrigation/icl/exception/AbstractException.java +++ b/src/main/java/com/irrigation/icl/exception/AbstractException.java @@ -9,14 +9,19 @@ package com.irrigation.icl.exception; - import com.irrigation.icl.exception.code.ErrorCode; import org.springframework.http.HttpStatus; import java.io.PrintWriter; import java.io.StringWriter; + +/** + * @author zhangchuanxi + * @Description 自定义抽象异常类 + */ public abstract class AbstractException extends RuntimeException { + private static final long serialVersionUID = 1L; private final HttpStatus status; diff --git a/src/main/java/com/irrigation/icl/exception/ContextRuntimeException.java b/src/main/java/com/irrigation/icl/exception/ContextRuntimeException.java index 9e70b52..364c4b9 100644 --- a/src/main/java/com/irrigation/icl/exception/ContextRuntimeException.java +++ b/src/main/java/com/irrigation/icl/exception/ContextRuntimeException.java @@ -1,5 +1,10 @@ package com.irrigation.icl.exception; +/** + * @author zhangchuanxi + * @Description 自定义异常类 + */ + public class ContextRuntimeException extends RuntimeException { public ContextRuntimeException(String message) { diff --git a/src/main/java/com/irrigation/icl/utils/DateFormatUtils.java b/src/main/java/com/irrigation/icl/utils/DateFormatUtils.java new file mode 100644 index 0000000..43efc64 --- /dev/null +++ b/src/main/java/com/irrigation/icl/utils/DateFormatUtils.java @@ -0,0 +1,81 @@ +package com.irrigation.icl.utils; + +import java.text.SimpleDateFormat; +import java.util.Locale; + +/** + * @author zhangchuanxi + * SimpleDateFormat 工具类 + * @version 1.0.1 + */ +public class DateFormatUtils { + + + public static SimpleDateFormat getInstance(String format) { + return new SimpleDateFormat(format); + } + + public static SimpleDateFormat getInstance(String format, Locale locale) { + return new SimpleDateFormat(format, locale); + } + + /** + * 标准日期格式 yyyy-MM-dd + */ + public final static SimpleDateFormat NORM_DATE_FORMAT = new SimpleDateFormat(DatePatternUtils.NORM_DATE_PATTERN); + + /** + * 标准时间格式 :HH:mm:ss + */ + public final static SimpleDateFormat NORM_TIME_FORMAT = new SimpleDateFormat(DatePatternUtils.NORM_TIME_PATTERN); + + /** + * 标准日期时间格式,精确到分 :yyyy-MM-dd HH:mm + */ + public final static SimpleDateFormat NORM_DATETIME_MINUTE_FORMAT = new SimpleDateFormat(DatePatternUtils.NORM_DATETIME_MINUTE_PATTERN); + + /** + * 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss + */ + public final static String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; + /** + * 标准日期时间格式,精确到秒 :yyyy-MM-dd HH:mm:ss + */ + public final static SimpleDateFormat NORM_DATETIME_FORMAT = new SimpleDateFormat(DatePatternUtils.NORM_DATETIME_PATTERN); + + + /** + * 标准日期时间格式,精确到毫秒 :yyyy-MM-dd HH:mm:ss.SSS + */ + public final static SimpleDateFormat NORM_DATETIME_MS_FORMAT = new SimpleDateFormat(DatePatternUtils.NORM_DATETIME_MS_PATTERN); + + /** + * 标准日期格式 :yyyy年MM月dd日 + */ + public final static SimpleDateFormat CHINESE_DATE_FORMAT = new SimpleDateFormat(DatePatternUtils.CHINESE_DATE_PATTERN); + + /** + * 标准日期格式 :yyyyMMdd + */ + public final static SimpleDateFormat PURE_DATE_FORMAT = new SimpleDateFormat(DatePatternUtils.PURE_DATE_PATTERN); + + + /** + * 标准日期格式 :HHmmss + */ + public final static SimpleDateFormat PURE_TIME_FORMAT = new SimpleDateFormat(DatePatternUtils.PURE_TIME_PATTERN); + + + /** + * 标准日期格式 :yyyyMMddHHmmss + */ + public final static SimpleDateFormat PURE_DATETIME_FORMAT = new SimpleDateFormat(DatePatternUtils.PURE_DATETIME_PATTERN); + + + /** + * 标准日期格式 :yyyyMMddHHmmssSSS + */ + public final static SimpleDateFormat PURE_DATETIME_MS_FORMAT = new SimpleDateFormat(DatePatternUtils.PURE_DATETIME_MS_PATTERN); + + +} diff --git a/src/main/java/com/irrigation/icl/utils/DatePatternUtils.java b/src/main/java/com/irrigation/icl/utils/DatePatternUtils.java new file mode 100644 index 0000000..860d0de --- /dev/null +++ b/src/main/java/com/irrigation/icl/utils/DatePatternUtils.java @@ -0,0 +1,141 @@ +package com.irrigation.icl.utils; + + +/** + * @author zhangchuanxi + * 日期格式化样式 + * @version 1.0.1 + *

+ * 格式描述: + * 年yyyy + * 月MM + * 日dd + * 时HH + * 分mm + * 秒ss + * 毫秒SS + */ +public class DatePatternUtils { + + private DatePatternUtils() { + } + + + //-------------------------------------------------------------------------------------------------------------------------------- Normal + + + /** + * 标准日期格式:yyyy-MM-dd + */ + public final static String NORM_YM_PATTERN = "yyyy-MM"; + + /** + * 标准日期格式:yyyy-MM-dd + */ + public final static String NORM_DATE_PATTERN = "yyyy-MM-dd"; + /** + * 标准时间格式:HH:mm:ss + */ + public final static String NORM_TIME_PATTERN = "HH:mm:ss"; + + + /** + * 标准日期时间格式,精确到分:yyyy-MM-dd HH:mm + */ + public final static String NORM_DATETIME_MINUTE_PATTERN = "yyyy-MM-dd HH:mm"; + + /** + * 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss + */ + public final static String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; + + + /** + * 标准日期时间格式,精确到毫秒:yyyy-MM-dd HH:mm:ss.SSS + */ + public final static String NORM_DATETIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS"; + + + /** + * 标准日期格式:yyyy年MM月dd日 + */ + public final static String CHINESE_DATE_PATTERN = "yyyy年MM月dd日"; + + + //-------------------------------------------------------------------------------------------------------------------------------- Pure + /** + * 标准日期格式:yyyyMMdd + */ + public final static String PURE_DATE_PATTERN = "yyyyMMdd"; + + + /** + * 标准日期格式:HHmmss + */ + public final static String PURE_TIME_PATTERN = "HHmmss"; + + + /** + * 标准日期格式:yyyyMMddHHmmss + */ + public final static String PURE_DATETIME_PATTERN = "yyyyMMddHHmmss"; + + + /** + * 标准日期格式:yyyyMMddHHmmssSSS + */ + public final static String PURE_DATETIME_MS_PATTERN = "yyyyMMddHHmmssSSS"; + + + /** + * yyyy/MM/dd" + */ + public final static String EXTRA_DATE_PATTERN = "yyyy/MM/dd"; + + /** + * yyyy/MM/dd HH:mm + */ + public final static String EXTRA_DATETIME_MINUTE_PATTERN = "yyyy/MM/dd HH:mm"; + + /** + * yyyy/MM/dd HH:mm:ss + */ + public final static String EXTRA_DATETIME_PATTERN = "yyyy/MM/dd HH:mm:ss"; + + + // //-------------------------------------------------------------------------------------------------------------------------------- Others +// /** +// * HTTP头中日期时间格式:EEE, dd MMM yyyy HH:mm:ss z +// */ +// public final static String HTTP_DATETIME_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z"; +// +// +// /** +// * JDK中日期时间格式:EEE MMM dd HH:mm:ss zzz yyyy +// */ +// public final static String JDK_DATETIME_PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy"; +// +// +// /** +// * UTC时间:yyyy-MM-dd'T'HH:mm:ss'Z' +// */ +// public final static String UTC_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'"; +// +// +// /** +// * UTC时间:yyyy-MM-dd'T'HH:mm:ssZ +// */ +// public final static String UTC_WITH_ZONE_OFFSET_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ"; +// +// +// /** +// * UTC时间:yyyy-MM-dd'T'HH:mm:ss.SSS'Z' +// */ +// public final static String UTC_MS_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; +// +// /** +// * UTC时间:yyyy-MM-dd'T'HH:mm:ssZ +// */ +// public final static String UTC_MS_WITH_ZONE_OFFSET_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; + +} \ No newline at end of file diff --git a/src/main/java/com/irrigation/icl/utils/DateUtils.java b/src/main/java/com/irrigation/icl/utils/DateUtils.java new file mode 100644 index 0000000..f0507d8 --- /dev/null +++ b/src/main/java/com/irrigation/icl/utils/DateUtils.java @@ -0,0 +1,766 @@ +package com.irrigation.icl.utils; + +import java.text.ParseException; +import java.time.*; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +/** + * 日期工具类 + * + * @version 1.0.1 + */ +public class DateUtils { + + + /** + * 当前时间,转换为{@link Date}对象 + * + * @return 当前时间 + */ + public static Date date() { + return date(currentTimeMillis()); + } + + + /** + * 获取时间 + * + * @param date 时间戳 + * @return + */ + public static Date date(Long date) { + return new Date(date); + } + + /** + * 获取指定某一天的开始时间戳 + * + * @param date + * @return + */ + public static Date getDailyStartTime(Date date) { + Calendar calendar = calendar(date); + calendar.set(Calendar.HOUR_OF_DAY, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.MILLISECOND, 0); + return calendar.getTime(); + } + + /** + * 获取指定某一天的结束时间戳 + * + * @param date + * @return + */ + public static Date getDailyEndTime(Date date) { + Calendar calendar = calendar(date); + calendar.set(Calendar.HOUR_OF_DAY, 23); + calendar.set(Calendar.MINUTE, 59); + calendar.set(Calendar.SECOND, 59); + calendar.set(Calendar.MILLISECOND, 999); + return calendar.getTime(); + } + + /** + * 日期加减 + * + * @param date + * @param field 对应 Calendar 常量 + * @param amount + * @return + */ + public static Date dateFieldAdd(Date date, int field, int amount) { + Calendar calendar = calendar(date); + calendar.add(field, amount); + return calendar.getTime(); + } + + + /** + * 创建Calendar对象,时间为默认时区的当前时间 + * + * @return Calendar对象 + * @since 4.6.6 + */ + public static Calendar calendar() { + return Calendar.getInstance(); + } + + /** + * 转换为Calendar对象 + * + * @param date 日期对象 + * @return Calendar对象 + */ + public static Calendar calendar(Date date) { + return calendar(date.getTime()); + } + + /** + * 转换为Calendar对象 + * + * @param millis 时间戳 + * @return Calendar对象 + */ + public static Calendar calendar(long millis) { + final Calendar cal = calendar(); + cal.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); + cal.setTimeInMillis(millis); + return cal; + } + + private static Calendar toCalendar(Date date) { + return toCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT), date); + } + + + private static Calendar toCalendar(TimeZone zone, Locale locale, Date date) { + if (null == locale) { + locale = Locale.getDefault(Locale.Category.FORMAT); + } + final Calendar cal = (null != zone) ? Calendar.getInstance(zone, locale) : Calendar.getInstance(locale); + cal.setFirstDayOfWeek(Calendar.MONDAY); + cal.setTime(date); + return cal; + } + + /** + * 当前时间的时间戳 + * + * @return 时间 + */ + public static long currentTimeMillis() { + return System.currentTimeMillis(); + } + + + /** + * 当前时间的时间戳(秒) + * + * @return 当前时间秒数 + */ + public static long currentSeconds() { + return System.currentTimeMillis() / 1000; + } + + /** + * 获得年的部分 + * + * @param date 日期 + * @return 年的部分 + */ + public static int year(Date date) { + return toCalendar(date).get(Calendar.YEAR); + } + + + /** + * 获得指定日期所属季度,从1开始计数 + * + * @param date 日期 + * @return 第几个季度 + * @since 4.1.0 + */ + public static int quarter(Date date) { + return month(date) / 3 + 1; + } + + + /** + * 获得月份,从0开始计数 + * + * @param date 日期 + * @return 月份,从0开始计数 + */ + public static int month(Date date) { + return toCalendar(date).get(Calendar.MONTH); + } + + + /** + * 获得指定日期是所在年份的第几周
+ * + * @param date 日期 + * @return 周 + */ + public static int weekOfYear(Date date) { + return toCalendar(date).get(Calendar.WEEK_OF_YEAR); + } + + /** + * 获得指定日期是所在月份的第几周
+ * + * @param date 日期 + * @return 周 + */ + public static int weekOfMonth(Date date) { + return toCalendar(date).get(Calendar.WEEK_OF_MONTH); + } + + /** + * 获得指定日期是这个日期所在月份的第几天
+ * + * @param date 日期 + * @return 天 + */ + public static int dayOfMonth(Date date) { + return toCalendar(date).get(Calendar.DAY_OF_MONTH); + } + + /** + * 获得指定日期是星期几,1表示周日,2表示周一 + * + * @param date 日期 + * @return 天 + */ + public static int dayOfWeek(Date date) { + return toCalendar(date).get(Calendar.DAY_OF_WEEK); + + } + + /** + * 获得指定日期的小时数部分
+ * + * @param date 日期 + * @return 小时数 + */ + public static int hour(Date date) { + return toCalendar(date).get(Calendar.HOUR_OF_DAY); + } + + /** + * 获得指定日期的分钟数部分
+ * 例如:10:04:15.250 =》 4 + * + * @param date 日期 + * @return 分钟数 + */ + public static int minute(Date date) { + return toCalendar(date).get(Calendar.MINUTE); + + } + + /** + * 获得指定日期的秒数部分
+ * + * @param date 日期 + * @return 秒数 + */ + public static int second(Date date) { + return toCalendar(date).get(Calendar.SECOND); + } + + /** + * 获得指定日期的毫秒数部分
+ * + * @param date 日期 + * @return 毫秒数 + */ + public static int millisecond(Date date) { + return toCalendar(date).get(Calendar.MILLISECOND); + + } + + /** + * 当前时间,格式 yyyy-MM-dd HH:mm:ss + * + * @return 当前时间的标准形式字符串 + */ + public static String now() { + return formatDateTime(new Date()); + } + + /** + * 当前日期,格式 yyyy-MM-dd + * + * @return 当前日期的标准形式字符串 + */ + public static String today() { + return formatDate(new Date()); + } + + // ------------------------------------ Format start ---------------------------------------------- + + + /** + * 根据特定格式格式化日期 + * + * @param date 被格式化的日期 + * @param pattern 日期格式,常用格式见: + * @return 格式化后的字符串 + * @see DatePatternUtils + */ + public static String format(Date date, String pattern) { + if (null == date || ObjectUtils.stringIsEmpty(pattern)) { + return null; + } + return DateFormatUtils.getInstance(pattern).format(date); + } + + /** + * 格式化日期时间
+ * 格式 yyyy-MM-dd HH:mm:ss + * + * @param date 被格式化的日期 + * @return 格式化后的日期 + */ + public static String formatDateTime(Date date) { + if (null == date) { + return null; + } + return DateFormatUtils.NORM_DATETIME_FORMAT.format(date); + } + + /** + * 格式化日期部分(不包括时间)
+ * 格式 yyyy-MM-dd + * + * @param date 被格式化的日期 + * @return 格式化后的字符串 + */ + public static String formatDate(Date date) { + if (null == date) { + return null; + } + return DateFormatUtils.NORM_DATE_FORMAT.format(date); + } + + /** + * 格式化时间
+ * 格式 HH:mm:ss + * + * @param date 被格式化的日期 + * @return 格式化后的字符串 + * @since 3.0.1 + */ + public static String formatTime(Date date) { + if (null == date) { + return null; + } + return DateFormatUtils.NORM_TIME_FORMAT.format(date); + } + + /** + * 格式化给定样式 + * + * @param timestamp + * @param pattern + * @return + * @see DatePatternUtils + */ + public static String format(Long timestamp, String pattern) { + if (ObjectUtils.isNull(timestamp) || ObjectUtils.isNull(pattern)) { + return null; + } + return DateFormatUtils.getInstance(pattern).format(date(timestamp)); + } + + // ------------------------------------ Format end ---------------------------------------------- + + // ------------------------------------ Parse start ---------------------------------------------- + + + /** + * 构建DateTime对象 + * + * @param dateStr Date字符串 + * @param pattern 格式 + * @return DateTime对象 + * @see DatePatternUtils + */ + public static Date parse(String dateStr, String pattern) { + try { + return DateFormatUtils.getInstance(pattern).parse(dateStr); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 将特定格式的日期转换为Date对象 + * + * @param dateStr 特定格式的日期 + * @param pattern 格式,例如yyyy-MM-dd + * @param locale 区域信息 + * @return 日期对象 + * @see DatePatternUtils + * @since 4.5.18 + */ + public static Date parse(String dateStr, String pattern, Locale locale) { + try { + return DateFormatUtils.getInstance(pattern, locale).parse(dateStr); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 格式yyyy-MM-dd HH:mm:ss + * + * @param dateString 标准形式的时间字符串 + * @return 日期对象 + */ + public static Date parseDateTime(String dateString) { + return parse(dateString, DatePatternUtils.NORM_DATETIME_PATTERN); + } + + /** + * 解析格式为yyyy-MM-dd的日期,忽略时分秒 + * + * @param dateString 标准形式的日期字符串 + * @return 日期对象 + */ + public static Date parseDate(String dateString) { + return parse(dateString, DatePatternUtils.NORM_DATE_PATTERN); + } + + /** + * 解析时间,格式HH:mm:ss,日期部分默认为1970-01-01 + * + * @param timeString 标准形式的日期字符串 + * @return 日期对象 + */ + public static Date parseTime(String timeString) { + return parse(timeString, DatePatternUtils.NORM_TIME_PATTERN); + } + + // ------------------------------------ Parse end ---------------------------------------------- + + /** + * date 转换为 long + * + * @param date + * @return + */ + public static long dateToLong(Date date) { + return ObjectUtils.isNull(date) ? 0L : date.getTime(); + } + + + /** + * 时间字符串转换long + * + * @param dateStr + * @param pattern + * @return + * @see DatePatternUtils + */ + public static long dateToLong(String dateStr, String pattern) { + if (ObjectUtils.isNull(dateStr) || ObjectUtils.isNull(pattern)) { + return 0L; + } + return parse(dateStr, pattern).getTime(); + } + +//---------------------new api + + + /** + * 获取指定天的北京时间戳为00:00:00的日期 + * + * @param year + * @param month + * @param day + * @return + */ + public static Date getStartTimeOfDay(int year, int month, int day) { + LocalDate localDate = LocalDate.of(year, month, day); + Instant instant = localDate.atStartOfDay(ZoneId.of("Asia/Shanghai")).toInstant(); + return Date.from(instant); + } + + /** + * 获取某月中第一天北京时间戳为00:00:00的日期 + * + * @param year + * @param month + * @return + */ + public static Date getStartDateOfMonth(int year, int month) { + return getStartTimeOfDay(year, month, 1); + } + + /** + * 获取某月中最后一天北京时间为23:59:59.999的日期 + * + * @param year + * @param month + * @return + */ + public static Date getEndDateOfMonth(int year, int month) { + YearMonth yearMonth = YearMonth.of(year, month); + LocalDate endOfMonth = yearMonth.atEndOfMonth(); + LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999); + ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai")); + return Date.from(zonedDateTime.toInstant()); + } + + /** + * 获得指定年北京时间0点的开始时间戳 + * + * @param year + * @return + */ + public static long getYearStartMilliseconds(Integer year) { + return LocalDateTime.of(year, 1, 1, 0, 0, 0, 0).toInstant(ZoneOffset.of("+8")).toEpochMilli(); + } + + /** + * 获得指定年北京时间最后一毫秒的时间戳 + * + * @param year + * @return + */ + public static long getYearEndMilliseconds(Integer year) { + return getYearStartMilliseconds(year + 1) - 1; + } + + /** + * 获得指定日期北京时间0点的时间戳 + * + * @param year + * @param month + * @param day + * @return + */ + public static long getDayStartMilliseconds(Integer year, Integer month, Integer day) { + return LocalDateTime.of(year, month, day, 0, 0, 0, 0).toInstant(ZoneOffset.of("+8")).toEpochMilli(); + } + + /** + * 获取指定日期北京时间23:59:59.999的时间戳 + * + * @param year + * @param month + * @param day + * @return + */ + public static long getDayEndMilliseconds(Integer year, Integer month, Integer day) { + return LocalDateTime.of(year, month, day, 23, 59, 59, 999).toInstant(ZoneOffset.of("+8")).toEpochMilli(); + } + + /** + * 获取传入时间戳所指向日期北京时间0点的时间戳 + * + * @param timestamp + * @return + */ + public static long getDayStartTimeStamp(Long timestamp) { + LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.ofHours(8)); + LocalDate localDate = time2.toLocalDate(); + return localDate.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli(); + } + + /** + * 获取传入时间戳所指向日期北京时间23:59:59.999的时间戳 + * + * @param timestamp + * @return + */ + public static long getDayEndTimeStamp(Long timestamp) { + LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.ofHours(8)).plusDays(1L); + LocalDate localDate = time2.toLocalDate(); + return localDate.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli() - 1L; + } + + + /** + * 获取指定月北京时间1月1日0点的时间戳 + * + * @param month + * @return + */ + public static long getStartTimestampOfMonth(int month) { + YearMonth yearMonth = YearMonth.now(ZoneId.of("Asia/Shanghai")); + return getDayStartMilliseconds(yearMonth.getYear(), month, 1); + } + + /** + * 获取当年一月的格式化字符串 yyyy-MM + * + * @return + */ + public static String getJanuary() { + YearMonth yearMonth = YearMonth.now().withMonth(1); + return yearMonth.format(DateTimeFormatter.ofPattern(DatePatternUtils.NORM_YM_PATTERN)); + } + + /** + * 获取当月格式化字符串 yyyy-MM + * + * @return + */ + public static String getCurrentMonth() { + LocalDate now = LocalDate.now(ZoneId.of("Asia/Shanghai")); + return now.format(DateTimeFormatter.ofPattern(DatePatternUtils.NORM_YM_PATTERN)); + } + + /** + * 通过时间戳获取前一日北京日期 + * + * @param timestamp + * @return + */ + public static Date yesterday(Long timestamp) { + LocalDateTime time2 = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of("Asia/Shanghai")).minusDays(1L); + return Date.from(time2.toInstant(ZoneOffset.ofHours(8))); + } + + /** + * 将时间戳转换为北京日期 + * + * @param timestamp + * @return + */ + public static Date convertTimestampToDate(Long timestamp) { + Instant instant = Instant.ofEpochMilli(timestamp).atOffset(ZoneOffset.ofHours(8)).toInstant(); + return Date.from(instant); + + } + + /** + * 获取timestamp+8小时的日期,专用于MongoDB查询 + * + * @param timestamp + * @return + */ + public static Date getYesterdayUtcDate(Long timestamp) { + Instant instant = Instant.ofEpochMilli(timestamp).plus(8L, ChronoUnit.HOURS).minus(1L, ChronoUnit.DAYS); + return Date.from(instant); + } + + + /** + * 获取+8小时时间戳的日期,专用于MongoDB查询 + * + * @param timestamp + * @return + */ + public static Date convertTimestampToUtcDate(Long timestamp) { + Instant instant = Instant.ofEpochMilli(timestamp).plus(8, ChronoUnit.HOURS); + return Date.from(instant); + } + + /** + * 获取日期 + * + * @return 如:2014-12-10 + */ + public static LocalDate getLocalDateNow() { + return LocalDate.now(); + } + + /** + * 获取日期 + * + * @param year + * @param month + * @param dayOfMont + * @return 如:2014-12-10 + */ + public static LocalDate getLocalDate(int year, int month, int dayOfMont) { + return LocalDate.of(year, month, dayOfMont); + } + + /** + * 获取时间 + * + * @return 如:14:29:40.558 + */ + public static LocalTime getLocalTimeNow() { + return LocalTime.now(); + } + + /** + * 获取时间 + * + * @return 14:29:40 + */ + public static LocalTime getLocalTimeNowOfSecond() { + return getLocalTimeNow().withNano(0); + } + + /** + * 获取指定时间 + * + * @param hour + * @param minute + * @param second + * @param nanoOfSecond + * @return + */ + public static LocalTime getLocalTime(int hour, int minute, int second, int nanoOfSecond) { + return LocalTime.of(hour, minute, second, nanoOfSecond); + } + + /** + * 获取时间和日期 + * + * @return 如:2016-11-06T15:20:27.996 + */ + public static LocalDateTime getLocalDateTimeNow() { + return LocalDateTime.now(); + } + + /** + * 获取时间和日期 + * + * @param year + * @param month + * @param dayOfMonth + * @param hour + * @param minute + * @param second + * @param nanoOfSecond + * @return + */ + public static LocalDateTime getLocalDateTime(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) { + return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond); + + + } + + /** + * LocalDateTime 格式化 + * + * @param localDateTime + * @param pattern + * @return + * @see DatePatternUtils + */ + public static String format(LocalDateTime localDateTime, String pattern) { + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); + return dateTimeFormatter.format(localDateTime); + } + + /** + * LocalDate 格式化 + * + * @param localDate + * @param pattern + * @return + */ + public static String format(LocalDate localDate, String pattern) { + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); + return dateTimeFormatter.format(localDate); + } + + /** + * 获取当前时间字符串 :2019-09-01 + * @return + */ + public static String formatDateNow() { + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DatePatternUtils.NORM_DATE_PATTERN); + return dateTimeFormatter.format(getLocalDateNow()); + } + + +} \ No newline at end of file diff --git a/src/main/java/com/irrigation/icl/utils/DoubleValueUtils.java b/src/main/java/com/irrigation/icl/utils/DoubleValueUtils.java new file mode 100644 index 0000000..86f7735 --- /dev/null +++ b/src/main/java/com/irrigation/icl/utils/DoubleValueUtils.java @@ -0,0 +1,184 @@ +package com.irrigation.icl.utils; + +import com.irrigation.icl.exception.ContextRuntimeException; + +import javax.annotation.Nonnull; +import java.math.BigDecimal; +import java.util.Objects; +import java.util.Optional; + +/** + * @description: 浮点数操作工具类 + * @Author: yangLang + * @CreateDate: 2020/4/29 16:09 + */ +public final class DoubleValueUtils { + + /** + * 默认保留位数 + */ + private final static int SCALE = 4; + + + /** + * Double 0.0 + */ + public final static Double DOUBLE_ZERO = Double.valueOf(0.0d); + + /** + * double 0.0 + */ + public final static double ZERO = 0.0d; + + + private DoubleValueUtils() { + } + + /** + * 计算两个浮点数之和s + * + * @param value 加数 + * @param addValue 被加数 + * @return 返回结果四舍五入,保留4位小数 + * @Author: yangLang + */ + public static double doubleAdd(Double value, Double addValue) { + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); + addValue = Optional.ofNullable(addValue).orElse(DOUBLE_ZERO); + return new BigDecimal(value.toString()).add(new BigDecimal(addValue.toString())) + .setScale(SCALE, BigDecimal.ROUND_HALF_UP) + .doubleValue(); + } + + /** + * 计算两个浮点数之和 + * + * @param value 加数 + * @param addValue 被加数 + * @param scale 保留位数 + * @return 返回结果四舍五入,保留4位小数 + * @Author: yangLang + */ + public static double doubleAdd(Double value, Double addValue, int scale) { + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); + addValue = Optional.ofNullable(addValue).orElse(DOUBLE_ZERO); + return new BigDecimal(value.toString()).add(new BigDecimal(addValue.toString())) + .setScale(scale, BigDecimal.ROUND_HALF_UP) + .doubleValue(); + } + + /** + * 计算两个浮点数之差 + * + * @param value 减数 + * @param subValue 被减数 + * @return 返回结果四舍五入,保留4位小数 + * @author: yangLang + */ + public static double doubleSub(Double value, Double subValue) { + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); + subValue = Optional.ofNullable(subValue).orElse(DOUBLE_ZERO); + return new BigDecimal(value.toString()).subtract(new BigDecimal(subValue.toString())) + .setScale(SCALE, BigDecimal.ROUND_HALF_UP) + .doubleValue(); + } + + /** + * 计算两个浮点数之差 + * + * @param value 减数 + * @param subValue 被减数 + * @param scale 保留位数 + * @return 返回结果四舍五入,保留4位小数 + * @author: yangLang + */ + public static double doubleSub(Double value, Double subValue, int scale) { + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); + subValue = Optional.ofNullable(subValue).orElse(DOUBLE_ZERO); + return new BigDecimal(value.toString()).subtract(new BigDecimal(subValue.toString())) + .setScale(scale, BigDecimal.ROUND_HALF_UP) + .doubleValue(); + } + + /** + * 计算两个浮点数之积 + * + * @param value 乘数 + * @param mutValue 被乘数 + * @return 返回结果四舍五入,保留4位小数 + * @author: yangLang + */ + public static double doubleMulti(Double value, Double mutValue) { + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); + mutValue = Optional.ofNullable(mutValue).orElse(DOUBLE_ZERO); + return new BigDecimal(value.toString()).multiply(new BigDecimal(mutValue.toString())) + .setScale(SCALE, BigDecimal.ROUND_HALF_UP) + .doubleValue(); + } + + /** + * 计算两个浮点数之积 + * + * @param value 乘数 + * @param mutValue 被乘数 + * @param scale 保留位数 + * @return 返回结果四舍五入,保留4位小数 + * @author: yangLang + */ + public static double doubleMulti(Double value, Double mutValue, int scale) { + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); + mutValue = Optional.ofNullable(mutValue).orElse(DOUBLE_ZERO); + return new BigDecimal(value.toString()).multiply(new BigDecimal(mutValue.toString())) + .setScale(scale, BigDecimal.ROUND_HALF_UP) + .doubleValue(); + } + + /** + * 计算两个浮点数之商 + * + * @param value 除数 + * @param divValue 被除数 + * @return 返回结果四舍五入,保留4位小数 + * @author: yangLang + */ + public static double doubleDiv(Double value, @Nonnull Double divValue) { + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); + validateDivValue(divValue); + return new BigDecimal(value.toString()).divide(new BigDecimal(divValue.toString())) + .setScale(SCALE, BigDecimal.ROUND_HALF_UP) + .doubleValue(); + } + + /** + * 计算两个浮点数之商 + * + * @param value 除数 + * @param divValue 被除数 + * @param scale 保留位数 + * @return 返回结果四舍五入,保留4位小数 + * @author: yangLang + */ + public static double doubleDiv(Double value, @Nonnull Double divValue, int scale) { + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); + validateDivValue(divValue); + return new BigDecimal(value.toString()).divide(new BigDecimal(divValue.toString())) + .setScale(scale, BigDecimal.ROUND_HALF_UP) + .doubleValue(); + } + + /** + * 检查除数是否为0 + * + * @param divValue + * @return + * @author: yangLang + */ + private static Double validateDivValue(@Nonnull Double divValue) { + Objects.requireNonNull(divValue, "被除数不能为空"); + if (ZERO == divValue.doubleValue()) { + throw new ContextRuntimeException("被除数不能为0"); + } + return divValue; + } + +} diff --git a/src/main/java/com/irrigation/icl/utils/ObjectUtils.java b/src/main/java/com/irrigation/icl/utils/ObjectUtils.java new file mode 100644 index 0000000..88c9bd2 --- /dev/null +++ b/src/main/java/com/irrigation/icl/utils/ObjectUtils.java @@ -0,0 +1,252 @@ +package com.irrigation.icl.utils; + +import com.google.common.collect.Lists; +import com.irrigation.icl.exception.ContextRuntimeException; +import org.springframework.beans.BeanUtils; + +import java.util.*; + +/** + * @author zhangchuanxi + * 对象工具类 + * @version 1.0.1 + */ +public class ObjectUtils { + + /** + * 复制对象错误提示 + */ + private final static String COPY_OBJECT_FAIL = "复制对象失败"; + + private ObjectUtils() { + } + + + /** + * 判断对象司是否是空 + * + * @param obj + * @return + */ + public static boolean isNull(Object obj) { + return obj == null; + } + + /** + * 判断对象司是否不是空 + * + * @param obj + * @return + */ + public static boolean nonNull(Object obj) { + return obj != null; + } + + /** + * 判断两个对象是否相等 + * + * @param a + * @param b + * @return + */ + public static boolean equals(Object a, Object b) { + return a != null && a.equals(b); + } + + /** + * 判断两个对象是否不相等 + * + * @param a + * @param b + * @return + */ + public static boolean unequal(Object a, Object b) { + return (a != null && !a.equals(b)) || (b != null && !b.equals(a)); + } + + /** + * 判断集合是否是空 + * + * @param collection + * @return + */ + public static boolean isEmpty(Collection collection) { + return collection == null || collection.isEmpty(); + } + + /** + * 判断集合是否不是空 + * + * @param collection + * @return + */ + public static boolean nonEmpty(Collection collection) { + return !isEmpty(collection); + } + + + /** + * 判断map 是否是空 + * + * @param map + * @return + */ + public static boolean isEmpty(Map map) { + return map == null || map.isEmpty(); + } + + /** + * 判断map 是否不是空 + * + * @param map + * @return + */ + public static boolean nonEmpty(Map map) { + return !isEmpty(map); + } + + + /** + * 判断字符串 是否是 null 或 length=0 + * + * @param cs + * @return + */ + public static boolean stringIsEmpty(CharSequence cs) { + return cs == null || cs.length() == 0; + } + + + /** + * 判断字符串 是否不是 null 并且 length!=0 + * + * @param cs + * @return + */ + public static boolean stringNonEmpty(CharSequence cs) { + return !stringIsEmpty(cs); + } + + /** + * 判断字符是否是空字符 + * + * @param cs + * @return + */ + public static boolean stringIsBlank(CharSequence cs) { + int strLen; + if (cs == null || (strLen = cs.length()) == 0) { + return true; + } + for (int i = 0; i < strLen; i++) { + if (!Character.isWhitespace(cs.charAt(i))) { + return false; + } + } + return true; + } + + /** + * 判断字符是否是空字符 + * + * @param cs + * @return + */ + public static boolean stringNonBlankB(CharSequence cs) { + return !stringIsBlank(cs); + } + + + /** + * 去掉字符串前后空白字符 + * + * @param str + * @return + */ + public static String stringTrim(final String str) { + return str == null ? null : str.trim(); + } + + /** + * 单个对象复制 + * + * @param source + * @param targetClass + * @param + * @return + */ + + public static T copyObject(Object source, Class targetClass) { + if (isNull(source)) { + return null; + } + try { + T target = targetClass.newInstance(); + BeanUtils.copyProperties(source, target); + return target; + } catch (Exception e) { + throw new ContextRuntimeException(COPY_OBJECT_FAIL, e); + } + } + + /** + * 集合复制 + * + * @param sourceList + * @param targetClass + * @param + * @return + */ + public static List copyObjectList(List sourceList, Class targetClass) { + List targetList = Lists.newArrayList(); + if (nonEmpty(sourceList)) { + for (S source : sourceList) { + targetList.add(copyObject(source, targetClass)); + } + } + return targetList; + } + + /** + * 集合通过连接符号返回字符串 + * + * @param iterable + * @param separator + * @return + */ + public static String stringJoin(Iterable iterable, String separator) { + return iterable == null ? null : stringJoin(iterable.iterator(), separator); + } + + private static String stringJoin(Iterator iterator, String separator) { + if (iterator == null) { + return null; + } else if (!iterator.hasNext()) { + return ""; + } else { + Object first = iterator.next(); + if (!iterator.hasNext()) { + return Objects.toString(first, ""); + } else { + StringBuilder buf = new StringBuilder(256); + if (first != null) { + buf.append(first); + } + + while (iterator.hasNext()) { + if (separator != null) { + buf.append(separator); + } + + Object obj = iterator.next(); + if (obj != null) { + buf.append(obj); + } + } + return buf.toString(); + } + } + } + + +} diff --git a/src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java b/src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java index 8bd3600..f3f1eda 100644 --- a/src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java +++ b/src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java @@ -1,18 +1,24 @@ package com.irrigation.icl.utils; -import org.springframework.security.authentication.encoding.BasePasswordEncoder; -import org.springframework.security.authentication.encoding.Md5PasswordEncoder; +import org.springframework.security.crypto.password.MessageDigestPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +/** + * @author zhangchuanxi + * md5加密工具类 + * @version 1.0.0 + */ public class PasswordEncoderUtil { - private final static BasePasswordEncoder encoder = new Md5PasswordEncoder(); + public static final PasswordEncoder ENCODER = new MessageDigestPasswordEncoder("MD5"); public static String encodePassword(String password) { - return encoder.encodePassword(password, null); + return ENCODER.encode(password); } public static boolean isPasswordValid(String rawPassword, String encodedPassword) { - return encoder.isPasswordValid(encodedPassword, rawPassword, null); + return ENCODER.matches(rawPassword, encodedPassword); } } diff --git a/src/main/java/com/irrigation/icl/utils/RestResultGeneratorUtil.java b/src/main/java/com/irrigation/icl/utils/RestResultGeneratorUtil.java index 621a3ca..3da8747 100644 --- a/src/main/java/com/irrigation/icl/utils/RestResultGeneratorUtil.java +++ b/src/main/java/com/irrigation/icl/utils/RestResultGeneratorUtil.java @@ -4,33 +4,39 @@ package com.irrigation.icl.utils; import com.irrigation.icl.entity.RestResult; import com.irrigation.icl.enums.ResultEnum; -public class RestResultGeneratorUtil { +/** + * @author zhangchuanxi + * @version 1.1.0 + * @Description api 公共返回类的工具类 + */ + +public final class RestResultGeneratorUtil { + + private RestResultGeneratorUtil() { + } public static RestResult getResult(int code, T data, String message) { RestResult result = new RestResult<>(code, message, data); return result; } - public static RestResult getSuccessResult() { + public static RestResult getSuccessResult() { return getResult(ResultEnum.SUCCESS.getCode(), null, ResultEnum.SUCCESS.getMessage()); } + public static RestResult getSuccessResult(T data) { return getResult(ResultEnum.SUCCESS.getCode(), data, ResultEnum.SUCCESS.getMessage()); } - public static RestResult getSuccessResult(T data,String message) { + public static RestResult getSuccessResult(T data, String message) { return getResult(ResultEnum.SUCCESS.getCode(), data, message); } - public static RestResult getSuccessResultMsg(String message) { + public static RestResult getSuccessResultMsg(String message) { return getResult(ResultEnum.SUCCESS.getCode(), null, message); } -// public static RestResult getErrorResult(String message) { -// return getResult(ResultEnum.ERROR.getCode(), null, message); -// } - public static RestResult getErrorResult(String message) { return getResult(ResultEnum.ERROR.getCode(), null, message); }