From 710654cd061793da01a41043e88847f45be352ae Mon Sep 17 00:00:00 2001 From: Keir Date: Fri, 8 Jan 2016 15:06:13 +0000 Subject: [PATCH] Use Validate class from Apache Commons to minify code. --- .../src/mineplex/chatsnap/FilePurger.java | 19 +++++------------- .../chatsnap/RedisCommandHandler.java | 20 ++++--------------- .../chatsnap/RedisConnectionHandler.java | 8 +++----- .../src/mineplex/chatsnap/ReportServer.java | 16 +++++++-------- Plugins/Mineplex.ReportServer/web/view.php | 2 +- 5 files changed, 20 insertions(+), 45 deletions(-) diff --git a/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/FilePurger.java b/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/FilePurger.java index 21aea0bb8..2d3e65ac0 100644 --- a/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/FilePurger.java +++ b/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/FilePurger.java @@ -5,6 +5,8 @@ import java.io.FileFilter; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; +import org.apache.commons.lang3.Validate; + /** * @author iKeirNez */ @@ -20,20 +22,9 @@ public class FilePurger implements Runnable _dataDir = dataDir; _logger = logger; - if (_dataDir == null) - { - throw new IllegalArgumentException("dataDir cannot be null."); - } - - if (!_dataDir.exists() || !_dataDir.isDirectory()) - { - throw new IllegalArgumentException("dataDir must exist and be a directory."); - } - - if (_logger == null) - { - throw new IllegalArgumentException("Logger cannot be null."); - } + Validate.notNull(_dataDir, "Data directory cannot be null."); + Validate.isTrue(_dataDir.exists() && dataDir.isDirectory(), "Path non-existant or not a directory: %s", _dataDir.getAbsolutePath()); + Validate.notNull(_logger, "Logger cannot be null."); } @Override diff --git a/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/RedisCommandHandler.java b/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/RedisCommandHandler.java index 7b9d18208..d05ef3194 100644 --- a/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/RedisCommandHandler.java +++ b/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/RedisCommandHandler.java @@ -3,13 +3,11 @@ package mineplex.chatsnap; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; -import java.io.IOException; import java.nio.file.Files; import java.util.Arrays; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.ThreadPoolExecutor; import java.util.logging.Level; import java.util.logging.Logger; @@ -18,6 +16,7 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import org.apache.commons.lang3.Validate; import redis.clients.jedis.JedisPubSub; /** @@ -41,20 +40,9 @@ public class RedisCommandHandler extends JedisPubSub _directory = directory; _logger = logger; - if (_directory == null) - { - throw new IllegalArgumentException("Directory cannot be null."); - } - - if (!directory.exists() || !directory.isDirectory()) - { - throw new IllegalArgumentException("Path non-existent or not a directory: " + directory.getAbsolutePath()); - } - - if (_logger == null) - { - throw new IllegalArgumentException("Logger cannot be null."); - } + Validate.notNull(_directory, "Directory cannot be null."); + Validate.isTrue(directory.exists() && directory.isDirectory(), "Path non-existent or not a directory: %s", directory.getPath()); + Validate.notNull(_logger, "Logger cannot be null."); } @Override diff --git a/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/RedisConnectionHandler.java b/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/RedisConnectionHandler.java index 4f4811745..c851d851e 100644 --- a/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/RedisConnectionHandler.java +++ b/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/RedisConnectionHandler.java @@ -3,6 +3,7 @@ package mineplex.chatsnap; import java.util.logging.Level; import java.util.logging.Logger; +import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.time.DurationFormatUtils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @@ -23,16 +24,13 @@ public class RedisConnectionHandler implements Runnable public RedisConnectionHandler(String name, JedisPool jedisPool, RedisCommandHandler handler, String[] channels, Logger logger) { - if (channels.length == 0) - { - throw new IllegalArgumentException("Must provide at least one channel."); - } - _name = name; _jedisPool = jedisPool; _handler = handler; _channels = channels; _logger = logger; + + Validate.isTrue(channels.length > 0, "Must provide at least one channel."); } @Override diff --git a/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/ReportServer.java b/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/ReportServer.java index bdc820554..371b19eb8 100644 --- a/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/ReportServer.java +++ b/Plugins/Mineplex.ReportServer/src/mineplex/chatsnap/ReportServer.java @@ -17,6 +17,7 @@ import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; +import org.apache.commons.lang3.Validate; import redis.clients.jedis.JedisPool; /** @@ -77,18 +78,15 @@ public class ReportServer _dataDirectory = dataDirectory; _logger = logger; - if (_dataDirectory.exists() && !_dataDirectory.isDirectory()) - { - throw new RuntimeException("Not a directory: " + _dataDirectory.getPath()); - } + Validate.notNull(_dataDirectory, "Data directory cannot be null."); - if (!_dataDirectory.exists() && !_dataDirectory.mkdir()) - { - throw new RuntimeException("Unable to create directory: " + _dataDirectory.getPath()); - } + // thrown if path exists but is not a directory + Validate.isTrue(!_dataDirectory.exists() || _dataDirectory.isDirectory(), "Not a directory: %S", _dataDirectory.getPath()); + + // throws if directory doesn't exist and cannot be created + Validate.isTrue(_dataDirectory.exists() || _dataDirectory.mkdir(), "Unable to create directory: " + _dataDirectory.getPath()); _handler = new RedisCommandHandler(_dataDirectory, _logger); - initializeConnectionsConfig(redisConfig); schedulePurgeTask(); } diff --git a/Plugins/Mineplex.ReportServer/web/view.php b/Plugins/Mineplex.ReportServer/web/view.php index 417849206..feb173565 100644 --- a/Plugins/Mineplex.ReportServer/web/view.php +++ b/Plugins/Mineplex.ReportServer/web/view.php @@ -352,7 +352,7 @@ if ($validIdentifier) $handler = $report->getHandler(); echo $handler != null ? "Staff Member assigned is " . $handler->getUsername() : "No Staff Member assigned"; ?> - +