diff options
author | Luck <git@lucko.me> | 2024-07-29 18:33:08 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2024-07-29 18:33:08 +0100 |
commit | 60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7 (patch) | |
tree | 2bf8fcf914ac57466549d35dcd89ef96d3a2d65f /spark-common/src/main/java/me/lucko/spark/common/util | |
parent | 4c0149b6a15fa887328bbd88c8055c2138cc4d72 (diff) | |
download | spark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.tar.gz spark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.tar.bz2 spark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.zip |
Add some unit tests
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/util')
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java | 5 | ||||
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java | 132 | ||||
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java | 54 | ||||
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/util/config/FileConfiguration.java (renamed from spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java) | 18 | ||||
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java | 106 |
5 files changed, 311 insertions, 4 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java b/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java index efeabfc..1dad75b 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java @@ -20,6 +20,8 @@ package me.lucko.spark.common.util; +import com.google.common.annotations.VisibleForTesting; + public class JavaVersion { ; @@ -28,7 +30,8 @@ public class JavaVersion { JAVA_VERSION = parseJavaVersion(System.getProperty("java.version")); } - private static int parseJavaVersion(String version) { + @VisibleForTesting + static int parseJavaVersion(String version) { if (version.startsWith("1.")) { // Java 8 and below return Integer.parseInt(version.substring(2, 3)); diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java new file mode 100644 index 0000000..ff7388a --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java @@ -0,0 +1,132 @@ +/* + * 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.util.config; + +import com.google.common.collect.ImmutableList; + +import java.util.Collections; +import java.util.List; + +class CombinedConfiguration implements Configuration { + + private final List<Configuration> configurations; + + CombinedConfiguration(Configuration... configurations) { + this.configurations = ImmutableList.copyOf(configurations).reverse(); + } + + @Override + public void load() { + for (Configuration configuration : this.configurations) { + configuration.load(); + } + } + + @Override + public void save() { + for (Configuration configuration : this.configurations) { + configuration.save(); + } + } + + @Override + public String getString(String path, String def) { + String result = def; + for (Configuration configuration : this.configurations) { + result = configuration.getString(path, result); + } + return result; + } + + @Override + public boolean getBoolean(String path, boolean def) { + boolean result = def; + for (Configuration configuration : this.configurations) { + result = configuration.getBoolean(path, result); + } + return result; + } + + @Override + public int getInteger(String path, int def) { + int result = def; + for (Configuration configuration : this.configurations) { + result = configuration.getInteger(path, result); + } + return result; + } + + @Override + public List<String> getStringList(String path) { + for (Configuration configuration : this.configurations) { + List<String> result = configuration.getStringList(path); + if (!result.isEmpty()) { + return result; + } + } + return Collections.emptyList(); + } + + @Override + public void setString(String path, String value) { + for (Configuration configuration : this.configurations) { + configuration.setString(path, value); + } + } + + @Override + public void setBoolean(String path, boolean value) { + for (Configuration configuration : this.configurations) { + configuration.setBoolean(path, value); + } + } + + @Override + public void setInteger(String path, int value) { + for (Configuration configuration : this.configurations) { + configuration.setInteger(path, value); + } + } + + @Override + public void setStringList(String path, List<String> value) { + for (Configuration configuration : this.configurations) { + configuration.setStringList(path, value); + } + } + + @Override + public boolean contains(String path) { + for (Configuration configuration : this.configurations) { + if (configuration.contains(path)) { + return true; + } + } + return false; + } + + @Override + public void remove(String path) { + for (Configuration configuration : this.configurations) { + configuration.remove(path); + } + } +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java new file mode 100644 index 0000000..c2c2d88 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java @@ -0,0 +1,54 @@ +/* + * 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.util.config; + +import java.util.List; + +public interface Configuration { + + static Configuration combining(Configuration... configurations) { + return new CombinedConfiguration(configurations); + } + + void load(); + + void save(); + + String getString(String path, String def); + + boolean getBoolean(String path, boolean def); + + int getInteger(String path, int def); + + List<String> getStringList(String path); + + void setString(String path, String value); + + void setBoolean(String path, boolean value); + + void setInteger(String path, int value); + + void setStringList(String path, List<String> value); + + boolean contains(String path); + + void remove(String path); +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/FileConfiguration.java index 586a845..72a4681 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/FileConfiguration.java @@ -18,7 +18,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package me.lucko.spark.common.util; +package me.lucko.spark.common.util.config; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -37,17 +37,18 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -public final class Configuration { +public class FileConfiguration implements Configuration { private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); private final Path file; private JsonObject root; - public Configuration(Path file) { + public FileConfiguration(Path file) { this.file = file; load(); } + @Override public void load() { JsonObject root = null; if (Files.exists(this.file)) { @@ -64,6 +65,7 @@ public final class Configuration { this.root = root; } + @Override public void save() { try { Files.createDirectories(this.file.getParent()); @@ -78,6 +80,7 @@ public final class Configuration { } } + @Override public String getString(String path, String def) { JsonElement el = this.root.get(path); if (el == null || !el.isJsonPrimitive()) { @@ -87,6 +90,7 @@ public final class Configuration { return el.getAsJsonPrimitive().getAsString(); } + @Override public boolean getBoolean(String path, boolean def) { JsonElement el = this.root.get(path); if (el == null || !el.isJsonPrimitive()) { @@ -97,6 +101,7 @@ public final class Configuration { return val.isBoolean() ? val.getAsBoolean() : def; } + @Override public int getInteger(String path, int def) { JsonElement el = this.root.get(path); if (el == null || !el.isJsonPrimitive()) { @@ -107,6 +112,7 @@ public final class Configuration { return val.isNumber() ? val.getAsInt() : def; } + @Override public List<String> getStringList(String path) { JsonElement el = this.root.get(path); if (el == null || !el.isJsonArray()) { @@ -122,18 +128,22 @@ public final class Configuration { return list; } + @Override public void setString(String path, String value) { this.root.add(path, new JsonPrimitive(value)); } + @Override public void setBoolean(String path, boolean value) { this.root.add(path, new JsonPrimitive(value)); } + @Override public void setInteger(String path, int value) { this.root.add(path, new JsonPrimitive(value)); } + @Override public void setStringList(String path, List<String> value) { JsonArray array = new JsonArray(); for (String str : value) { @@ -142,10 +152,12 @@ public final class Configuration { this.root.add(path, array); } + @Override public boolean contains(String path) { return this.root.has(path); } + @Override public void remove(String path) { this.root.remove(path); } diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java new file mode 100644 index 0000000..d076554 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java @@ -0,0 +1,106 @@ +/* + * 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.util.config; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public enum RuntimeConfiguration implements Configuration { + SYSTEM_PROPERTIES { + @Override + public String getString(String path, String def) { + return System.getProperty("spark." + path, def); + } + }, + + ENVIRONMENT_VARIABLES { + @Override + public String getString(String path, String def) { + String name = "SPARK_" + path.replace(".", "_").replace("-", "_").toUpperCase(); + String value = System.getenv(name); + return value != null ? value : def; + } + }; + + @Override + public boolean getBoolean(String path, boolean def) { + return Boolean.parseBoolean(getString(path, Boolean.toString(def))); + } + + @Override + public int getInteger(String path, int def) { + try { + return Integer.parseInt(getString(path, Integer.toString(def))); + } catch (NumberFormatException e) { + return def; + } + } + + @Override + public List<String> getStringList(String path) { + String value = getString(path, ""); + if (value.isEmpty()) { + return Collections.emptyList(); + } + return Arrays.asList(value.split(",")); + } + + @Override + public boolean contains(String path) { + return getString(path, null) != null; + } + + @Override + public void load() { + // no-op + } + + @Override + public void save() { + // no-op + } + + @Override + public void setString(String path, String value) { + // no-op + } + + @Override + public void setBoolean(String path, boolean value) { + // no-op + } + + @Override + public void setInteger(String path, int value) { + // no-op + } + + @Override + public void setStringList(String path, List<String> value) { + // no-op + } + + @Override + public void remove(String path) { + // no-op + } +} |