summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils
diff options
context:
space:
mode:
authorbjculkin <bjculkin@BECK-GT5TRW1.wvu-ad.wvu.edu>2018-04-25 15:29:18 -0400
committerbjculkin <bjculkin@BECK-GT5TRW1.wvu-ad.wvu.edu>2018-04-25 15:29:18 -0400
commitd7bae4d2145d8337570fec03974272d49ba5269d (patch)
tree2f44ef7a13315990836344a4616ceaeaa1486916 /base/src/main/java/bjc/utils
parent37e55c679f9a9ca1d57d061eac5e5deef0ef0a90 (diff)
Add some new classes
Import some new classes from an old project
Diffstat (limited to 'base/src/main/java/bjc/utils')
-rw-r--r--base/src/main/java/bjc/utils/exceptions/InvalidDirectionException.java27
-rw-r--r--base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java6
-rw-r--r--base/src/main/java/bjc/utils/misc/Direction.java221
-rw-r--r--base/src/main/java/bjc/utils/misc/PropertyDB.java (renamed from base/src/main/java/bjc/utils/PropertyDB.java)2
-rw-r--r--base/src/main/java/bjc/utils/misc/RelativeDirection.java123
-rw-r--r--base/src/main/java/bjc/utils/parserutils/DoubleMatcher.java4
-rw-r--r--base/src/main/java/bjc/utils/parserutils/StringDescaper.java3
-rw-r--r--base/src/main/java/bjc/utils/parserutils/TokenUtils.java6
-rw-r--r--base/src/main/java/bjc/utils/parserutils/splitter/ConfigurableTokenSplitter.java2
9 files changed, 382 insertions, 12 deletions
diff --git a/base/src/main/java/bjc/utils/exceptions/InvalidDirectionException.java b/base/src/main/java/bjc/utils/exceptions/InvalidDirectionException.java
new file mode 100644
index 0000000..216953c
--- /dev/null
+++ b/base/src/main/java/bjc/utils/exceptions/InvalidDirectionException.java
@@ -0,0 +1,27 @@
+package bjc.utils.exceptions;
+
+/**
+ * Represents the condition where a direction has been used in a improper
+ * manner
+ *
+ * @author ben
+ *
+ */
+public class InvalidDirectionException extends IllegalArgumentException {
+
+ /**
+ * Version for serialization
+ */
+ private static final long serialVersionUID = 6852151917518831932L;
+
+ /**
+ * Create a new {@link InvalidDirectionException} with the given cause
+ *
+ * @param cause
+ * The situation that resulting in this exit being thrown
+ */
+ public InvalidDirectionException(String cause) {
+ super(cause);
+ }
+
+}
diff --git a/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java b/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java
index 1ac97a6..7e709dc 100644
--- a/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java
+++ b/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java
@@ -14,15 +14,15 @@ import bjc.utils.ioutils.format.directives.LiteralDirective;
import bjc.utils.ioutils.format.directives.NumberDirective;
import bjc.utils.ioutils.format.directives.RadixDirective;
+import static bjc.utils.misc.PropertyDB.applyFormat;
+import static bjc.utils.misc.PropertyDB.getRegex;
+
import java.util.HashMap;
import java.util.Map;
import java.util.UnknownFormatConversionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import static bjc.utils.PropertyDB.applyFormat;
-import static bjc.utils.PropertyDB.getRegex;
-
/**
* An implementation of CL's FORMAT.
*
diff --git a/base/src/main/java/bjc/utils/misc/Direction.java b/base/src/main/java/bjc/utils/misc/Direction.java
new file mode 100644
index 0000000..4833f14
--- /dev/null
+++ b/base/src/main/java/bjc/utils/misc/Direction.java
@@ -0,0 +1,221 @@
+package bjc.utils.misc;
+
+import java.util.Random;
+import java.util.function.Consumer;
+
+import bjc.utils.exceptions.InvalidDirectionException;
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.IList;
+
+import org.apache.commons.lang3.text.WordUtils;
+
+/**
+ * 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
+ *
+ * @author ben
+ *
+ */
+public enum Direction {
+ /**
+ * Negative z-axis
+ */
+ DOWN,
+ /**
+ * Positive y-axis
+ */
+ EAST,
+ /**
+ * Positive x-axis
+ */
+ NORTH,
+ /**
+ * Negative x-axis
+ */
+ SOUTH,
+ /**
+ * Positive z-axis
+ */
+ UP,
+ /**
+ * Negative y-axis
+ */
+ WEST;
+
+ /**
+ * The source of randomness for picking random directions
+ */
+ private static final Random RNG = new Random();
+
+ /**
+ * Get a list of all the cardinal directions
+ *
+ * @return A list of all the cardinal directions
+ */
+ public static IList<Direction> cardinals() {
+ return new FunctionalList<>(NORTH, SOUTH, EAST, WEST);
+ }
+
+ /**
+ * Perform the specified action once with each of the cardinal
+ * directions
+ *
+ * @param act
+ * The action to perform for each cardinal direction
+ */
+ public static void forCardinalDirections(Consumer<Direction> act) {
+ cardinals().forEach(act);
+ }
+
+ /**
+ * 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
+ * @param act
+ * The action to perform for each of the cardinals
+ */
+ 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 "
+ + nCardinals);
+ }
+
+ IList<Direction> cards = cardinals();
+
+ for (int i = 0; i <= 4 - nCardinals; i++) {
+ Direction rDir = cards.randItem(RNG::nextInt);
+
+ cards.removeMatching(rDir);
+ }
+
+ cards.forEach(act);
+ }
+
+ /**
+ * Create a value of the enumeration from a string
+ *
+ * @param value
+ * The string to turn into a value
+ * @return The direction corresponding to the given value
+ */
+ public static Direction properValueOf(String value) {
+ return valueOf(value.toUpperCase());
+ }
+
+ /**
+ * 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);
+ }
+ }
+
+ /**
+ * Get the direction that opposes 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");
+ }
+ }
+
+ /**
+ * 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);
+
+ }
+ }
+
+ /**
+ * 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);
+
+ }
+ }
+
+ @Override
+ public String toString() {
+ /*
+ * Make sure the word is properly capitalized for english
+ */
+ return WordUtils.capitalize(super.toString().toLowerCase());
+ }
+}
diff --git a/base/src/main/java/bjc/utils/PropertyDB.java b/base/src/main/java/bjc/utils/misc/PropertyDB.java
index 85f1e2f..c9434ef 100644
--- a/base/src/main/java/bjc/utils/PropertyDB.java
+++ b/base/src/main/java/bjc/utils/misc/PropertyDB.java
@@ -1,4 +1,4 @@
-package bjc.utils;
+package bjc.utils.misc;
import java.util.HashMap;
import java.util.Map;
diff --git a/base/src/main/java/bjc/utils/misc/RelativeDirection.java b/base/src/main/java/bjc/utils/misc/RelativeDirection.java
new file mode 100644
index 0000000..d7ccf6e
--- /dev/null
+++ b/base/src/main/java/bjc/utils/misc/RelativeDirection.java
@@ -0,0 +1,123 @@
+package bjc.utils.misc;
+
+import java.util.Random;
+import java.util.function.Consumer;
+
+import bjc.utils.exceptions.InvalidDirectionException;
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.IList;
+
+/**
+ * Represents a direction that is relative to another direction
+ *
+ * @author ben
+ *
+ */
+public enum RelativeDirection {
+ /**
+ * Same as direction
+ */
+ BACKWARD,
+ /**
+ * Opposing direction
+ */
+ FORWARD,
+ /**
+ * Counterclockwise from direction
+ */
+ LEFT,
+ /**
+ * Clockwise from direction
+ */
+ RIGHT;
+
+ private static Random RNG = new Random();
+
+ /**
+ * 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
+ * @param action
+ * 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
+ */
+ public static void forRandomDirections(int numDirections,
+ Consumer<RelativeDirection> action, boolean ignoreBackwards) {
+ int maxNDirections;
+
+ if (ignoreBackwards) {
+ maxNDirections = 3;
+ } else {
+ maxNDirections = 4;
+ }
+
+ if (numDirections <= 0 || numDirections > maxNDirections) {
+ throw new IllegalArgumentException(
+ "Tried to do things with incorrect number of relative directions. Tried with "
+ + numDirections);
+ }
+
+ 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);
+
+ relativeDirs.removeMatching(relativeDir);
+ }
+
+ relativeDirs.forEach(action);
+ }
+
+ /**
+ * Properly convert a string to a relative direction
+ *
+ * @param value
+ * The string to convert
+ * @return The relative direction represented by the string
+ */
+ public static RelativeDirection properValueOf(String value) {
+ return valueOf(value.toUpperCase());
+ }
+
+ /**
+ * Change another direction by turning the way this direction specifies
+ *
+ * @param dir
+ * The direction to change
+ * @return The direction after turning this way
+ */
+ public Direction makeAbsolute(Direction dir) {
+ // 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);
+ }
+ }
+
+ // Since it isn't a cardinal direction, absolutize it against a
+ // random direction
+ return this.makeAbsolute(Direction.NORTH);
+ }
+}
diff --git a/base/src/main/java/bjc/utils/parserutils/DoubleMatcher.java b/base/src/main/java/bjc/utils/parserutils/DoubleMatcher.java
index 31966d7..3618c50 100644
--- a/base/src/main/java/bjc/utils/parserutils/DoubleMatcher.java
+++ b/base/src/main/java/bjc/utils/parserutils/DoubleMatcher.java
@@ -1,7 +1,7 @@
package bjc.utils.parserutils;
-import static bjc.utils.PropertyDB.applyFormat;
-import static bjc.utils.PropertyDB.getRegex;
+import static bjc.utils.misc.PropertyDB.applyFormat;
+import static bjc.utils.misc.PropertyDB.getRegex;
import java.util.regex.Pattern;
diff --git a/base/src/main/java/bjc/utils/parserutils/StringDescaper.java b/base/src/main/java/bjc/utils/parserutils/StringDescaper.java
index e8c340a..d6c4f3c 100644
--- a/base/src/main/java/bjc/utils/parserutils/StringDescaper.java
+++ b/base/src/main/java/bjc/utils/parserutils/StringDescaper.java
@@ -8,10 +8,9 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
+import static bjc.utils.misc.PropertyDB.getRegex;
import static java.util.Map.Entry;
-import static bjc.utils.PropertyDB.getRegex;
-
/**
<<<<<<< Updated upstream
* Customizable string escapes.
diff --git a/base/src/main/java/bjc/utils/parserutils/TokenUtils.java b/base/src/main/java/bjc/utils/parserutils/TokenUtils.java
index 7b159af..c1a68a8 100644
--- a/base/src/main/java/bjc/utils/parserutils/TokenUtils.java
+++ b/base/src/main/java/bjc/utils/parserutils/TokenUtils.java
@@ -1,8 +1,8 @@
package bjc.utils.parserutils;
-import static bjc.utils.PropertyDB.applyFormat;
-import static bjc.utils.PropertyDB.getCompiledRegex;
-import static bjc.utils.PropertyDB.getRegex;
+import static bjc.utils.misc.PropertyDB.applyFormat;
+import static bjc.utils.misc.PropertyDB.getCompiledRegex;
+import static bjc.utils.misc.PropertyDB.getRegex;
import java.util.LinkedList;
import java.util.List;
diff --git a/base/src/main/java/bjc/utils/parserutils/splitter/ConfigurableTokenSplitter.java b/base/src/main/java/bjc/utils/parserutils/splitter/ConfigurableTokenSplitter.java
index 731c9c6..2a565f2 100644
--- a/base/src/main/java/bjc/utils/parserutils/splitter/ConfigurableTokenSplitter.java
+++ b/base/src/main/java/bjc/utils/parserutils/splitter/ConfigurableTokenSplitter.java
@@ -1,6 +1,6 @@
package bjc.utils.parserutils.splitter;
-import static bjc.utils.PropertyDB.applyFormat;
+import static bjc.utils.misc.PropertyDB.applyFormat;
import java.util.LinkedHashSet;
import java.util.Set;