diff --git a/ContentGrabbingJi-core/pom.xml b/ContentGrabbingJi-core/pom.xml index ff375b0..1ca81b2 100644 --- a/ContentGrabbingJi-core/pom.xml +++ b/ContentGrabbingJi-core/pom.xml @@ -45,6 +45,12 @@ 3.0.0-alpha-SNAPSHOT + + com.google.code.gson + gson + 2.8.5 + + net.lamgc ContentGrabbingJi-CacheStore-local diff --git a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreBuilder.java b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreBuilder.java index 4f4a516..bace79e 100644 --- a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreBuilder.java +++ b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreBuilder.java @@ -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()); diff --git a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java index 5a2c72e..e0eb95c 100644 --- a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java +++ b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java @@ -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); + } } diff --git a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/converter/GsonJsonArrayConverter.java b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/converter/GsonJsonArrayConverter.java new file mode 100644 index 0000000..830505a --- /dev/null +++ b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/converter/GsonJsonArrayConverter.java @@ -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 . + */ + +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 { + + 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); + } +} diff --git a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/converter/GsonJsonObjectConverter.java b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/converter/GsonJsonObjectConverter.java new file mode 100644 index 0000000..447d478 --- /dev/null +++ b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/converter/GsonJsonObjectConverter.java @@ -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 . + */ + +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 { + + 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); + } +}