summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/misc
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-04-13 18:40:41 -0400
committerBen Culkin <scorpress@gmail.com>2020-04-13 18:40:41 -0400
commitd4ca769e542b2489b1e23cfcbdc3a0b7275b87cd (patch)
tree1653a7399f97fb0c63ce62e3f60fd830d5c37f70 /base/src/main/java/bjc/utils/misc
parent2ac2e31a56ae59ee582e43a90c3495f86dd9ee7a (diff)
Cleanup pass
Cleanup pass to uniformize things
Diffstat (limited to 'base/src/main/java/bjc/utils/misc')
-rw-r--r--base/src/main/java/bjc/utils/misc/Direction.java154
-rw-r--r--base/src/main/java/bjc/utils/misc/PropertyDB.java57
-rw-r--r--base/src/main/java/bjc/utils/misc/RelativeDirection.java54
3 files changed, 128 insertions, 137 deletions
diff --git a/base/src/main/java/bjc/utils/misc/Direction.java b/base/src/main/java/bjc/utils/misc/Direction.java
index b8a982e..6455d8d 100644
--- a/base/src/main/java/bjc/utils/misc/Direction.java
+++ b/base/src/main/java/bjc/utils/misc/Direction.java
@@ -11,10 +11,10 @@ import bjc.funcdata.IList;
/**
* A set of cardinal directions
- *
- * The particular axis assigned to the coordinates are based off of x being
- * the bottom left axis on a conventional 3D plot
- *
+ *
+ * The particular axis assigned to the coordinates are based off of x being the
+ * bottom left axis on a conventional 3D plot
+ *
* @author ben
*
*/
@@ -51,7 +51,7 @@ public enum Direction {
/**
* Get a list of all the cardinal directions
- *
+ *
* @return A list of all the cardinal directions
*/
public static IList<Direction> cardinals() {
@@ -59,9 +59,8 @@ public enum Direction {
}
/**
- * Perform the specified action once with each of the cardinal
- * directions
- *
+ * Perform the specified action once with each of the cardinal directions
+ *
* @param act
* The action to perform for each cardinal direction
*/
@@ -71,15 +70,14 @@ public enum Direction {
/**
* Perform a specified action for a random number of cardinals.
- *
+ *
* @param nCardinals
- * The number of cardinal directions to act on. Must be
- * greater than 0 and less then 5
+ * The number of cardinal directions to act on. Must be
+ * greater than 0 and less then 5
* @param act
- * The action to perform for each of the cardinals
+ * The action to perform for each of the cardinals
*/
- public static void forRandomCardinals(int nCardinals,
- Consumer<Direction> act) {
+ public static void forRandomCardinals(int nCardinals, Consumer<Direction> act) {
if (nCardinals <= 0 || nCardinals > 4) {
throw new IllegalArgumentException(
"Tried to do things with incorrect number of cardinal directions. Tried with "
@@ -99,9 +97,9 @@ public enum Direction {
/**
* Create a value of the enumeration from a string
- *
+ *
* @param value
- * The string to turn into a value
+ * The string to turn into a value
* @return The direction corresponding to the given value
*/
public static Direction properValueOf(String value) {
@@ -110,103 +108,97 @@ public enum Direction {
/**
* Test if this direction is a cardinal direction
- *
+ *
* @return If the direction is cardinal or not
*/
public boolean isCardinal() {
switch (this) {
- case EAST:
- case NORTH:
- case SOUTH:
- case WEST:
- return true;
- case DOWN:
- case UP:
- return false;
- default:
- throw new InvalidDirectionException(
- "WAT. Somehow ended up with an invalid direction "
- + this);
+ case EAST:
+ case NORTH:
+ case SOUTH:
+ case WEST:
+ return true;
+ case DOWN:
+ case UP:
+ return false;
+ default:
+ throw new InvalidDirectionException(
+ "WAT. Somehow ended up with an invalid direction " + this);
}
}
/**
* Get the direction that opposes the current one
- *
- * @return The direction that is in the opposite direction of the
- * current one
+ *
+ * @return The direction that is in the opposite direction of the current one
*/
public Direction opposing() {
switch (this) {
- case NORTH:
- return SOUTH;
- case EAST:
- return WEST;
- case SOUTH:
- return NORTH;
- case WEST:
- return WEST;
- case UP:
- return DOWN;
- case DOWN:
- return UP;
- default:
- throw new IllegalStateException(
- "Enumeration got into a invalid state. WAT");
+ case NORTH:
+ return SOUTH;
+ case EAST:
+ return WEST;
+ case SOUTH:
+ return NORTH;
+ case WEST:
+ return WEST;
+ case UP:
+ return DOWN;
+ case DOWN:
+ return UP;
+ default:
+ throw new IllegalStateException("Enumeration got into a invalid state. WAT");
}
}
/**
* Get the direction that is clockwise on the compass from this one.
- *
+ *
* Only works on cardinals.
- *
+ *
* @return The cardinal clockwise from this one
*/
public Direction rotateClockwise() {
switch (this) {
- case NORTH:
- return EAST;
- case EAST:
- return SOUTH;
- case SOUTH:
- return WEST;
- case WEST:
- return NORTH;
- case UP:
- case DOWN:
- default:
- throw new InvalidDirectionException(
- "Can't rotate non-cardinal direction clockwise: "
- + this);
+ case NORTH:
+ return EAST;
+ case EAST:
+ return SOUTH;
+ case SOUTH:
+ return WEST;
+ case WEST:
+ return NORTH;
+ case UP:
+ case DOWN:
+ default:
+ throw new InvalidDirectionException(
+ "Can't rotate non-cardinal direction clockwise: " + this);
}
}
/**
- * Get the direction that is counter-clockwise on the compass from this
- * one.
- *
+ * Get the direction that is counter-clockwise on the compass from this one.
+ *
* Only works on cardinals.
- *
+ *
* @return The cardinal counter-clockwise from this one
*/
public Direction rotateCounterClockwise() {
switch (this) {
- case NORTH:
- return WEST;
- case EAST:
- return NORTH;
- case SOUTH:
- return EAST;
- case WEST:
- return SOUTH;
- case UP:
- case DOWN:
- default:
- throw new InvalidDirectionException(
- "Can't rotate non-cardinal direction counterclockwise: "
- + this);
+ case NORTH:
+ return WEST;
+ case EAST:
+ return NORTH;
+ case SOUTH:
+ return EAST;
+ case WEST:
+ return SOUTH;
+ case UP:
+ case DOWN:
+ default:
+ throw new InvalidDirectionException(
+ "Can't rotate non-cardinal direction counterclockwise: " + this);
}
}
diff --git a/base/src/main/java/bjc/utils/misc/PropertyDB.java b/base/src/main/java/bjc/utils/misc/PropertyDB.java
index 09e1999..09c136c 100644
--- a/base/src/main/java/bjc/utils/misc/PropertyDB.java
+++ b/base/src/main/java/bjc/utils/misc/PropertyDB.java
@@ -16,8 +16,8 @@ import bjc.utils.ioutils.SimpleProperties;
*/
public class PropertyDB {
/* Regex storage. */
- private static SimpleProperties regexes;
- private static Map<String, Pattern> compiledRegexes;
+ private static SimpleProperties regexes;
+ private static Map<String, Pattern> compiledRegexes;
/* Format string storage. */
private static SimpleProperties formats;
@@ -40,33 +40,35 @@ public class PropertyDB {
/**
* Reload all the properties from their files.
*
- * NOTE: Any attempts to read from the property DB while properties are
- * being loaded will block, to prevent reads from partial states.
+ * NOTE: Any attempts to read from the property DB while properties are being
+ * loaded will block, to prevent reads from partial states.
*/
public static void reloadProperties() {
/* * Do the load with the write lock taken. */
loadLock.write(() -> {
- if(LOGLOAD) {
+ if (LOGLOAD) {
System.out.println("Reading regex properties:");
}
/* * Load regexes. */
regexes = new SimpleProperties();
- regexes.loadFrom(PropertyDB.class.getResourceAsStream("/regexes.sprop"), false);
- if(LOGLOAD) {
+ regexes.loadFrom(PropertyDB.class.getResourceAsStream("/regexes.sprop"),
+ false);
+ if (LOGLOAD) {
regexes.outputProperties(System.out);
System.out.println();
}
compiledRegexes = new HashMap<>();
- if(LOGLOAD) {
+ if (LOGLOAD) {
System.out.println("Reading format properties:");
}
/* * Load formats. */
formats = new SimpleProperties();
- formats.loadFrom(PropertyDB.class.getResourceAsStream("/formats.sprop"), false);
- if(LOGLOAD) {
+ formats.loadFrom(PropertyDB.class.getResourceAsStream("/formats.sprop"),
+ false);
+ if (LOGLOAD) {
formats.outputProperties(System.out);
System.out.println();
}
@@ -77,14 +79,15 @@ public class PropertyDB {
* Retrieve a persisted regular expression.
*
* @param key
- * The name of the regular expression.
+ * The name of the regular expression.
*
* @return The regular expression with that name.
*/
public static String getRegex(final String key) {
return loadLock.read(() -> {
- if(!regexes.containsKey(key)) {
- final String msg = String.format("No regular expression named '%s' found", key);
+ if (!regexes.containsKey(key)) {
+ final String msg
+ = String.format("No regular expression named '%s' found", key);
throw new NoSuchElementException(msg);
}
@@ -94,26 +97,24 @@ public class PropertyDB {
}
/**
- * Retrieve a persisted regular expression, compiled into a regular
- * expression.
+ * Retrieve a persisted regular expression, compiled into a regular expression.
*
* @param key
- * The name of the regular expression.
+ * The name of the regular expression.
*
* @return The regular expression with that name.
*/
public static Pattern getCompiledRegex(final String key) {
return loadLock.read(() -> {
- if(!regexes.containsKey(key)) {
- final String msg = String.format("No regular expression named '%s' found", key);
+ if (!regexes.containsKey(key)) {
+ final String msg
+ = String.format("No regular expression named '%s' found", key);
throw new NoSuchElementException(msg);
}
/* * Get the regex, and cache a compiled version. */
- return compiledRegexes.computeIfAbsent(key, strang -> {
- return Pattern.compile(regexes.get(strang));
- });
+ return compiledRegexes.computeIfAbsent(key, strang -> Pattern.compile(regexes.get(strang)));
});
}
@@ -121,14 +122,15 @@ public class PropertyDB {
* Retrieve a persisted format string.
*
* @param key
- * The name of the format string.
+ * The name of the format string.
*
* @return The format string with that name.
*/
public static String getFormat(final String key) {
return loadLock.read(() -> {
- if(!formats.containsKey(key)) {
- final String msg = String.format("No format string named '%s' found", key);
+ if (!formats.containsKey(key)) {
+ final String msg
+ = String.format("No format string named '%s' found", key);
throw new NoSuchElementException(msg);
}
@@ -138,14 +140,13 @@ public class PropertyDB {
}
/**
- * Retrieve a persisted format string, and apply it to a set of
- * arguments.
+ * Retrieve a persisted format string, and apply it to a set of arguments.
*
* @param key
- * The name of the format string.
+ * The name of the format string.
*
* @param objects
- * The parameters to the format string.
+ * The parameters to the format string.
*
* @return The format string with that name.
*/
diff --git a/base/src/main/java/bjc/utils/misc/RelativeDirection.java b/base/src/main/java/bjc/utils/misc/RelativeDirection.java
index 515ddea..7012377 100644
--- a/base/src/main/java/bjc/utils/misc/RelativeDirection.java
+++ b/base/src/main/java/bjc/utils/misc/RelativeDirection.java
@@ -9,7 +9,7 @@ import bjc.funcdata.IList;
/**
* Represents a direction that is relative to another direction
- *
+ *
* @author ben
*
*/
@@ -34,17 +34,17 @@ public enum RelativeDirection {
private static Random RNG = new Random();
/**
- * Perform a specified action for a random number of relative
- * directions.
- *
+ * Perform a specified action for a random number of relative directions.
+ *
* @param numDirections
- * The number of cardinal directions to act on. Must be
- * greater than 0 and less then 5
+ * The number of cardinal directions to act on. Must be
+ * greater than 0 and less then 5
* @param action
- * The action to perform for each of the relative directions
+ * The action to perform for each of the relative
+ * directions
* @param ignoreBackwards
- * Whether or not to not have a chance of one of the
- * directions being backwards
+ * Whether or not to not have a chance of one of the
+ * directions being backwards
*/
public static void forRandomDirections(int numDirections,
Consumer<RelativeDirection> action, boolean ignoreBackwards) {
@@ -62,16 +62,14 @@ public enum RelativeDirection {
+ numDirections);
}
- IList<RelativeDirection> relativeDirs =
- new FunctionalList<>(values());
+ IList<RelativeDirection> relativeDirs = new FunctionalList<>(values());
if (ignoreBackwards) {
relativeDirs.removeMatching(BACKWARD);
}
for (int i = 0; i <= maxNDirections - numDirections; i++) {
- RelativeDirection relativeDir =
- relativeDirs.randItem(RNG::nextInt);
+ RelativeDirection relativeDir = relativeDirs.randItem(RNG::nextInt);
relativeDirs.removeMatching(relativeDir);
}
@@ -81,9 +79,9 @@ public enum RelativeDirection {
/**
* Properly convert a string to a relative direction
- *
+ *
* @param value
- * The string to convert
+ * The string to convert
* @return The relative direction represented by the string
*/
public static RelativeDirection properValueOf(String value) {
@@ -92,7 +90,7 @@ public enum RelativeDirection {
/**
* Change another direction by turning the way this direction specifies
- *
+ *
* @param dir
* The direction to change
* @return The direction after turning this way
@@ -101,18 +99,18 @@ public enum RelativeDirection {
// Only cardinal directions can be truly absolutized
if (dir.isCardinal()) {
switch (this) {
- case BACKWARD:
- return dir;
- case FORWARD:
- return dir.opposing();
- case LEFT:
- return dir.rotateCounterClockwise();
- case RIGHT:
- return dir.rotateClockwise();
- default:
- throw new InvalidDirectionException(
- "Attempted to make absolute a direction in a unknown way "
- + this);
+ case BACKWARD:
+ return dir;
+ case FORWARD:
+ return dir.opposing();
+ case LEFT:
+ return dir.rotateCounterClockwise();
+ case RIGHT:
+ return dir.rotateClockwise();
+ default:
+ throw new InvalidDirectionException(
+ "Attempted to make absolute a direction in a unknown way "
+ + this);
}
}