Skip to content

Commit

Permalink
feat(核心 / 工具箱): 将工具箱大部分功能移至核心
Browse files Browse the repository at this point in the history
  • Loading branch information
houtaroy committed Nov 16, 2023
1 parent 7e8c53e commit 5d10374
Show file tree
Hide file tree
Showing 51 changed files with 327 additions and 81 deletions.
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);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.koala.cache.interceptor;

import cn.koala.toolkit.ArrayHelper;
import cn.koala.util.ObjectUtils;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
Expand All @@ -24,8 +24,8 @@ public class ListKeyGenerator implements KeyGenerator {
@SuppressWarnings("unchecked")
public Object generate(@NonNull Object target, @NonNull Method method, @NonNull Object... params) {
return LIST_KEY_TEMPLATE.formatted(
generateParameters(ArrayHelper.get(params, Map.class)),
generatePageable(ArrayHelper.get(params, Pageable.class))
generateParameters(ObjectUtils.getFirst(params, Map.class)),
generatePageable(ObjectUtils.getFirst(params, Pageable.class))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cn.koala.cache.support;

import cn.koala.cache.CacheCondition;
import cn.koala.cache.CacheConditionRegistration;
import cn.koala.cache.CacheConditionRegistry;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.Cache;
Expand Down Expand Up @@ -29,7 +28,6 @@ public class CompositeCacheCondition implements CacheCondition {
public boolean matches(Object target, Method method, Collection<Cache> caches, Object... params) {
Set<String> cacheNames = caches.stream().map(Cache::getName).collect(Collectors.toSet());
return registry.get(cacheNames)
.map(CacheConditionRegistration::getCacheCondition)
.map(condition -> condition.matches(target, method, caches, params))
.orElse(true);
}
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.koala.cache.support;

import cn.koala.cache.CacheCondition;
import cn.koala.toolkit.ArrayHelper;
import cn.koala.util.ObjectUtils;
import org.springframework.cache.Cache;
import org.springframework.util.CollectionUtils;

Expand Down Expand Up @@ -30,7 +30,7 @@ public SelectiveListCacheCondition(String... parameterName) {
@Override
@SuppressWarnings("unchecked")
public boolean matches(Object target, Method method, Collection<Cache> caches, Object... params) {
Map<String, Object> parameters = ArrayHelper.get(params, Map.class);
Map<String, Object> parameters = ObjectUtils.getFirst(params, Map.class);
return !CollectionUtils.isEmpty(parameters) && parameters.keySet().containsAll(parameterNames);
}
}
4 changes: 4 additions & 0 deletions koala-domains/koala-code-gen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<groupId>cn.koala</groupId>
<artifactId>koala-web</artifactId>
</dependency>
<dependency>
<groupId>cn.koala</groupId>
<artifactId>koala-toolkit</artifactId>
</dependency>

<dependency>
<groupId>com.jfinal</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import cn.koala.exception.BusinessException;
import cn.koala.template.Template;
import cn.koala.template.TemplateGroupService;
import cn.koala.toolkit.CompressHelper;
import cn.koala.util.BusinessAssert;
import cn.koala.util.CompressUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
Expand Down Expand Up @@ -43,7 +43,7 @@ public String download(Long databaseId, List<String> tableNames, Long templateGr
try {
File root = preview2File(preview(databaseId, tableNames, templateGroupId));
String result = root.getName() + "." + ArchiveStreamFactory.ZIP;
CompressHelper.compress(root, new File(downloadPath + result), ArchiveStreamFactory.ZIP);
CompressUtils.compress(root, new File(downloadPath + result), ArchiveStreamFactory.ZIP);
return result;
} catch (Exception e) {
throw new BusinessException("生成代码文件失败", e);
Expand Down
19 changes: 19 additions & 0 deletions koala-domains/koala-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,24 @@
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
</dependency>
</dependencies>
</project>
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();
}
}
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();
}
}
}
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);
}
}
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);
}
}
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);
}
}
Loading

0 comments on commit 5d10374

Please sign in to comment.