From 7fcef96b0a9b59bf583b186ed05a9b8fcd400014 Mon Sep 17 00:00:00 2001 From: LamGC Date: Mon, 28 Sep 2020 23:02:50 +0800 Subject: [PATCH] =?UTF-8?q?[Add][Change]=20Core=20=E5=A2=9E=E5=8A=A0=20Con?= =?UTF-8?q?verter=20=E5=B9=B6=E5=A2=9E=E5=8A=A0=E5=AF=B9=20FactoryName=20?= =?UTF-8?q?=E4=B8=8D=E5=85=81=E8=AE=B8=E9=87=8D=E5=A4=8D=E7=9A=84=E9=99=90?= =?UTF-8?q?=E5=88=B6;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Add] GsonJsonObjectConverter, GsonJsonArrayConverter 增加对 Gson 中 JsonArray, JsonObject 的转换; [Change] CacheStoreBuilder, FactoryInfo 增加对 FactoryName 的不重复限制, 增加对 FactoryInfo 的 hashCode, equals 方法实现(以 FactoryName 字段为主); [Add] com.google.code.gson:gson:2.8.5 增加对 Gson 的依赖(解析数据, 后续可能会转移); --- ContentGrabbingJi-core/pom.xml | 6 ++ .../cgj/bot/cache/CacheStoreBuilder.java | 4 ++ .../net/lamgc/cgj/bot/cache/FactoryInfo.java | 19 ++++++ .../converter/GsonJsonArrayConverter.java | 59 +++++++++++++++++++ .../converter/GsonJsonObjectConverter.java | 59 +++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/converter/GsonJsonArrayConverter.java create mode 100644 ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/converter/GsonJsonObjectConverter.java 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); + } +}