aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark/common/activitylog
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2021-03-23 00:52:44 +0000
committerLuck <git@lucko.me>2021-03-23 00:52:44 +0000
commit0fd7d30f2a9027c2bd9df3215759c6d91d110acc (patch)
treebdf02fa8b7c6dfc7dc6db5eaaabeb1e17189d420 /spark-common/src/main/java/me/lucko/spark/common/activitylog
parent9766754d28fcbca1ccbeefc11ef7a88a4e3d7946 (diff)
downloadspark-0fd7d30f2a9027c2bd9df3215759c6d91d110acc.tar.gz
spark-0fd7d30f2a9027c2bd9df3215759c6d91d110acc.tar.bz2
spark-0fd7d30f2a9027c2bd9df3215759c6d91d110acc.zip
Refactor and tidy up, more consistent code style
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/activitylog')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/activitylog/Activity.java111
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/activitylog/ActivityLog.java86
2 files changed, 111 insertions, 86 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/activitylog/Activity.java b/spark-common/src/main/java/me/lucko/spark/common/activitylog/Activity.java
new file mode 100644
index 0000000..561515a
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/common/activitylog/Activity.java
@@ -0,0 +1,111 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.activitylog;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonPrimitive;
+
+import me.lucko.spark.common.command.sender.CommandSender;
+
+import java.util.concurrent.TimeUnit;
+
+public final class Activity {
+ private final CommandSender.Data user;
+ private final long time;
+ private final String type;
+
+ private final String dataType;
+ private final String dataValue;
+
+ public static Activity urlActivity(CommandSender user, long time, String type, String url) {
+ return new Activity(user.toData(), time, type, "url", url);
+ }
+
+ public static Activity fileActivity(CommandSender user, long time, String type, String filePath) {
+ return new Activity(user.toData(), time, type, "file", filePath);
+ }
+
+ private Activity(CommandSender.Data user, long time, String type, String dataType, String dataValue) {
+ this.user = user;
+ this.time = time;
+ this.type = type;
+ this.dataType = dataType;
+ this.dataValue = dataValue;
+ }
+
+ public CommandSender.Data getUser() {
+ return this.user;
+ }
+
+ public long getTime() {
+ return this.time;
+ }
+
+ public String getType() {
+ return this.type;
+ }
+
+ public String getDataType() {
+ return this.dataType;
+ }
+
+ public String getDataValue() {
+ return this.dataValue;
+ }
+
+ public boolean shouldExpire() {
+ if (this.dataType.equals("url")) {
+ return (System.currentTimeMillis() - this.time) > TimeUnit.DAYS.toMillis(60);
+ } else {
+ return false;
+ }
+ }
+
+ public JsonObject serialize() {
+ JsonObject object = new JsonObject();
+
+ object.add("user", this.user.serialize());
+ object.add("time", new JsonPrimitive(this.time));
+ object.add("type", new JsonPrimitive(this.type));
+
+ JsonObject data = new JsonObject();
+ data.add("type", new JsonPrimitive(this.dataType));
+ data.add("value", new JsonPrimitive(this.dataValue));
+ object.add("data", data);
+
+ return object;
+ }
+
+ public static Activity deserialize(JsonElement element) {
+ JsonObject object = element.getAsJsonObject();
+
+ CommandSender.Data user = CommandSender.Data.deserialize(object.get("user"));
+ long time = object.get("time").getAsJsonPrimitive().getAsLong();
+ String type = object.get("type").getAsJsonPrimitive().getAsString();
+
+ JsonObject dataObject = object.get("data").getAsJsonObject();
+ String dataType = dataObject.get("type").getAsJsonPrimitive().getAsString();
+ String dataValue = dataObject.get("value").getAsJsonPrimitive().getAsString();
+
+ return new Activity(user, time, type, dataType, dataValue);
+ }
+}
diff --git a/spark-common/src/main/java/me/lucko/spark/common/activitylog/ActivityLog.java b/spark-common/src/main/java/me/lucko/spark/common/activitylog/ActivityLog.java
index b344ded..2693962 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/activitylog/ActivityLog.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/activitylog/ActivityLog.java
@@ -24,10 +24,7 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
-import com.google.gson.JsonPrimitive;
-import me.lucko.spark.common.command.sender.CommandSender;
import java.io.BufferedReader;
import java.io.BufferedWriter;
@@ -37,7 +34,6 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedList;
import java.util.List;
-import java.util.concurrent.TimeUnit;
public class ActivityLog {
@@ -132,86 +128,4 @@ public class ActivityLog {
}
}
- public static final class Activity {
- private final CommandSender.Data user;
- private final long time;
- private final String type;
-
- private final String dataType;
- private final String dataValue;
-
- public static Activity urlActivity(CommandSender user, long time, String type, String url) {
- return new Activity(user.toData(), time, type, "url", url);
- }
-
- public static Activity fileActivity(CommandSender user, long time, String type, String filePath) {
- return new Activity(user.toData(), time, type, "file", filePath);
- }
-
- private Activity(CommandSender.Data user, long time, String type, String dataType, String dataValue) {
- this.user = user;
- this.time = time;
- this.type = type;
- this.dataType = dataType;
- this.dataValue = dataValue;
- }
-
- public CommandSender.Data getUser() {
- return this.user;
- }
-
- public long getTime() {
- return this.time;
- }
-
- public String getType() {
- return this.type;
- }
-
- public String getDataType() {
- return this.dataType;
- }
-
- public String getDataValue() {
- return this.dataValue;
- }
-
- public boolean shouldExpire() {
- if (this.dataType.equals("url")) {
- return (System.currentTimeMillis() - this.time) > TimeUnit.DAYS.toMillis(60);
- } else {
- return false;
- }
- }
-
- public JsonObject serialize() {
- JsonObject object = new JsonObject();
-
- object.add("user", this.user.serialize());
- object.add("time", new JsonPrimitive(this.time));
- object.add("type", new JsonPrimitive(this.type));
-
- JsonObject data = new JsonObject();
- data.add("type", new JsonPrimitive(this.dataType));
- data.add("value", new JsonPrimitive(this.dataValue));
- object.add("data", data);
-
- return object;
- }
-
- public static Activity deserialize(JsonElement element) {
- JsonObject object = element.getAsJsonObject();
-
- CommandSender.Data user = CommandSender.Data.deserialize(object.get("user"));
- long time = object.get("time").getAsJsonPrimitive().getAsLong();
- String type = object.get("type").getAsJsonPrimitive().getAsString();
-
- JsonObject dataObject = object.get("data").getAsJsonObject();
- String dataType = dataObject.get("type").getAsJsonPrimitive().getAsString();
- String dataValue = dataObject.get("value").getAsJsonPrimitive().getAsString();
-
- return new Activity(user, time, type, dataType, dataValue);
- }
- }
-
}