-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
327 additions
and
81 deletions.
There are no files selected for viewing
7 changes: 4 additions & 3 deletions
7
koala-domains/koala-cache/src/main/java/cn/koala/cache/CacheConditionRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
package cn.koala.cache; | ||
|
||
import cn.koala.toolkit.registry.Registry; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
/** | ||
* 缓存条件注册表 | ||
* | ||
* @author Houtaroy | ||
*/ | ||
public interface CacheConditionRegistry extends Registry<Set<String>, CacheConditionRegistration> { | ||
public interface CacheConditionRegistry { | ||
|
||
Optional<CacheCondition> get(Set<String> cacheNames); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 11 additions & 6 deletions
17
...mains/koala-cache/src/main/java/cn/koala/cache/support/DefaultCacheConditionRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,31 @@ | ||
package cn.koala.cache.support; | ||
|
||
import cn.koala.cache.CacheCondition; | ||
import cn.koala.cache.CacheConditionRegistration; | ||
import cn.koala.cache.CacheConditionRegistry; | ||
import cn.koala.toolkit.registry.AbstractCacheableRegistry; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
/** | ||
* 默认缓存条件注册表 | ||
* | ||
* @author Houtaroy | ||
*/ | ||
public class DefaultCacheConditionRegistry extends AbstractCacheableRegistry<Set<String>, CacheConditionRegistration> | ||
implements CacheConditionRegistry { | ||
public class DefaultCacheConditionRegistry implements CacheConditionRegistry { | ||
|
||
private final List<CacheConditionRegistration> registrations; | ||
|
||
public DefaultCacheConditionRegistry(List<CacheConditionRegistration> registrations) { | ||
super(registrations); | ||
this.registrations = registrations; | ||
} | ||
|
||
@Override | ||
protected boolean matches(Set<String> key, CacheConditionRegistration registration) { | ||
return registration.getCacheNames().containsAll(key); | ||
public Optional<CacheCondition> get(Set<String> cacheNames) { | ||
return registrations.stream() | ||
.filter(registration -> registration.getCacheNames().containsAll(cacheNames)) | ||
.findFirst() | ||
.map(CacheConditionRegistration::getCacheCondition); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
koala-domains/koala-core/src/main/java/cn/koala/util/ClassUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cn.koala.util; | ||
|
||
import com.google.common.reflect.TypeToken; | ||
import org.springframework.lang.Nullable; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* 类工具类 | ||
* | ||
* @author Houtaroy | ||
*/ | ||
public abstract class ClassUtils extends org.springframework.util.ClassUtils { | ||
|
||
@Nullable | ||
public static List<Class<?>> getGenericClasses(Class<?> objectClass, Class<?> genericClass) { | ||
if (!genericClass.isAssignableFrom(objectClass)) { | ||
return null; | ||
} | ||
if (ObjectUtils.isEmpty(genericClass.getTypeParameters())) { | ||
return null; | ||
} | ||
TypeToken<?> typeToken = TypeToken.of(objectClass); | ||
List<Class<?>> result = new ArrayList<>(genericClass.getTypeParameters().length); | ||
Arrays.stream(genericClass.getTypeParameters()) | ||
.map(typeToken::resolveType) | ||
.map(TypeToken::getType) | ||
.forEach(type -> result.add((Class<?>) type)); | ||
return result; | ||
} | ||
|
||
public static Class<?> getGenericClass(Class<?> objectClass, Class<?> genericClass) { | ||
return getGenericClass(objectClass, genericClass, 0); | ||
} | ||
|
||
public static Class<?> getGenericClass(Class<?> objectClass, Class<?> genericClass, int index) { | ||
if (!genericClass.isAssignableFrom(objectClass)) { | ||
return null; | ||
} | ||
if (ObjectUtils.isEmpty(genericClass.getTypeParameters())) { | ||
return null; | ||
} | ||
if (genericClass.getTypeParameters().length < index) { | ||
return null; | ||
} | ||
TypeToken<?> typeToken = TypeToken.of(objectClass); | ||
return typeToken.resolveType(genericClass.getTypeParameters()[index]).getRawType(); | ||
} | ||
|
||
public static List<Method> getMethods(Class<?> clazz, Class<? extends Annotation> annotation) { | ||
return Arrays.stream(clazz.getMethods()) | ||
.filter(method -> method.isAnnotationPresent(annotation)) | ||
.toList(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
koala-domains/koala-core/src/main/java/cn/koala/util/CompressUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cn.koala.util; | ||
|
||
import org.apache.commons.compress.archivers.ArchiveEntry; | ||
import org.apache.commons.compress.archivers.ArchiveException; | ||
import org.apache.commons.compress.archivers.ArchiveOutputStream; | ||
import org.apache.commons.compress.archivers.ArchiveStreamFactory; | ||
import org.apache.commons.io.FileUtils; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.ObjectUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
/** | ||
* 压缩工具类 | ||
* | ||
* @author Houtaroy | ||
*/ | ||
public abstract class CompressUtils { | ||
|
||
public static void compress(@NonNull File source, @NonNull File dest, @NonNull String archiverName) | ||
throws IOException, ArchiveException { | ||
|
||
Assert.notNull(source, "源文件不能为空"); | ||
Assert.notNull(dest, "目标文件不能为空"); | ||
Assert.hasLength(archiverName, "压缩类型不能为空"); | ||
Assert.isTrue(source.exists(), "目标文件不存在"); | ||
try (ArchiveOutputStream output = createArchiveOutputStream(dest, archiverName)) { | ||
compress(output, source, ""); | ||
} | ||
} | ||
|
||
private static ArchiveOutputStream createArchiveOutputStream(File file, String archiverName) | ||
throws IOException, ArchiveException { | ||
|
||
return ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(archiverName, Files.newOutputStream(file.toPath())); | ||
} | ||
|
||
private static void compress(ArchiveOutputStream output, File source, String path) throws IOException { | ||
String filePath = StringUtils.hasLength(path) ? path + source.getName() : source.getName(); | ||
if (source.isDirectory()) { | ||
File[] children = source.listFiles(); | ||
if (ObjectUtils.isEmpty(children)) { | ||
ArchiveEntry entry = output.createArchiveEntry(source, filePath); | ||
output.putArchiveEntry(entry); | ||
output.closeArchiveEntry(); | ||
} else { | ||
for (File child : children) { | ||
compress(output, child, filePath + File.separator); | ||
} | ||
} | ||
} else { | ||
ArchiveEntry entry = output.createArchiveEntry(source, filePath); | ||
output.putArchiveEntry(entry); | ||
byte[] bs = FileUtils.readFileToByteArray(source); | ||
output.write(bs); | ||
output.closeArchiveEntry(); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
koala-domains/koala-core/src/main/java/cn/koala/util/FileTypeUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cn.koala.util; | ||
|
||
import org.apache.tika.Tika; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* 文件类型工具类 | ||
* | ||
* @author Houtaroy | ||
*/ | ||
public abstract class FileTypeUtils { | ||
|
||
private static final Tika INSTANCE = new Tika(); | ||
|
||
public static boolean isImage(File file) throws IOException { | ||
return isImage(new FileInputStream(file)); | ||
} | ||
|
||
public static boolean isPdf(File file) throws IOException { | ||
return isPdf(new FileInputStream(file)); | ||
} | ||
|
||
public static boolean isImage(InputStream inputStream) throws IOException { | ||
return isType(inputStream, "image"); | ||
} | ||
|
||
public static boolean isPdf(InputStream inputStream) throws IOException { | ||
return isType(inputStream, "application/pdf"); | ||
} | ||
|
||
public static boolean isType(File file, String typePrefix) throws IOException { | ||
return isType(new FileInputStream(file), typePrefix); | ||
} | ||
|
||
public static boolean isType(InputStream inputStream, String typePrefix) throws IOException { | ||
return INSTANCE.detect(inputStream).startsWith(typePrefix); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
koala-domains/koala-core/src/main/java/cn/koala/util/LocalDateTimeUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cn.koala.util; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.util.Date; | ||
|
||
/** | ||
* LocalDateTime工具类 | ||
* | ||
* @author Houtaroy | ||
*/ | ||
public abstract class LocalDateTimeUtils { | ||
|
||
public static LocalDateTime from(Date date) { | ||
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); | ||
} | ||
|
||
public static Date toDate() { | ||
return toDate(LocalDateTime.now()); | ||
} | ||
|
||
public static Date toDate(LocalDateTime localDateTime) { | ||
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); | ||
} | ||
|
||
public static LocalDateTime atEndOfDay() { | ||
return atEndOfDay(LocalDateTime.now()); | ||
} | ||
|
||
public static LocalDateTime atEndOfDay(LocalDateTime localDateTime) { | ||
return localDateTime.toLocalDate().atStartOfDay().plusDays(1).minusSeconds(1); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
koala-domains/koala-core/src/main/java/cn/koala/util/ObjectUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cn.koala.util; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* 对象工具类 | ||
* | ||
* @author Houtaroy | ||
*/ | ||
public abstract class ObjectUtils extends org.springframework.util.ObjectUtils { | ||
|
||
public static <T> T getFirst(Object[] objects, Class<T> clazz) { | ||
return Arrays.stream(objects) | ||
.filter(param -> clazz.isAssignableFrom(param.getClass())) | ||
.findFirst() | ||
.map(clazz::cast) | ||
.orElse(null); | ||
} | ||
} |
Oops, something went wrong.