From dca8e9f586fd595a7995f07788318fb92b8cce79 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 28 Jul 2016 16:44:36 -0400 Subject: Format/Cleanup pass --- .../main/java/bjc/utils/cli/GenericCommand.java | 9 +- .../java/bjc/utils/cli/GenericCommandMode.java | 26 ++--- .../src/main/java/bjc/utils/cli/ICommandHelp.java | 5 +- .../src/main/java/bjc/utils/cli/ICommandMode.java | 21 ++-- .../components/ComponentDescriptionFileParser.java | 9 +- .../utils/components/FileComponentRepository.java | 57 ++++++----- .../bjc/utils/components/IComponentRepository.java | 16 +-- .../src/main/java/bjc/utils/data/BoundLazy.java | 49 +++++----- .../main/java/bjc/utils/data/BoundLazyPair.java | 60 ++++++------ .../main/java/bjc/utils/data/BoundListHolder.java | 19 ++-- .../src/main/java/bjc/utils/data/Either.java | 89 +++++++++-------- .../java/bjc/utils/data/HalfBoundLazyPair.java | 51 +++++----- .../src/main/java/bjc/utils/data/IHolder.java | 25 +++-- BJC-Utils2/src/main/java/bjc/utils/data/IPair.java | 108 ++++++++++----------- .../src/main/java/bjc/utils/data/Identity.java | 16 +-- BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java | 16 +-- .../src/main/java/bjc/utils/data/LazyPair.java | 104 ++++++++++---------- .../src/main/java/bjc/utils/data/ListHolder.java | 27 +++--- .../src/main/java/bjc/utils/data/Option.java | 16 +-- BJC-Utils2/src/main/java/bjc/utils/data/Pair.java | 51 +++++----- .../main/java/bjc/utils/data/SingleSupplier.java | 4 +- .../src/main/java/bjc/utils/data/WrappedLazy.java | 24 ++--- .../main/java/bjc/utils/data/WrappedOption.java | 24 ++--- .../main/java/bjc/utils/funcdata/ExtendedMap.java | 3 +- .../java/bjc/utils/funcdata/FunctionalList.java | 15 +-- .../utils/funcdata/FunctionalStringTokenizer.java | 3 +- .../src/main/java/bjc/utils/funcdata/IList.java | 78 +++++++-------- .../src/main/java/bjc/utils/funcdata/IMap.java | 3 +- .../bjc/utils/funcdata/TransformedValueMap.java | 4 +- .../src/main/java/bjc/utils/funcdata/Tree.java | 15 ++- .../bjc/utils/funcdata/bst/BinarySearchTree.java | 4 +- .../java/bjc/utils/funcdata/theory/Bifunctor.java | 88 ++++++++--------- .../java/bjc/utils/funcutils/CollectorUtils.java | 8 +- .../bjc/utils/funcutils/CompoundCollector.java | 38 ++++---- .../bjc/utils/funcutils/GroupPartIteration.java | 18 ++-- .../main/java/bjc/utils/funcutils/ListUtils.java | 30 +++--- .../main/java/bjc/utils/funcutils/StringUtils.java | 13 ++- .../java/bjc/utils/funcutils/TokenDeaffixer.java | 3 +- .../java/bjc/utils/funcutils/TokenSplitter.java | 3 +- .../src/main/java/bjc/utils/gen/RandomGrammar.java | 3 +- .../main/java/bjc/utils/gen/WeightedGrammar.java | 18 ++-- .../main/java/bjc/utils/gen/WeightedRandom.java | 6 +- .../main/java/bjc/utils/gui/HolderOutputPanel.java | 26 ++--- .../utils/parserutils/RuleBasedConfigReader.java | 4 +- .../utils/parserutils/RuleBasedReaderPragmas.java | 57 ++++++----- .../bjc/utils/parserutils/TokenTransformer.java | 4 +- .../bjc/utils/parserutils/TreeConstructor.java | 4 +- 47 files changed, 614 insertions(+), 660 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc') diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java index 1a95018..e73d936 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java @@ -24,14 +24,15 @@ public class GenericCommand implements ICommand { */ public GenericCommand(ICommandHandler handler, String description, String help) { - if(handler == null) { - throw new NullPointerException("Command handler must not be null"); + if (handler == null) { + throw new NullPointerException( + "Command handler must not be null"); } - + this.handler = handler; this.help = new GenericHelp(description, help); } - + @Override public ICommand aliased() { return new DelegatingCommand(this); diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java index 59d3dc3..4b3b4fd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -21,24 +21,24 @@ public class GenericCommandMode implements ICommandMode { /* * Contains the commands this mode handles */ - private IMap commandHandlers; - private IMap defaultHandlers; + private IMap commandHandlers; + private IMap defaultHandlers; // Contains help topics without an associated command - private IMap helpTopics; + private IMap helpTopics; // The action to execute upon encountering an unknown command - private BiConsumer unknownCommandHandler; + private BiConsumer unknownCommandHandler; // The functions to use for input/output - private Consumer errorOutput; - private Consumer normalOutput; + private Consumer errorOutput; + private Consumer normalOutput; // The name of this command mode, or null if it is unnamed - private String modeName; + private String modeName; // The custom prompt to use, or null if none is specified - private String customPrompt; + private String customPrompt; /** * Create a new generic command mode @@ -331,6 +331,11 @@ public class GenericCommandMode implements ICommandMode { return ICommandMode.super.getName(); } + @Override + public boolean isCustomPromptEnabled() { + return customPrompt != null; + } + @Override public ICommandMode process(String command, String[] args) { normalOutput.accept("\n"); @@ -414,9 +419,4 @@ public class GenericCommandMode implements ICommandMode { defaultHandlers.put("exit", buildExitCommand()); } - - @Override - public boolean isCustomPromptEnabled() { - return customPrompt != null; - } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java index bd81537..472d6a3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java @@ -18,8 +18,9 @@ public interface ICommandHelp { * Get the summary line for a command. * * A summary line should consist of a string of the following format - * "\t" - * where anything in angle brackets should be filled in. + * "\t" where anything in angle brackets + * should be filled in. + * * @return The summary line line for a command */ public String getSummary(); diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java index f60e571..8583b80 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java @@ -43,6 +43,15 @@ public interface ICommandMode { return ""; } + /** + * Check if this mode uses a custom prompt + * + * @return Whether or not this mode uses a custom prompt + */ + public default boolean isCustomPromptEnabled() { + return false; + } + /** * Process a command in this mode * @@ -53,17 +62,7 @@ public interface ICommandMode { * @return The command mode to use for the next command. Defaults to * returning this, and doing nothing else */ - public default ICommandMode process(String command, - String[] args) { + public default ICommandMode process(String command, String[] args) { return this; } - - /** - * Check if this mode uses a custom prompt - * - * @return Whether or not this mode uses a custom prompt - */ - public default boolean isCustomPromptEnabled() { - return false; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java index b35c77b..ef8f8b6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java @@ -1,11 +1,12 @@ package bjc.utils.components; +import static bjc.utils.parserutils.RuleBasedReaderPragmas.buildInteger; +import static bjc.utils.parserutils.RuleBasedReaderPragmas.buildStringCollapser; + import java.io.InputStream; import bjc.utils.parserutils.RuleBasedConfigReader; -import static bjc.utils.parserutils.RuleBasedReaderPragmas.*; - /** * Read a component description from a file * @@ -38,8 +39,8 @@ public class ComponentDescriptionFileParser { * The stream to parse from * @return The description parsed from the stream */ - public static ComponentDescription - fromStream(InputStream inputSource) { + public static ComponentDescription fromStream( + InputStream inputSource) { ComponentDescriptionState readState = reader .fromStream(inputSource, new ComponentDescriptionState()); diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java index 182549c..bb2d421 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -28,14 +28,14 @@ import bjc.utils.funcutils.FileUtils; public class FileComponentRepository implements IComponentRepository { // The logger to use for storing data about this class - private static final Logger CLASS_LOGGER = - Logger.getLogger("FileComponentRepository"); + private static final Logger CLASS_LOGGER = Logger + .getLogger("FileComponentRepository"); // The internal storage of components private IMap components; // The path that all the components came from - private Path sourceDirectory; + private Path sourceDirectory; /** * Create a new component repository sourcing components from files in @@ -71,25 +71,24 @@ public class FileComponentRepository // Predicate to use to traverse all the files in a directory, but // not recurse into sub-directories - BiPredicate firstLevelTraverser = - (pth, attr) -> { - if (attr.isDirectory() && !isFirstDir.getValue()) { - - /* - * Skip directories, they probably have component - * support files. - */ - return false; - } - - /* - * Don't skip the first directory, that's the parent - * directory - */ - isFirstDir.replace(false); - - return true; - }; + BiPredicate firstLevelTraverser = (pth, + attr) -> { + if (attr.isDirectory() && !isFirstDir.getValue()) { + + /* + * Skip directories, they probably have component support + * files. + */ + return false; + } + + /* + * Don't skip the first directory, that's the parent directory + */ + isFirstDir.replace(false); + + return true; + }; // Try reading components try { @@ -106,6 +105,11 @@ public class FileComponentRepository } } + @Override + public IMap getAll() { + return components; + } + @Override public ComponentType getByName(String name) { return components.get(name); @@ -116,11 +120,6 @@ public class FileComponentRepository return components.valueList(); } - @Override - public IMap getAll() { - return components; - } - @Override public String getSource() { return "Components read from directory " + sourceDirectory + "."; @@ -141,8 +140,8 @@ public class FileComponentRepository "Component reader read null component"); } else if (!components.containsKey(component.getName())) { // We only care about the latest version of a component - ComponentType oldComponent = - components.put(component.getName(), component); + ComponentType oldComponent = components + .put(component.getName(), component); if (oldComponent.getVersion() > component.getVersion()) { components.put(oldComponent.getName(), oldComponent); diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java index 2644276..1d322af 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java @@ -13,6 +13,14 @@ import bjc.utils.funcdata.IMap; * The type of components contained in this repository */ public interface IComponentRepository { + /** + * Get all of the components this repository knows about + * + * @return A map from component name to component, containing all of + * the components in the repositories + */ + public IMap getAll(); + /** * Get a component with a specific name * @@ -32,14 +40,6 @@ public interface IComponentRepository return getAll().valueList(); } - /** - * Get all of the components this repository knows about - * - * @return A map from component name to component, containing all of - * the components in the repositories - */ - public IMap getAll(); - /** * Get the source from which these components came * diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java index 1256e31..b6cd715 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java @@ -15,28 +15,27 @@ class BoundLazy /* * The old value */ - private Supplier> oldSupplier; + private Supplier> oldSupplier; /* * The function to use to transform the old value into a new value */ - private Function> binder; + private Function> binder; /* * The bound value being held */ - private IHolder boundHolder; + private IHolder boundHolder; /* * Whether the bound value has been actualized or not */ - private boolean holderBound; + private boolean holderBound; /* * Transformations currently pending on the bound value */ - private IList> actions = - new FunctionalList<>(); + private IList> actions = new FunctionalList<>(); /* * Create a new bound lazy value @@ -48,13 +47,12 @@ class BoundLazy } @Override - public IHolder - bind(Function> bindr) { + public IHolder bind( + Function> bindr) { /* * Prepare a list of pending actions */ - IList> pendingActions = - new FunctionalList<>(); + IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); /* @@ -82,11 +80,18 @@ class BoundLazy } @Override - public IHolder - map(Function mapper) { + public Function> lift( + Function func) { + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + + @Override + public IHolder map( + Function mapper) { // Prepare a list of pending actions - IList> pendingActions = - new FunctionalList<>(); + IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); // Prepare the new supplier @@ -117,28 +122,20 @@ class BoundLazy } @Override - public IHolder - transform(UnaryOperator transformer) { + public IHolder transform( + UnaryOperator transformer) { actions.add(transformer); return this; } @Override - public UnwrappedType - unwrap(Function unwrapper) { + public UnwrappedType unwrap( + Function unwrapper) { if (!holderBound) { boundHolder = oldSupplier.get().unwrap(binder::apply); } return boundHolder.unwrap(unwrapper); } - - @Override - public Function> - lift(Function func) { - return (val) -> { - return new Lazy<>(func.apply(val)); - }; - } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java index 2b20349..c527d94 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java @@ -90,25 +90,17 @@ class BoundLazyPair } @Override - public MergedType merge( - BiFunction merger) { - if (!pairBound) { - boundPair = binder.apply(leftSupplier.get(), - rightSupplier.get()); - - pairBound = true; - } - - return boundPair.merge(merger); - } - - @Override - public String toString() { - if (pairBound) { - return boundPair.toString(); - } - - return "(un-materialized)"; + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + BiFunction rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return bind((leftVal, rightVal) -> { + return new LazyPair<>( + leftCombiner.apply(leftVal, otherLeft), + rightCombiner.apply(rightVal, otherRight)); + }); + }); } @Override @@ -168,16 +160,24 @@ class BoundLazyPair } @Override - public IPair combine( - IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - return new LazyPair<>( - leftCombiner.apply(leftVal, otherLeft), - rightCombiner.apply(rightVal, otherRight)); - }); - }); + public MergedType merge( + BiFunction merger) { + if (!pairBound) { + boundPair = binder.apply(leftSupplier.get(), + rightSupplier.get()); + + pairBound = true; + } + + return boundPair.merge(merger); + } + + @Override + public String toString() { + if (pairBound) { + return boundPair.toString(); + } + + return "(un-materialized)"; } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java index 85ec8f6..fe47dcc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java @@ -8,8 +8,7 @@ import bjc.utils.funcdata.IList; class BoundListHolder implements IHolder { private IList> heldHolders; - public BoundListHolder( - IList> toHold) { + public BoundListHolder(IList> toHold) { heldHolders = toHold; } @@ -24,6 +23,14 @@ class BoundListHolder implements IHolder { return new BoundListHolder<>(boundHolders); } + @Override + public Function> lift( + Function func) { + return (val) -> { + return new ListHolder<>(func.apply(val)); + }; + } + @Override public IHolder map( Function mapper) { @@ -50,12 +57,4 @@ class BoundListHolder implements IHolder { Function unwrapper) { return heldHolders.randItem().unwrap(unwrapper); } - - @Override - public Function> lift( - Function func) { - return (val) -> { - return new ListHolder<>(func.apply(val)); - }; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java index 8787888..9418882 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java @@ -15,21 +15,6 @@ import java.util.function.Function; */ public class Either implements IPair { - private LeftType leftVal; - private RightType rightVal; - - private boolean isLeft; - - private Either(LeftType left, RightType right) { - if (left == null) { - rightVal = right; - } else { - leftVal = left; - - isLeft = true; - } - } - /** * Create a new either with the left value occupied * @@ -41,11 +26,10 @@ public class Either * The value to put on the left * @return An either with the left side occupied */ - public static Either - fromLeft(LeftType left) { + public static Either fromLeft( + LeftType left) { return new Either<>(left, null); } - /** * Create a new either with the right value occupied * @@ -57,11 +41,27 @@ public class Either * The value to put on the right * @return An either with the right side occupied */ - public static Either - fromRight(RightType right) { + public static Either fromRight( + RightType right) { return new Either<>(null, right); } + private LeftType leftVal; + + private RightType rightVal; + + private boolean isLeft; + + private Either(LeftType left, RightType right) { + if (left == null) { + rightVal = right; + } else { + leftVal = left; + + isLeft = true; + } + } + @Override public IPair bind( BiFunction> binder) { @@ -89,8 +89,26 @@ public class Either } @Override - public IPair - mapLeft(Function mapper) { + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + BiFunction rightCombiner) { + if (isLeft) { + return otherPair.bind((otherLeft, otherRight) -> { + return new Either<>(leftCombiner.apply(leftVal, otherLeft), + null); + }); + } + + return otherPair.bind((otherLeft, otherRight) -> { + return new Either<>(null, + rightCombiner.apply(rightVal, otherRight)); + }); + } + + @Override + public IPair mapLeft( + Function mapper) { if (isLeft) { return new Either<>(mapper.apply(leftVal), null); } @@ -99,8 +117,8 @@ public class Either } @Override - public IPair - mapRight(Function mapper) { + public IPair mapRight( + Function mapper) { if (isLeft) { return new Either<>(leftVal, null); } @@ -109,27 +127,8 @@ public class Either } @Override - public MergedType - merge(BiFunction merger) { + public MergedType merge( + BiFunction merger) { return merger.apply(leftVal, rightVal); } - - @Override - public - IPair - combine(IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { - if (isLeft) { - return otherPair.bind((otherLeft, otherRight) -> { - return new Either<>(leftCombiner.apply(leftVal, otherLeft), - null); - }); - } - - return otherPair.bind((otherLeft, otherRight) -> { - return new Either<>(null, - rightCombiner.apply(rightVal, otherRight)); - }); - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java index 198dd96..d91ede2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java @@ -80,20 +80,22 @@ class HalfBoundLazyPair } @Override - public MergedType - merge(BiFunction merger) { - if (!pairBound) { - boundPair = binder.apply(oldSupplier.get()); - - pairBound = true; - } - - return boundPair.merge(merger); + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + BiFunction rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return bind((leftVal, rightVal) -> { + return new LazyPair<>( + leftCombiner.apply(leftVal, otherLeft), + rightCombiner.apply(rightVal, otherRight)); + }); + }); } @Override - public IPair - mapLeft(Function mapper) { + public IPair mapLeft( + Function mapper) { Supplier leftSupp = () -> { if (pairBound) { return mapper.apply(boundPair.getLeft()); @@ -116,8 +118,8 @@ class HalfBoundLazyPair } @Override - public IPair - mapRight(Function mapper) { + public IPair mapRight( + Function mapper) { Supplier leftSupp = () -> { if (pairBound) { return boundPair.getLeft(); @@ -138,19 +140,16 @@ class HalfBoundLazyPair return new LazyPair<>(leftSupp, rightSupp); } - + @Override - public - IPair - combine(IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - return new LazyPair<>( - leftCombiner.apply(leftVal, otherLeft), - rightCombiner.apply(rightVal, otherRight)); - }); - }); + public MergedType merge( + BiFunction merger) { + if (!pairBound) { + boundPair = binder.apply(oldSupplier.get()); + + pairBound = true; + } + + return boundPair.merge(merger); } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java index 58ae5db..b2e4369 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -25,8 +25,8 @@ public interface IHolder extends Functor { * The function to bind to the value * @return A holder from binding the value */ - public IHolder - bind(Function> binder); + public IHolder bind( + Function> binder); /** * Apply an action to the value @@ -43,9 +43,8 @@ public interface IHolder extends Functor { } @Override - default - Function, Functor> - fmap(Function func) { + default Function, Functor> fmap( + Function func) { return (argumentFunctor) -> { if (!(argumentFunctor instanceof IHolder)) { throw new IllegalArgumentException( @@ -72,8 +71,8 @@ public interface IHolder extends Functor { * The function to lift over the holder * @return The function lifted over the holder */ - public Function> - lift(Function func); + public Function> lift( + Function func); /** * Make this holder lazy @@ -114,8 +113,8 @@ public interface IHolder extends Functor { * The function to do mapping with * @return A holder with the mapped value */ - public IHolder - map(Function mapper); + public IHolder map( + Function mapper); /** * Replace the held value with a new one @@ -137,8 +136,8 @@ public interface IHolder extends Functor { * The function to transform the value with * @return The holder itself, for easy chaining */ - public IHolder - transform(UnaryOperator transformer); + public IHolder transform( + UnaryOperator transformer); /** * Unwrap the value contained in this holder so that it is no longer @@ -150,6 +149,6 @@ public interface IHolder extends Functor { * The function to use to unwrap the value * @return The unwrapped held value */ - public UnwrappedType - unwrap(Function unwrapper); + public UnwrappedType unwrap( + Function unwrapper); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java index f94d656..a2a635f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -56,6 +56,46 @@ public interface IPair public IPair bindRight( Function> rightBinder); + /** + * Pairwise combine two pairs together + * + * @param + * The left type of the other pair + * @param + * The right type of the other pair + * @param otherPair + * The pair to combine with + * @return The pairs, pairwise combined together + */ + public default IPair, IPair> combine( + IPair otherPair) { + return combine(otherPair, + (left, otherLeft) -> new Pair<>(left, otherLeft), + (right, otherRight) -> new Pair<>(right, otherRight)); + } + + /** + * Combine the contents of two pairs together + * + * @param + * The type of the left value of the other pair + * @param + * The type of the right value of the other pair + * @param + * The type of the left value of the combined pair + * @param + * The type of the right value of the combined pair + * @param otherPair + * The other pair to combine with + * @param leftCombiner + * @param rightCombiner + * @return A pair with its values combined + */ + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + BiFunction rightCombiner); + /** * Immediately perfom the specified action with the contents of this * pair @@ -72,25 +112,22 @@ public interface IPair } @Override - default - Function, Bifunctor> - fmapLeft(Function func) { + default Function, Bifunctor> fmapLeft( + Function func) { return (argumentPair) -> { if (!(argumentPair instanceof IPair)) { throw new IllegalArgumentException( "This function can only be applied to instances of IPair"); } - IPair argPair = - (IPair) argumentPair; + IPair argPair = (IPair) argumentPair; return argPair.mapLeft(func); }; } @Override - default - Function, Bifunctor> + default Function, Bifunctor> fmapRight(Function func) { return (argumentPair) -> { @@ -99,8 +136,7 @@ public interface IPair "This function can only be applied to instances of IPair"); } - IPair argPair = - (IPair) argumentPair; + IPair argPair = (IPair) argumentPair; return argPair.mapRight(func); }; @@ -137,8 +173,8 @@ public interface IPair * pair * @return The pair, with its left part transformed */ - public IPair - mapLeft(Function mapper); + public IPair mapLeft( + Function mapper); /** * Transform the value on the right side of the pair. Doesn't modify @@ -151,8 +187,8 @@ public interface IPair * pair * @return The pair, with its right part transformed */ - public IPair - mapRight(Function mapper); + public IPair mapRight( + Function mapper); /** * Merge the two values in this pair into a single value @@ -163,48 +199,6 @@ public interface IPair * The function to use for merging * @return The pair, merged into a single value */ - public MergedType - merge(BiFunction merger); - - /** - * Combine the contents of two pairs together - * - * @param - * The type of the left value of the other pair - * @param - * The type of the right value of the other pair - * @param - * The type of the left value of the combined pair - * @param - * The type of the right value of the combined pair - * @param otherPair - * The other pair to combine with - * @param leftCombiner - * @param rightCombiner - * @return A pair with its values combined - */ - public - IPair - combine(IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner); - - /** - * Pairwise combine two pairs together - * - * @param - * The left type of the other pair - * @param - * The right type of the other pair - * @param otherPair - * The pair to combine with - * @return The pairs, pairwise combined together - */ - public default - IPair, IPair> - combine(IPair otherPair) { - return combine(otherPair, - (left, otherLeft) -> new Pair<>(left, otherLeft), - (right, otherRight) -> new Pair<>(right, otherRight)); - } + public MergedType merge( + BiFunction merger); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java index f42ceb7..8fcaf98 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java @@ -88,6 +88,14 @@ public class Identity implements IHolder { return result; } + @Override + public Function> lift( + Function func) { + return (val) -> { + return new Identity<>(func.apply(val)); + }; + } + @Override public IHolder map( Function mapper) { @@ -112,12 +120,4 @@ public class Identity implements IHolder { Function unwrapper) { return unwrapper.apply(heldValue); } - - @Override - public Function> lift( - Function func) { - return (val) -> { - return new Identity<>(func.apply(val)); - }; - } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java index 22f948c..f05204b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java @@ -75,6 +75,14 @@ public class Lazy implements IHolder { }, binder); } + @Override + public Function> lift( + Function func) { + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + @Override public IHolder map( Function mapper) { @@ -133,12 +141,4 @@ public class Lazy implements IHolder { return unwrapper.apply(heldValue); } - - @Override - public Function> lift( - Function func) { - return (val) -> { - return new Lazy<>(func.apply(val)); - }; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java index 490c4fc..df4c3ac 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java @@ -95,6 +95,20 @@ public class LazyPair return new HalfBoundLazyPair<>(rightSupp, rightBinder); } + @Override + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + BiFunction rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return bind((leftVal, rightVal) -> { + return new LazyPair<>( + leftCombiner.apply(leftVal, otherLeft), + rightCombiner.apply(rightVal, otherRight)); + }); + }); + } + @Override public LeftType getLeft() { if (!leftMaterialized) { @@ -117,47 +131,6 @@ public class LazyPair return rightValue; } - @Override - public MergedType merge( - BiFunction merger) { - if (!leftMaterialized) { - leftValue = leftSupplier.get(); - - leftMaterialized = true; - } - - if (!rightMaterialized) { - rightValue = rightSupplier.get(); - - rightMaterialized = true; - } - - return merger.apply(leftValue, rightValue); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("pair[l="); - - if (leftMaterialized) { - sb.append(leftValue.toString()); - } else { - sb.append("(un-materialized)"); - } - - sb.append(", r="); - - if (rightMaterialized) { - sb.append(rightValue.toString()); - } else { - sb.append("(un-materialized)"); - } - - sb.append("]"); - - return sb.toString(); - } - @Override public IPair mapLeft( Function mapper) { @@ -203,16 +176,43 @@ public class LazyPair } @Override - public IPair combine( - IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - return new LazyPair<>( - leftCombiner.apply(leftVal, otherLeft), - rightCombiner.apply(rightVal, otherRight)); - }); - }); + public MergedType merge( + BiFunction merger) { + if (!leftMaterialized) { + leftValue = leftSupplier.get(); + + leftMaterialized = true; + } + + if (!rightMaterialized) { + rightValue = rightSupplier.get(); + + rightMaterialized = true; + } + + return merger.apply(leftValue, rightValue); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("pair[l="); + + if (leftMaterialized) { + sb.append(leftValue.toString()); + } else { + sb.append("(un-materialized)"); + } + + sb.append(", r="); + + if (rightMaterialized) { + sb.append(rightValue.toString()); + } else { + sb.append("(un-materialized)"); + } + + sb.append("]"); + + return sb.toString(); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java index 03765ed..fc6180b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java @@ -17,10 +17,6 @@ import bjc.utils.funcdata.IList; public class ListHolder implements IHolder { private IList heldValues; - private ListHolder(IList toHold) { - heldValues = toHold; - } - /** * Create a new list holder * @@ -38,15 +34,26 @@ public class ListHolder implements IHolder { } } + private ListHolder(IList toHold) { + heldValues = toHold; + } + @Override public IHolder bind( Function> binder) { - IList> boundValues = heldValues - .map(binder); + IList> boundValues = heldValues.map(binder); return new BoundListHolder<>(boundValues); } + @Override + public Function> lift( + Function func) { + return (val) -> { + return new ListHolder<>(new FunctionalList<>(func.apply(val))); + }; + } + @Override public IHolder map( Function mapper) { @@ -68,12 +75,4 @@ public class ListHolder implements IHolder { Function unwrapper) { return unwrapper.apply(heldValues.randItem()); } - - @Override - public Function> lift( - Function func) { - return (val) -> { - return new ListHolder<>(new FunctionalList<>(func.apply(val))); - }; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Option.java b/BJC-Utils2/src/main/java/bjc/utils/data/Option.java index 9f6d448..16d90e3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Option.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Option.java @@ -34,6 +34,14 @@ public class Option implements IHolder { return binder.apply(held); } + @Override + public Function> lift( + Function func) { + return (val) -> { + return new Option<>(func.apply(val)); + }; + } + @Override public IHolder map( Function mapper) { @@ -63,12 +71,4 @@ public class Option implements IHolder { return unwrapper.apply(held); } - - @Override - public Function> lift( - Function func) { - return (val) -> { - return new Option<>(func.apply(val)); - }; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java index 1fc0d19..1000fc0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -70,24 +70,19 @@ public class Pair } @Override - public MergedType - merge(BiFunction merger) { - if (merger == null) { - throw new NullPointerException("Merger must not be null"); - } - - return merger.apply(leftValue, rightValue); - } - - @Override - public String toString() { - return "pair[l=" + leftValue.toString() + ", r=" - + rightValue.toString() + "]"; + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + BiFunction rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return new Pair<>(leftCombiner.apply(leftValue, otherLeft), + rightCombiner.apply(rightValue, otherRight)); + }); } @Override - public IPair - mapLeft(Function mapper) { + public IPair mapLeft( + Function mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } @@ -96,8 +91,8 @@ public class Pair } @Override - public IPair - mapRight(Function mapper) { + public IPair mapRight( + Function mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } @@ -106,14 +101,18 @@ public class Pair } @Override - public - IPair - combine(IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return new Pair<>(leftCombiner.apply(leftValue, otherLeft), - rightCombiner.apply(rightValue, otherRight)); - }); + public MergedType merge( + BiFunction merger) { + if (merger == null) { + throw new NullPointerException("Merger must not be null"); + } + + return merger.apply(leftValue, rightValue); + } + + @Override + public String toString() { + return "pair[l=" + leftValue.toString() + ", r=" + + rightValue.toString() + "]"; } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java index 989f1a5..f40ab29 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java @@ -3,6 +3,8 @@ package bjc.utils.data; import java.util.function.Supplier; public class SingleSupplier implements Supplier { + private static long nextID = 0; + private Supplier source; private boolean gotten; @@ -13,8 +15,6 @@ public class SingleSupplier implements Supplier { // instantiation was, in case of duplicate initiations private Exception instSite; - private static long nextID = 0; - public SingleSupplier(Supplier supp) { source = supp; diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/WrappedLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/WrappedLazy.java index 737482c..8ca29bc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/WrappedLazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/WrappedLazy.java @@ -6,6 +6,10 @@ import java.util.function.UnaryOperator; class WrappedLazy implements IHolder { private IHolder> held; + public WrappedLazy(IHolder wrappedHolder) { + held = new Lazy<>(wrappedHolder); + } + // This has an extra parameter, because otherwise it erases to the same // as the public one private WrappedLazy(IHolder> wrappedHolder, @@ -13,10 +17,6 @@ class WrappedLazy implements IHolder { held = wrappedHolder; } - public WrappedLazy(IHolder wrappedHolder) { - held = new Lazy<>(wrappedHolder); - } - @Override public IHolder bind( Function> binder) { @@ -28,6 +28,14 @@ class WrappedLazy implements IHolder { return new WrappedLazy<>(newHolder, false); } + @Override + public Function> lift( + Function func) { + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + @Override public IHolder map( Function mapper) { @@ -58,12 +66,4 @@ class WrappedLazy implements IHolder { return containedHolder.unwrap(unwrapper); }); } - - @Override - public Function> lift( - Function func) { - return (val) -> { - return new Lazy<>(func.apply(val)); - }; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/WrappedOption.java b/BJC-Utils2/src/main/java/bjc/utils/data/WrappedOption.java index c36cafa..5be55cc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/WrappedOption.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/WrappedOption.java @@ -6,15 +6,15 @@ import java.util.function.UnaryOperator; class WrappedOption implements IHolder { private IHolder> held; + public WrappedOption(IHolder seedValue) { + held = new Option<>(seedValue); + } + private WrappedOption(IHolder> toHold, @SuppressWarnings("unused") boolean dummy) { held = toHold; } - public WrappedOption(IHolder seedValue) { - held = new Option<>(seedValue); - } - @Override public IHolder bind( Function> binder) { @@ -32,6 +32,14 @@ class WrappedOption implements IHolder { return new WrappedOption<>(newHolder, false); } + @Override + public Function> lift( + Function func) { + return (val) -> { + return new Option<>(func.apply(val)); + }; + } + @Override public IHolder map( Function mapper) { @@ -78,12 +86,4 @@ class WrappedOption implements IHolder { }); }); } - - @Override - public Function> lift( - Function func) { - return (val) -> { - return new Option<>(func.apply(val)); - }; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java index 378a4a0..211f964 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java @@ -6,8 +6,7 @@ import java.util.function.Function; import bjc.utils.funcutils.ListUtils; -class ExtendedMap - implements IMap { +class ExtendedMap implements IMap { private IMap delegate; private IMap store; diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java index 638ad7e..590ed1d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -166,8 +166,7 @@ public class FunctionalList implements Cloneable, IList { * IFunctionalList, java.util.function.BiFunction) */ @Override - public IList combineWith( - IList rightList, + public IList combineWith(IList rightList, BiFunction itemCombiner) { if (rightList == null) { throw new NullPointerException( @@ -227,8 +226,7 @@ public class FunctionalList implements Cloneable, IList { * Function) */ @Override - public IList flatMap( - Function> elementExpander) { + public IList flatMap(Function> elementExpander) { if (elementExpander == null) { throw new NullPointerException("Expander must not be null"); } @@ -237,8 +235,7 @@ public class FunctionalList implements Cloneable, IList { this.wrappedList.size()); forEach(element -> { - IList expandedElement = elementExpander - .apply(element); + IList expandedElement = elementExpander.apply(element); if (expandedElement == null) { throw new NullPointerException( @@ -396,8 +393,7 @@ public class FunctionalList implements Cloneable, IList { * IFunctionalList) */ @Override - public IList> pairWith( - IList rightList) { + public IList> pairWith(IList rightList) { return combineWith(rightList, Pair::new); } @@ -407,8 +403,7 @@ public class FunctionalList implements Cloneable, IList { * @see bjc.utils.funcdata.IFunctionalList#partition(int) */ @Override - public IList> partition( - int numberPerPartition) { + public IList> partition(int numberPerPartition) { if (numberPerPartition < 1 || numberPerPartition > wrappedList.size()) { throw new IllegalArgumentException("" + numberPerPartition diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java index 18617d1..3714bcd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java @@ -155,8 +155,7 @@ public class FunctionalStringTokenizer { * The function to use to convert tokens. * @return A list containing all of the converted tokens. */ - public IList toList( - Function tokenTransformer) { + public IList toList(Function tokenTransformer) { if (tokenTransformer == null) { throw new NullPointerException("Transformer must not be null"); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java index 00ec653..e446b64 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java @@ -49,6 +49,29 @@ public interface IList { */ boolean anyMatch(Predicate matchPredicate); + /** + * Reduce the contents of this list using a collector + * + * @param + * The intermediate accumulation type + * @param + * The final, reduced type + * @param collector + * The collector to use for reduction + * @return The reduced list + */ + public default ReducedType collect( + Collector collector) { + BiConsumer accumulator = collector + .accumulator(); + + return reduceAux(collector.supplier().get(), (value, state) -> { + accumulator.accept(state, value); + + return state; + }, collector.finisher()); + } + /** * Combine this list with another one into a new list and merge the * results. Works sort of like a combined zip/map over resulting pairs. @@ -136,8 +159,8 @@ public interface IList { * The predicate to match by * @return A list containing all elements that match the predicate */ - IList - getMatching(Predicate matchPredicate); + IList getMatching( + Predicate matchPredicate); /** * Retrieve the size of the wrapped list @@ -164,8 +187,8 @@ public interface IList { * The function to apply to each element in the list * @return A new list containing the mapped elements of this list. */ - IList - map(Function elementTransformer); + IList map( + Function elementTransformer); /** * Zip two lists into a list of pairs @@ -178,8 +201,8 @@ public interface IList { * @return A list containing pairs of this element and the specified * list */ - IList> - pairWith(IList rightList); + IList> pairWith( + IList rightList); /** * Partition this list into a list of sublists @@ -198,16 +221,6 @@ public interface IList { */ void prepend(ContainedType item); - /** - * Select a random item from this list, using the provided random - * number generator. - * - * @param rnd - * The random number generator to use. - * @return A random element from this list. - */ - ContainedType randItem(Function rnd); - /** * Select a random item from the list, using a default random number * generator @@ -218,6 +231,16 @@ public interface IList { return randItem((num) -> (int) (Math.random() * num)); } + /** + * Select a random item from this list, using the provided random + * number generator. + * + * @param rnd + * The random number generator to use. + * @return A random element from this list. + */ + ContainedType randItem(Function rnd); + /** * Reduce this list to a single value, using a accumulative approach. * @@ -310,27 +333,4 @@ public interface IList { * @return An iterable view onto the list */ public Iterable toIterable(); - - /** - * Reduce the contents of this list using a collector - * - * @param - * The intermediate accumulation type - * @param - * The final, reduced type - * @param collector - * The collector to use for reduction - * @return The reduced list - */ - public default ReducedType collect( - Collector collector) { - BiConsumer accumulator = - collector.accumulator(); - - return reduceAux(collector.supplier().get(), (value, state) -> { - accumulator.accept(state, value); - - return state; - }, collector.finisher()); - } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java index 243e58a..a2426b2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java @@ -95,8 +95,7 @@ public interface IMap { * The function to use to transform values * @return The map where each value will be transformed after lookup */ - IMap mapValues( - Function transformer); + IMap mapValues(Function transformer); /** * Add an entry to the map diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java index cf54935..b620bd9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java @@ -18,8 +18,8 @@ import java.util.function.Function; */ final class TransformedValueMap implements IMap { - private IMap mapToTransform; - private Function transformer; + private IMap mapToTransform; + private Function transformer; public TransformedValueMap(IMap destMap, Function transform) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java index 34b70d5..834c124 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java @@ -16,12 +16,12 @@ import bjc.utils.funcutils.StringUtils; * @param */ public class Tree implements ITree { - private ContainedType data; + private ContainedType data; private IList> children; - private boolean hasChildren; + private boolean hasChildren; - private int childCount = 0; + private int childCount = 0; /** * Create a new leaf node in a tree @@ -134,11 +134,10 @@ public class Tree implements ITree { Function, NewType> nodeTransformer = nodeCollapser .apply(data); - IList collapsedChildren = children - .map((child) -> { - return child.collapse(leafTransform, nodeCollapser, - (subTreeVal) -> subTreeVal); - }); + IList collapsedChildren = children.map((child) -> { + return child.collapse(leafTransform, nodeCollapser, + (subTreeVal) -> subTreeVal); + }); return nodeTransformer.apply(collapsedChildren); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java index 9cfc9a4..865f4d6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java @@ -74,8 +74,8 @@ public class BinarySearchTree { * The distance from the pivot * @return Whether the adjusted pivot is with the list */ - private boolean adjustedPivotInBounds(IList elements, - int pivot, int pivotAdjustment) { + private boolean adjustedPivotInBounds(IList elements, int pivot, + int pivotAdjustment) { return (pivot - pivotAdjustment) >= 0 && (pivot + pivotAdjustment) < elements.getSize(); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java index e8c74fc..d3ebdac 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java @@ -13,6 +13,43 @@ import java.util.function.Function; * */ public interface Bifunctor { + /** + * Lift a pair of functions to a single function that maps over both + * parts of a pair + * + * @param + * The old left type of the pair + * @param + * The old right type of the pair + * @param + * The new left type of the pair + * @param + * The new right type of the pair + * @param leftFunc + * The function that maps over the left of the pair + * @param rightFunc + * The function that maps over the right of the pair + * @return A function that maps over both parts of the pair + */ + public default Function, Bifunctor> bimap( + Function leftFunc, + Function rightFunc) { + Function, Bifunctor> bimappedFunc = ( + argPair) -> { + Function, Bifunctor> leftMapper = argPair + . fmapLeft(leftFunc); + + Bifunctor leftMappedFunctor = leftMapper + .apply(argPair); + Function, Bifunctor> rightMapper = leftMappedFunctor + . fmapRight(rightFunc); + + return rightMapper.apply(leftMappedFunctor); + }; + + return bimappedFunc; + } + /** * Lift a function to operate over the left part of this pair * @@ -27,9 +64,8 @@ public interface Bifunctor { * pair * @return The function lifted to work over the left side of bifunctors */ - public - Function, Bifunctor> - fmapLeft(Function func); + public Function, Bifunctor> fmapLeft( + Function func); /** * Lift a function to operate over the right part of this pair @@ -46,50 +82,8 @@ public interface Bifunctor { * @return The function lifted to work over the right side of * bifunctors */ - public - Function, Bifunctor> - fmapRight(Function func); - - /** - * Lift a pair of functions to a single function that maps over both - * parts of a pair - * - * @param - * The old left type of the pair - * @param - * The old right type of the pair - * @param - * The new left type of the pair - * @param - * The new right type of the pair - * @param leftFunc - * The function that maps over the left of the pair - * @param rightFunc - * The function that maps over the right of the pair - * @return A function that maps over both parts of the pair - */ - public default - Function, Bifunctor> - bimap(Function leftFunc, - Function rightFunc) { - Function, Bifunctor> bimappedFunc = - (argPair) -> { - Function, Bifunctor> leftMapper = - argPair. fmapLeft( - leftFunc); - - Bifunctor leftMappedFunctor = - leftMapper.apply(argPair); - Function, Bifunctor> rightMapper = - leftMappedFunctor - . fmapRight( - rightFunc); - - return rightMapper.apply(leftMappedFunctor); - }; - - return bimappedFunc; - } + public Function, Bifunctor> fmapRight( + Function func); /** * Get the value contained on the left of this bifunctor diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CollectorUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CollectorUtils.java index a44059b..f7210b5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CollectorUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CollectorUtils.java @@ -31,11 +31,9 @@ public class CollectorUtils { * The second collector to use * @return A collector that functions as mentioned above */ - public static - Collector>, IPair> - compoundCollect( - Collector firstCollector, - Collector secondCollector) { + public static Collector>, IPair> compoundCollect( + Collector firstCollector, + Collector secondCollector) { return new CompoundCollector<>(firstCollector, secondCollector); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java index ed8d05f..60eb163 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java @@ -6,6 +6,7 @@ import java.util.function.BinaryOperator; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collector; + import bjc.utils.data.IHolder; import bjc.utils.data.IPair; import bjc.utils.data.Identity; @@ -30,19 +31,11 @@ final class CompoundCollector>> supplier() { - return () -> new Identity<>( - new Pair<>(firstCollector.supplier().get(), - secondCollector.supplier().get())); - } - - @Override - public BiConsumer>, InitialType> - accumulator() { - BiConsumer firstAccumulator = - firstCollector.accumulator(); - BiConsumer secondAccumulator = - secondCollector.accumulator(); + public BiConsumer>, InitialType> accumulator() { + BiConsumer firstAccumulator = firstCollector + .accumulator(); + BiConsumer secondAccumulator = secondCollector + .accumulator(); return (state, value) -> { state.doWith((statePair) -> { @@ -54,11 +47,16 @@ final class CompoundCollector characteristics() { + return characteristicSet; + } + @Override public BinaryOperator>> combiner() { BinaryOperator firstCombiner = firstCollector.combiner(); - BinaryOperator secondCombiner = - secondCollector.combiner(); + BinaryOperator secondCombiner = secondCollector + .combiner(); return (leftState, rightState) -> { return leftState.unwrap((leftPair) -> { @@ -71,8 +69,7 @@ final class CompoundCollector>, IPair> - finisher() { + public Function>, IPair> finisher() { return (state) -> { return state.unwrap((pair) -> { return pair.bind((leftVal, rightVal) -> { @@ -85,8 +82,9 @@ final class CompoundCollector - characteristics() { - return characteristicSet; + public Supplier>> supplier() { + return () -> new Identity<>( + new Pair<>(firstCollector.supplier().get(), + secondCollector.supplier().get())); } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java index 34a7ee0..81781f6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java @@ -16,17 +16,17 @@ import bjc.utils.funcdata.IList; * The type of element in the list being partitioned */ final class GroupPartIteration implements Consumer { - private IList> returnedList; - private IHolder> currentPartition; - private IList rejectedItems; - private IHolder numberInCurrentPartition; - private int numberPerPartition; - private Function elementCounter; + private IList> returnedList; + private IHolder> currentPartition; + private IList rejectedItems; + private IHolder numberInCurrentPartition; + private int numberPerPartition; + private Function elementCounter; public GroupPartIteration(IList> returned, - IHolder> currPart, - IList rejects, IHolder numInCurrPart, - int nPerPart, Function eleCount) { + IHolder> currPart, IList rejects, + IHolder numInCurrPart, int nPerPart, + Function eleCount) { this.returnedList = returned; this.currentPartition = currPart; this.rejectedItems = rejects; diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index 913ccc4..f09a794 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -78,8 +78,7 @@ public class ListUtils { * @return The tokens that have been deaffixed * */ - public static IList deAffixTokens( - IList input, + public static IList deAffixTokens(IList input, Deque> operators) { if (input == null) { throw new NullPointerException("Input must not be null"); @@ -88,8 +87,7 @@ public class ListUtils { "Set of operators must not be null"); } - IHolder> returnedList = new Identity<>( - input); + IHolder> returnedList = new Identity<>(input); operators.forEach((operator) -> returnedList .transform((oldReturn) -> oldReturn.flatMap((token) -> { @@ -115,9 +113,8 @@ public class ListUtils { * selected from the specified list without replacement */ - public static IList drawWithoutReplacement( - IList list, int numberOfItems, - Function rng) { + public static IList drawWithoutReplacement(IList list, + int numberOfItems, Function rng) { IList selectedItems = new FunctionalList<>( new ArrayList<>(numberOfItems)); @@ -153,9 +150,8 @@ public class ListUtils { * @return A new list containing the desired number of items randomly * selected from the specified list */ - public static IList drawWithReplacement( - IList list, int numberOfItems, - Function rng) { + public static IList drawWithReplacement(IList list, + int numberOfItems, Function rng) { IList selectedItems = new FunctionalList<>( new ArrayList<>(numberOfItems)); @@ -181,9 +177,8 @@ public class ListUtils { * The number of elements to put in each partition * @return A list partitioned according to the above rules */ - public static IList> groupPartition( - IList input, Function elementCounter, - int numberPerPartition) { + public static IList> groupPartition(IList input, + Function elementCounter, int numberPerPartition) { if (input == null) { throw new NullPointerException("Input list must not be null"); } else if (elementCounter == null) { @@ -254,8 +249,7 @@ public class ListUtils { * @return A list containing all the elements of the lists */ @SafeVarargs - public static IList mergeLists( - IList... lists) { + public static IList mergeLists(IList... lists) { IList returnedList = new FunctionalList<>(); for (IList list : lists) { @@ -279,8 +273,7 @@ public class ListUtils { * @return A list of tokens split on all the operators * */ - public static IList splitTokens( - IList input, + public static IList splitTokens(IList input, Deque> operators) { if (input == null) { throw new NullPointerException("Input must not be null"); @@ -289,8 +282,7 @@ public class ListUtils { "Set of operators must not be null"); } - IHolder> returnedList = new Identity<>( - input); + IHolder> returnedList = new Identity<>(input); operators.forEach((operator) -> { returnedList.transform((oldReturn) -> { diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java index 9401b7e..6ddf420 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java @@ -75,10 +75,15 @@ public class StringUtils { } /** - * Print out a deque with a special case for easily showing a deque is empty - * @param The type in the deque - * @param queue The deque to print - * @return A string version of the deque, with allowance for an empty deque + * Print out a deque with a special case for easily showing a deque is + * empty + * + * @param + * The type in the deque + * @param queue + * The deque to print + * @return A string version of the deque, with allowance for an empty + * deque */ public static String printDeque( Deque queue) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenDeaffixer.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenDeaffixer.java index 6ed4ecf..709538b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenDeaffixer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenDeaffixer.java @@ -14,8 +14,7 @@ final class TokenDeaffixer } @Override - public IList apply(String operatorName, - String operatorRegex) { + public IList apply(String operatorName, String operatorRegex) { if (operatorName == null) { throw new NullPointerException( "Operator name must not be null"); diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenSplitter.java index b9693a7..e2d59d3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenSplitter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenSplitter.java @@ -14,8 +14,7 @@ final class TokenSplitter } @Override - public IList apply(String operatorName, - String operatorRegex) { + public IList apply(String operatorName, String operatorRegex) { if (operatorName == null) { throw new NullPointerException( "Operator name must not be null"); diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java index a764a08..7ed476e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java @@ -59,8 +59,7 @@ public class RandomGrammar extends WeightedGrammar { * @param cases * The cases to add for this rule. */ - public void makeRule(E rule, - IList> cases) { + public void makeRule(E rule, IList> cases) { if (cases == null) { throw new NullPointerException("Cases must not be null"); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java index 78db7d8..fe10741 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -23,7 +23,7 @@ public class WeightedGrammar { /** * The initial rule of the grammar */ - protected String initialRule; + protected String initialRule; /** * The rules currently in this grammar @@ -33,12 +33,12 @@ public class WeightedGrammar { /** * The random number generator used for random numbers */ - private Random rng; + private Random rng; /** * All of the subgrammars of this grammar */ - protected IMap> subgrammars; + protected IMap> subgrammars; /** * Create a new weighted grammar. @@ -76,8 +76,7 @@ public class WeightedGrammar { * @param cse * The case being added. */ - public void addCase(E ruleName, int probability, - IList cse) { + public void addCase(E ruleName, int probability, IList cse) { if (ruleName == null) { throw new NullPointerException("Rule name must be not null"); } else if (cse == null) { @@ -145,8 +144,7 @@ public class WeightedGrammar { * The set of cases for the rule. * @return Whether or not the rule was succesfully added. */ - public boolean addRule(E name, - WeightedRandom> cases) { + public boolean addRule(E name, WeightedRandom> cases) { if (name == null) { throw new NullPointerException("Name must not be null"); } else if (cases == null) { @@ -222,16 +220,14 @@ public class WeightedGrammar { * The rule to test. * @return A set of sentances generated by the specified rule. */ - public IList> generateDebugValues( - E ruleName) { + public IList> generateDebugValues(E ruleName) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } IList> returnedList = new FunctionalList<>(); - WeightedRandom> ruleGenerator = rules - .get(ruleName); + WeightedRandom> ruleGenerator = rules.get(ruleName); for (int i = 0; i < 10; i++) { returnedList.add(ruleGenerator.generateValue()); diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java index 7c6af23..e345424 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -26,14 +26,14 @@ public class WeightedRandom { /** * The list of possible results to pick from */ - private IList results; + private IList results; /** * The source for any needed random numbers */ - private Random source; + private Random source; - private int totalChance; + private int totalChance; /** * Create a new weighted random generator with the specified source of diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/HolderOutputPanel.java b/BJC-Utils2/src/main/java/bjc/utils/gui/HolderOutputPanel.java index 49c7283..afd60cf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/HolderOutputPanel.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/HolderOutputPanel.java @@ -48,6 +48,19 @@ public class HolderOutputPanel extends JPanel { add(value); } + /** + * Set this panel back to its initial state + */ + public void reset() { + stopUpdating(); + + value.setText("(stopped)"); + + updateTimer = new Timer(nDelay, (event) -> { + value.setText(val.getValue()); + }); + } + /** * Start updating the contents of the field from the holder */ @@ -63,17 +76,4 @@ public class HolderOutputPanel extends JPanel { value.setText(value.getText() + " (stopped)"); } - - /** - * Set this panel back to its initial state - */ - public void reset() { - stopUpdating(); - - value.setText("(stopped)"); - - updateTimer = new Timer(nDelay, (event) -> { - value.setText(val.getValue()); - }); - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java index 5d044e1..67ca215 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java @@ -187,8 +187,8 @@ public class RuleBasedConfigReader { } private boolean startRule(E state, boolean ruleOpen, String line) { - FunctionalStringTokenizer tokenizer = - new FunctionalStringTokenizer(line, " "); + FunctionalStringTokenizer tokenizer = new FunctionalStringTokenizer( + line, " "); String nextToken = tokenizer.nextToken(); diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java index 9d9d1b1..2c77c5d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedReaderPragmas.java @@ -15,66 +15,63 @@ import bjc.utils.funcutils.ListUtils; public class RuleBasedReaderPragmas { /** - * Creates a pragma that takes any number of arguments and collapses - * them all into a single string + * Creates a pragma that takes a single integer argument * * @param * The type of state that goes along with this pragma * @param name * The name of this pragma, for error message purpose * @param consumer - * The function to invoke with the parsed string + * The function to invoke with the parsed integer * @return A pragma that functions as described above. */ - public static - BiConsumer - buildStringCollapser(String name, - BiConsumer consumer) { + public static BiConsumer buildInteger( + String name, BiConsumer consumer) { return (tokenizer, state) -> { if (!tokenizer.hasMoreTokens()) { throw new PragmaFormatException("Pragma " + name - + " requires one string argument"); + + " requires one integer argument"); } - consumer.accept(ListUtils.collapseTokens( - tokenizer.toList((strang) -> strang)), state); + String token = tokenizer.nextToken(); + + try { + consumer.accept(Integer.parseInt(token), state); + } catch (NumberFormatException nfex) { + PragmaFormatException pfex = new PragmaFormatException( + "Argument " + token + + " to version pragma isn't a valid integer. " + + "This pragma requires a integer argument"); + + pfex.initCause(nfex); + + throw pfex; + } }; } /** - * Creates a pragma that takes a single integer argument + * Creates a pragma that takes any number of arguments and collapses + * them all into a single string * * @param * The type of state that goes along with this pragma * @param name * The name of this pragma, for error message purpose * @param consumer - * The function to invoke with the parsed integer + * The function to invoke with the parsed string * @return A pragma that functions as described above. */ - public static - BiConsumer buildInteger( - String name, BiConsumer consumer) { + public static BiConsumer buildStringCollapser( + String name, BiConsumer consumer) { return (tokenizer, state) -> { if (!tokenizer.hasMoreTokens()) { throw new PragmaFormatException("Pragma " + name - + " requires one integer argument"); + + " requires one string argument"); } - String token = tokenizer.nextToken(); - - try { - consumer.accept(Integer.parseInt(token), state); - } catch (NumberFormatException nfex) { - PragmaFormatException pfex = - new PragmaFormatException("Argument " + token - + " to version pragma isn't a valid integer. " - + "This pragma requires a integer argument"); - - pfex.initCause(nfex); - - throw pfex; - } + consumer.accept(ListUtils.collapseTokens( + tokenizer.toList((strang) -> strang)), state); }; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java index 9fb64cf..1c1a6fb 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java @@ -29,8 +29,8 @@ final class TokenTransformer implements Consumer { }); } - private IPair>, ITree> - handleOperator(Deque> queuedASTs) { + private IPair>, ITree> handleOperator( + Deque> queuedASTs) { ITree newAST; if (isSpecialOperator.test(element)) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java index c703a72..67a74d9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -79,8 +79,8 @@ public class TreeConstructor { "Special operator determiner must not be null"); } - IHolder>, ITree>> initialState = - new Identity<>(new Pair<>(new LinkedList<>(), null)); + IHolder>, ITree>> initialState = new Identity<>( + new Pair<>(new LinkedList<>(), null)); tokens.forEach( new TokenTransformer<>(initialState, operatorPredicate, -- cgit v1.2.3