add check

This commit is contained in:
Looly 2021-08-16 21:11:28 +08:00
parent efaa0a5ca1
commit f288c497b8
5 changed files with 71 additions and 7 deletions

View File

@ -3,9 +3,12 @@
-------------------------------------------------------------------------------------------------------------
# 5.7.9 (2021-08-12)
# 5.7.9 (2021-08-16)
### 🐣新特性
* 【extra 】 FileUtil增加moveContent方法issue#I45H30@Gitee
* 【extra 】 JschPool.getSession获取时检查是否连接状态issue#I45N5I@Gitee
*
### 🐞Bug修复
* 【extra 】 修复TinyPinyinEngine空构造造成可能的误判问题

View File

@ -1004,6 +1004,22 @@ public class FileUtil extends PathUtil {
move(src.toPath(), target.toPath(), isOverride);
}
/**
* 移动文件或者目录
*
* @param src 源文件或者目录
* @param target 目标文件或者目录
* @param isOverride 是否覆盖目标只有目标为文件才覆盖
* @throws IORuntimeException IO异常
* @see PathUtil#moveContent(Path, Path, boolean)
* @since 5.7.9
*/
public static void moveContent(File src, File target, boolean isOverride) throws IORuntimeException {
Assert.notNull(src, "Src file must be not null!");
Assert.notNull(target, "target file must be not null!");
moveContent(src.toPath(), target.toPath(), isOverride);
}
/**
* 修改文件或目录的文件名不变更路径只是简单修改文件名不保留扩展名<br>
*

View File

@ -210,6 +210,9 @@ public class PathUtil {
* @since 5.5.1
*/
public static Path copy(Path src, Path target, CopyOption... options) throws IORuntimeException {
Assert.notNull(src, "Src path must be not null !");
Assert.notNull(target, "Target path must be not null !");
if (isDirectory(src)) {
return copyContent(src, target.resolve(src.getFileName()), options);
}
@ -231,6 +234,9 @@ public class PathUtil {
* @since 5.5.1
*/
public static Path copyContent(Path src, Path target, CopyOption... options) throws IORuntimeException {
Assert.notNull(src, "Src path must be not null !");
Assert.notNull(target, "Target path must be not null !");
try {
Files.walkFileTree(src, new CopyVisitor(src, target, options));
} catch (IOException e) {
@ -452,7 +458,11 @@ public class PathUtil {
/**
* 移动文件或目录<br>
* 当目标是目录时会将源文件或文件夹整体移动至目标目录下<br>
* 例如move("/usr/aaa", "/usr/bbb")结果为"/usr/bbb/aaa"
* 例如
* <ul>
* <li>move("/usr/aaa/abc.txt", "/usr/bbb")结果为"/usr/bbb/abc.txt"</li>
* <li>move("/usr/aaa", "/usr/bbb")结果为"/usr/bbb/aaa"</li>
* </ul>
*
* @param src 源文件或目录路径
* @param target 目标路径如果为目录则移动到此目录下
@ -463,10 +473,31 @@ public class PathUtil {
public static Path move(Path src, Path target, boolean isOverride) {
Assert.notNull(src, "Src path must be not null !");
Assert.notNull(target, "Target path must be not null !");
final CopyOption[] options = isOverride ? new CopyOption[]{StandardCopyOption.REPLACE_EXISTING} : new CopyOption[]{};
if (isDirectory(target)) {
target = target.resolve(src.getFileName());
}
return moveContent(src, target, isOverride);
}
/**
* 移动文件或目录内容到目标目录中例如
* <ul>
* <li>moveContent("/usr/aaa/abc.txt", "/usr/bbb")结果为"/usr/bbb/abc.txt"</li>
* <li>moveContent("/usr/aaa", "/usr/bbb")结果为"/usr/bbb"</li>
* </ul>
*
* @param src 源文件或目录路径
* @param target 目标路径如果为目录则移动到此目录下
* @param isOverride 是否覆盖目标文件
* @return 目标文件Path
* @since 5.7.9
*/
public static Path moveContent(Path src, Path target, boolean isOverride) {
Assert.notNull(src, "Src path must be not null !");
Assert.notNull(target, "Target path must be not null !");
final CopyOption[] options = isOverride ? new CopyOption[]{StandardCopyOption.REPLACE_EXISTING} : new CopyOption[]{};
// 自动创建目标的父目录
mkParentDirs(target);
try {

View File

@ -10,6 +10,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Predicate;
/**
* 简单缓存无超时实现默认使用{@link WeakHashMap}实现缓存自动清理
@ -76,15 +77,28 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
* @return 值对象
*/
public V get(K key, Func0<V> supplier) {
return get(key, null, supplier);
}
/**
* 从缓存中获得对象当对象不在缓存中或已经过期返回Func0回调产生的对象
*
* @param key
* @param validPredicate 检查结果对象是否可用如是否断开连接等
* @param supplier 如果不存在回调方法或结果不可用用于生产值对象
* @return 值对象
* @since 5.7.9
*/
public V get(K key, Predicate<V> validPredicate, Func0<V> supplier) {
V v = get(key);
if(null == v && null != supplier){
if (null == v && null != supplier) {
//每个key单独获取一把锁降低锁的粒度提高并发能力see pr#1385@Github
final Lock keyLock = keyLockMap.computeIfAbsent(key, k -> new ReentrantLock());
keyLock.lock();
try {
// 双重检查防止在竞争锁的过程中已经有其它线程写入
v = cache.get(key);
if (null == v) {
if (null == v || (null != validPredicate && false == validPredicate.test(v))) {
try {
v = supplier.call();
} catch (Exception e) {

View File

@ -42,7 +42,7 @@ public enum JschSessionPool {
*/
public Session getSession(String sshHost, int sshPort, String sshUser, String sshPass) {
final String key = StrUtil.format("{}@{}:{}", sshUser, sshHost, sshPort);
return this.cache.get(key, ()-> JschUtil.openSession(sshHost, sshPort, sshUser, sshPass));
return this.cache.get(key, Session::isConnected, ()-> JschUtil.openSession(sshHost, sshPort, sshUser, sshPass));
}
/**
@ -57,7 +57,7 @@ public enum JschSessionPool {
*/
public Session getSession(String sshHost, int sshPort, String sshUser, String prvkey, byte[] passphrase) {
final String key = StrUtil.format("{}@{}:{}", sshUser, sshHost, sshPort);
return this.cache.get(key, ()->JschUtil.openSession(sshHost, sshPort, sshUser, prvkey, passphrase));
return this.cache.get(key, Session::isConnected, ()->JschUtil.openSession(sshHost, sshPort, sshUser, prvkey, passphrase));
}
/**