mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-29 22:27:33 +00:00
[Add][Change] Core 增加 Converter 并增加对 FactoryName 不允许重复的限制;
[Add] GsonJsonObjectConverter, GsonJsonArrayConverter 增加对 Gson 中 JsonArray, JsonObject 的转换; [Change] CacheStoreBuilder, FactoryInfo 增加对 FactoryName 的不重复限制, 增加对 FactoryInfo 的 hashCode, equals 方法实现(以 FactoryName 字段为主); [Add] com.google.code.gson:gson:2.8.5 增加对 Gson 的依赖(解析数据, 后续可能会转移);
This commit is contained in:
parent
cd3d5e42eb
commit
7fcef96b0a
@ -45,6 +45,12 @@
|
||||
<version>3.0.0-alpha-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.lamgc</groupId>
|
||||
<artifactId>ContentGrabbingJi-CacheStore-local</artifactId>
|
||||
|
@ -58,6 +58,10 @@ public class CacheStoreBuilder {
|
||||
FactoryInfo info;
|
||||
try {
|
||||
info = new FactoryInfo(factory.getClass());
|
||||
if (FACTORY_INFO_MAP.containsValue(info)) {
|
||||
log.warn("发现 Name 重复的 Factory, 已跳过. (被拒绝的实现: {})", factory.getClass().getName());
|
||||
continue;
|
||||
}
|
||||
FACTORY_INFO_MAP.put(factory, info);
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.warn("Factory {} 加载失败: {}", factory.getClass().getName(), e.getMessage());
|
||||
|
@ -19,6 +19,8 @@ package net.lamgc.cgj.bot.cache;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* CacheStoreFactory 的标识信息.
|
||||
* @author LamGC
|
||||
@ -62,4 +64,21 @@ public class FactoryInfo {
|
||||
public int getFactoryPriority() {
|
||||
return factoryPriority;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
FactoryInfo that = (FactoryInfo) o;
|
||||
return factoryName.equals(that.factoryName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(factoryName);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2020 LamGC
|
||||
*
|
||||
* ContentGrabbingJi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* ContentGrabbingJi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.lamgc.cgj.bot.cache.converter;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import net.lamgc.cgj.bot.cache.convert.StringConverter;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Gson 的 JsonArray 转换器.
|
||||
* @author LamGC
|
||||
*/
|
||||
public class GsonJsonArrayConverter implements StringConverter<JsonArray> {
|
||||
|
||||
private final Gson gson;
|
||||
|
||||
/**
|
||||
* 使用默认配置的 Gson 对象创建转换器.
|
||||
*/
|
||||
public GsonJsonArrayConverter() {
|
||||
this(new Gson());
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用自定义的 Gson 对象创建转换器.
|
||||
* @param gson Gson 对象
|
||||
* @throws NullPointerException 如果 gson 参数传入 null, 则抛出该异常.
|
||||
*/
|
||||
public GsonJsonArrayConverter(Gson gson) {
|
||||
this.gson = Objects.requireNonNull(gson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String to(JsonArray source) {
|
||||
return gson.toJson(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonArray from(String target) {
|
||||
return gson.fromJson(target, JsonArray.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2020 LamGC
|
||||
*
|
||||
* ContentGrabbingJi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* ContentGrabbingJi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.lamgc.cgj.bot.cache.converter;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.lamgc.cgj.bot.cache.convert.StringConverter;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Gson 的 JsonObject 转换器.
|
||||
* @author LamGC
|
||||
*/
|
||||
public class GsonJsonObjectConverter implements StringConverter<JsonObject> {
|
||||
|
||||
private final Gson gson;
|
||||
|
||||
/**
|
||||
* 使用默认配置的 Gson 对象创建转换器.
|
||||
*/
|
||||
public GsonJsonObjectConverter() {
|
||||
this(new Gson());
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用自定义的 Gson 对象创建转换器.
|
||||
* @param gson Gson 对象
|
||||
* @throws NullPointerException 如果 gson 参数传入 null, 则抛出该异常.
|
||||
*/
|
||||
public GsonJsonObjectConverter(Gson gson) {
|
||||
this.gson = Objects.requireNonNull(gson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String to(JsonObject source) {
|
||||
return gson.toJson(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject from(String target) {
|
||||
return gson.fromJson(target, JsonObject.class);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user