diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-28 16:44:36 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-28 16:44:36 -0400 |
| commit | dca8e9f586fd595a7995f07788318fb92b8cce79 (patch) | |
| tree | 4fdf216d4a30c2c663d4a429f79cfa471c8579c4 /BJC-Utils2/src/main/java/bjc/utils | |
| parent | b1317e5e62bb044cd8a676cb3fc2da86e9922caf (diff) | |
Format/Cleanup pass
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils')
47 files changed, 614 insertions, 660 deletions
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<String, ICommand> commandHandlers; - private IMap<String, ICommand> defaultHandlers; + private IMap<String, ICommand> commandHandlers; + private IMap<String, ICommand> defaultHandlers; // Contains help topics without an associated command - private IMap<String, ICommandHelp> helpTopics; + private IMap<String, ICommandHelp> helpTopics; // The action to execute upon encountering an unknown command - private BiConsumer<String, String[]> unknownCommandHandler; + private BiConsumer<String, String[]> unknownCommandHandler; // The functions to use for input/output - private Consumer<String> errorOutput; - private Consumer<String> normalOutput; + private Consumer<String> errorOutput; + private Consumer<String> 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 @@ -332,6 +332,11 @@ public class GenericCommandMode implements ICommandMode { } @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 - * "<command-name>\t<command-summary>" - * where anything in angle brackets should be filled in. + * "<command-name>\t<command-summary>" 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 @@ -44,6 +44,15 @@ public interface ICommandMode { } /** + * 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 * * @param command @@ -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<ComponentType extends IDescribedComponent> implements IComponentRepository<ComponentType> { // 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<String, ComponentType> 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<ComponentType extends IDescribedComponent> // Predicate to use to traverse all the files in a directory, but // not recurse into sub-directories - BiPredicate<Path, BasicFileAttributes> 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<Path, BasicFileAttributes> 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 { @@ -107,6 +106,11 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> } @Override + public IMap<String, ComponentType> getAll() { + return components; + } + + @Override public ComponentType getByName(String name) { return components.get(name); } @@ -117,11 +121,6 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> } @Override - public IMap<String, ComponentType> getAll() { - return components; - } - - @Override public String getSource() { return "Components read from directory " + sourceDirectory + "."; } @@ -141,8 +140,8 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> "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 @@ -14,6 +14,14 @@ import bjc.utils.funcdata.IMap; */ public interface IComponentRepository<ComponentType extends IDescribedComponent> { /** + * 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<String, ComponentType> getAll(); + + /** * Get a component with a specific name * * @param name @@ -33,14 +41,6 @@ public interface IComponentRepository<ComponentType extends IDescribedComponent> } /** - * 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<String, ComponentType> getAll(); - - /** * Get the source from which these components came * * @return 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<OldType, BoundContainedType> /* * The old value */ - private Supplier<IHolder<OldType>> oldSupplier; + private Supplier<IHolder<OldType>> oldSupplier; /* * The function to use to transform the old value into a new value */ - private Function<OldType, IHolder<BoundContainedType>> binder; + private Function<OldType, IHolder<BoundContainedType>> binder; /* * The bound value being held */ - private IHolder<BoundContainedType> boundHolder; + private IHolder<BoundContainedType> 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<UnaryOperator<BoundContainedType>> actions = - new FunctionalList<>(); + private IList<UnaryOperator<BoundContainedType>> actions = new FunctionalList<>(); /* * Create a new bound lazy value @@ -48,13 +47,12 @@ class BoundLazy<OldType, BoundContainedType> } @Override - public <BoundType> IHolder<BoundType> - bind(Function<BoundContainedType, IHolder<BoundType>> bindr) { + public <BoundType> IHolder<BoundType> bind( + Function<BoundContainedType, IHolder<BoundType>> bindr) { /* * Prepare a list of pending actions */ - IList<UnaryOperator<BoundContainedType>> pendingActions = - new FunctionalList<>(); + IList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); /* @@ -82,11 +80,18 @@ class BoundLazy<OldType, BoundContainedType> } @Override - public <MappedType> IHolder<MappedType> - map(Function<BoundContainedType, MappedType> mapper) { + public <NewType> Function<BoundContainedType, IHolder<NewType>> lift( + Function<BoundContainedType, NewType> func) { + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + + @Override + public <MappedType> IHolder<MappedType> map( + Function<BoundContainedType, MappedType> mapper) { // Prepare a list of pending actions - IList<UnaryOperator<BoundContainedType>> pendingActions = - new FunctionalList<>(); + IList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); // Prepare the new supplier @@ -117,28 +122,20 @@ class BoundLazy<OldType, BoundContainedType> } @Override - public IHolder<BoundContainedType> - transform(UnaryOperator<BoundContainedType> transformer) { + public IHolder<BoundContainedType> transform( + UnaryOperator<BoundContainedType> transformer) { actions.add(transformer); return this; } @Override - public <UnwrappedType> UnwrappedType - unwrap(Function<BoundContainedType, UnwrappedType> unwrapper) { + public <UnwrappedType> UnwrappedType unwrap( + Function<BoundContainedType, UnwrappedType> unwrapper) { if (!holderBound) { boundHolder = oldSupplier.get().unwrap(binder::apply); } return boundHolder.unwrap(unwrapper); } - - @Override - public <NewType> Function<BoundContainedType, IHolder<NewType>> - lift(Function<BoundContainedType, NewType> 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<OldLeft, OldRight, NewLeft, NewRight> } @Override - public <MergedType> MergedType merge( - BiFunction<NewLeft, NewRight, MergedType> 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 <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<NewRight, OtherRight, CombinedRight> 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<OldLeft, OldRight, NewLeft, NewRight> } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - IPair<OtherLeft, OtherRight> otherPair, - BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - return new LazyPair<>( - leftCombiner.apply(leftVal, otherLeft), - rightCombiner.apply(rightVal, otherRight)); - }); - }); + public <MergedType> MergedType merge( + BiFunction<NewLeft, NewRight, MergedType> 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<ContainedType> implements IHolder<ContainedType> { private IList<IHolder<ContainedType>> heldHolders; - public BoundListHolder( - IList<IHolder<ContainedType>> toHold) { + public BoundListHolder(IList<IHolder<ContainedType>> toHold) { heldHolders = toHold; } @@ -25,6 +24,14 @@ class BoundListHolder<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new ListHolder<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { IList<IHolder<MappedType>> mappedHolders = heldHolders @@ -50,12 +57,4 @@ class BoundListHolder<ContainedType> implements IHolder<ContainedType> { Function<ContainedType, UnwrappedType> unwrapper) { return heldHolders.randItem().unwrap(unwrapper); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> 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<LeftType, RightType> implements IPair<LeftType, RightType> { - 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<LeftType, RightType> * The value to put on the left * @return An either with the left side occupied */ - public static <LeftType, RightType> Either<LeftType, RightType> - fromLeft(LeftType left) { + public static <LeftType, RightType> Either<LeftType, RightType> fromLeft( + LeftType left) { return new Either<>(left, null); } - /** * Create a new either with the right value occupied * @@ -57,11 +41,27 @@ public class Either<LeftType, RightType> * The value to put on the right * @return An either with the right side occupied */ - public static <LeftType, RightType> Either<LeftType, RightType> - fromRight(RightType right) { + public static <LeftType, RightType> Either<LeftType, RightType> 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 <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) { @@ -89,8 +89,26 @@ public class Either<LeftType, RightType> } @Override - public <NewLeft> IPair<NewLeft, RightType> - mapLeft(Function<LeftType, NewLeft> mapper) { + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<RightType, OtherRight, CombinedRight> 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 <NewLeft> IPair<NewLeft, RightType> mapLeft( + Function<LeftType, NewLeft> mapper) { if (isLeft) { return new Either<>(mapper.apply(leftVal), null); } @@ -99,8 +117,8 @@ public class Either<LeftType, RightType> } @Override - public <NewRight> IPair<LeftType, NewRight> - mapRight(Function<RightType, NewRight> mapper) { + public <NewRight> IPair<LeftType, NewRight> mapRight( + Function<RightType, NewRight> mapper) { if (isLeft) { return new Either<>(leftVal, null); } @@ -109,27 +127,8 @@ public class Either<LeftType, RightType> } @Override - public <MergedType> MergedType - merge(BiFunction<LeftType, RightType, MergedType> merger) { + public <MergedType> MergedType merge( + BiFunction<LeftType, RightType, MergedType> merger) { return merger.apply(leftVal, rightVal); } - - @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> - IPair<CombinedLeft, CombinedRight> - combine(IPair<OtherLeft, OtherRight> otherPair, - BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<RightType, OtherRight, CombinedRight> 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<OldType, NewLeft, NewRight> } @Override - public <MergedType> MergedType - merge(BiFunction<NewLeft, NewRight, MergedType> merger) { - if (!pairBound) { - boundPair = binder.apply(oldSupplier.get()); - - pairBound = true; - } - - return boundPair.merge(merger); + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return bind((leftVal, rightVal) -> { + return new LazyPair<>( + leftCombiner.apply(leftVal, otherLeft), + rightCombiner.apply(rightVal, otherRight)); + }); + }); } @Override - public <NewLeftType> IPair<NewLeftType, NewRight> - mapLeft(Function<NewLeft, NewLeftType> mapper) { + public <NewLeftType> IPair<NewLeftType, NewRight> mapLeft( + Function<NewLeft, NewLeftType> mapper) { Supplier<NewLeftType> leftSupp = () -> { if (pairBound) { return mapper.apply(boundPair.getLeft()); @@ -116,8 +118,8 @@ class HalfBoundLazyPair<OldType, NewLeft, NewRight> } @Override - public <NewRightType> IPair<NewLeft, NewRightType> - mapRight(Function<NewRight, NewRightType> mapper) { + public <NewRightType> IPair<NewLeft, NewRightType> mapRight( + Function<NewRight, NewRightType> mapper) { Supplier<NewLeft> leftSupp = () -> { if (pairBound) { return boundPair.getLeft(); @@ -138,19 +140,16 @@ class HalfBoundLazyPair<OldType, NewLeft, NewRight> return new LazyPair<>(leftSupp, rightSupp); } - + @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> - IPair<CombinedLeft, CombinedRight> - combine(IPair<OtherLeft, OtherRight> otherPair, - BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - return new LazyPair<>( - leftCombiner.apply(leftVal, otherLeft), - rightCombiner.apply(rightVal, otherRight)); - }); - }); + public <MergedType> MergedType merge( + BiFunction<NewLeft, NewRight, MergedType> 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<ContainedType> extends Functor<ContainedType> { * The function to bind to the value * @return A holder from binding the value */ - public <BoundType> IHolder<BoundType> - bind(Function<ContainedType, IHolder<BoundType>> binder); + public <BoundType> IHolder<BoundType> bind( + Function<ContainedType, IHolder<BoundType>> binder); /** * Apply an action to the value @@ -43,9 +43,8 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { } @Override - default <ArgType, ReturnType> - Function<Functor<ArgType>, Functor<ReturnType>> - fmap(Function<ArgType, ReturnType> func) { + default <ArgType, ReturnType> Function<Functor<ArgType>, Functor<ReturnType>> fmap( + Function<ArgType, ReturnType> func) { return (argumentFunctor) -> { if (!(argumentFunctor instanceof IHolder<?>)) { throw new IllegalArgumentException( @@ -72,8 +71,8 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * The function to lift over the holder * @return The function lifted over the holder */ - public <NewType> Function<ContainedType, IHolder<NewType>> - lift(Function<ContainedType, NewType> func); + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func); /** * Make this holder lazy @@ -114,8 +113,8 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * The function to do mapping with * @return A holder with the mapped value */ - public <MappedType> IHolder<MappedType> - map(Function<ContainedType, MappedType> mapper); + public <MappedType> IHolder<MappedType> map( + Function<ContainedType, MappedType> mapper); /** * Replace the held value with a new one @@ -137,8 +136,8 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * The function to transform the value with * @return The holder itself, for easy chaining */ - public IHolder<ContainedType> - transform(UnaryOperator<ContainedType> transformer); + public IHolder<ContainedType> transform( + UnaryOperator<ContainedType> transformer); /** * Unwrap the value contained in this holder so that it is no longer @@ -150,6 +149,6 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * The function to use to unwrap the value * @return The unwrapped held value */ - public <UnwrappedType> UnwrappedType - unwrap(Function<ContainedType, UnwrappedType> unwrapper); + public <UnwrappedType> UnwrappedType unwrap( + Function<ContainedType, UnwrappedType> 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 @@ -57,6 +57,46 @@ public interface IPair<LeftType, RightType> Function<RightType, IPair<LeftType, BoundRight>> rightBinder); /** + * Pairwise combine two pairs together + * + * @param <OtherLeft> + * The left type of the other pair + * @param <OtherRight> + * The right type of the other pair + * @param otherPair + * The pair to combine with + * @return The pairs, pairwise combined together + */ + public default <OtherLeft, OtherRight> IPair<IPair<LeftType, OtherLeft>, IPair<RightType, OtherRight>> combine( + IPair<OtherLeft, OtherRight> otherPair) { + return combine(otherPair, + (left, otherLeft) -> new Pair<>(left, otherLeft), + (right, otherRight) -> new Pair<>(right, otherRight)); + } + + /** + * Combine the contents of two pairs together + * + * @param <OtherLeft> + * The type of the left value of the other pair + * @param <OtherRight> + * The type of the right value of the other pair + * @param <CombinedLeft> + * The type of the left value of the combined pair + * @param <CombinedRight> + * 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 <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<RightType, OtherRight, CombinedRight> rightCombiner); + + /** * Immediately perfom the specified action with the contents of this * pair * @@ -72,25 +112,22 @@ public interface IPair<LeftType, RightType> } @Override - default <OldLeft, OldRight, NewLeft> - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> - fmapLeft(Function<OldLeft, NewLeft> func) { + default <OldLeft, OldRight, NewLeft> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> fmapLeft( + Function<OldLeft, NewLeft> func) { return (argumentPair) -> { if (!(argumentPair instanceof IPair<?, ?>)) { throw new IllegalArgumentException( "This function can only be applied to instances of IPair"); } - IPair<OldLeft, OldRight> argPair = - (IPair<OldLeft, OldRight>) argumentPair; + IPair<OldLeft, OldRight> argPair = (IPair<OldLeft, OldRight>) argumentPair; return argPair.mapLeft(func); }; } @Override - default <OldLeft, OldRight, NewRight> - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<OldLeft, NewRight>> + default <OldLeft, OldRight, NewRight> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<OldLeft, NewRight>> fmapRight(Function<OldRight, NewRight> func) { return (argumentPair) -> { @@ -99,8 +136,7 @@ public interface IPair<LeftType, RightType> "This function can only be applied to instances of IPair"); } - IPair<OldLeft, OldRight> argPair = - (IPair<OldLeft, OldRight>) argumentPair; + IPair<OldLeft, OldRight> argPair = (IPair<OldLeft, OldRight>) argumentPair; return argPair.mapRight(func); }; @@ -137,8 +173,8 @@ public interface IPair<LeftType, RightType> * pair * @return The pair, with its left part transformed */ - public <NewLeft> IPair<NewLeft, RightType> - mapLeft(Function<LeftType, NewLeft> mapper); + public <NewLeft> IPair<NewLeft, RightType> mapLeft( + Function<LeftType, NewLeft> mapper); /** * Transform the value on the right side of the pair. Doesn't modify @@ -151,8 +187,8 @@ public interface IPair<LeftType, RightType> * pair * @return The pair, with its right part transformed */ - public <NewRight> IPair<LeftType, NewRight> - mapRight(Function<RightType, NewRight> mapper); + public <NewRight> IPair<LeftType, NewRight> mapRight( + Function<RightType, NewRight> mapper); /** * Merge the two values in this pair into a single value @@ -163,48 +199,6 @@ public interface IPair<LeftType, RightType> * The function to use for merging * @return The pair, merged into a single value */ - public <MergedType> MergedType - merge(BiFunction<LeftType, RightType, MergedType> merger); - - /** - * Combine the contents of two pairs together - * - * @param <OtherLeft> - * The type of the left value of the other pair - * @param <OtherRight> - * The type of the right value of the other pair - * @param <CombinedLeft> - * The type of the left value of the combined pair - * @param <CombinedRight> - * 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 <OtherLeft, OtherRight, CombinedLeft, CombinedRight> - IPair<CombinedLeft, CombinedRight> - combine(IPair<OtherLeft, OtherRight> otherPair, - BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<RightType, OtherRight, CombinedRight> rightCombiner); - - /** - * Pairwise combine two pairs together - * - * @param <OtherLeft> - * The left type of the other pair - * @param <OtherRight> - * The right type of the other pair - * @param otherPair - * The pair to combine with - * @return The pairs, pairwise combined together - */ - public default <OtherLeft, OtherRight> - IPair<IPair<LeftType, OtherLeft>, IPair<RightType, OtherRight>> - combine(IPair<OtherLeft, OtherRight> otherPair) { - return combine(otherPair, - (left, otherLeft) -> new Pair<>(left, otherLeft), - (right, otherRight) -> new Pair<>(right, otherRight)); - } + public <MergedType> MergedType merge( + BiFunction<LeftType, RightType, MergedType> 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 @@ -89,6 +89,14 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new Identity<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { return new Identity<>(mapper.apply(heldValue)); @@ -112,12 +120,4 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { Function<ContainedType, UnwrappedType> unwrapper) { return unwrapper.apply(heldValue); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> 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 @@ -76,6 +76,14 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>(); @@ -133,12 +141,4 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { return unwrapper.apply(heldValue); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> 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 @@ -96,6 +96,20 @@ public class LazyPair<LeftType, RightType> } @Override + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<RightType, OtherRight, CombinedRight> 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) { leftValue = leftSupplier.get(); @@ -118,47 +132,6 @@ public class LazyPair<LeftType, RightType> } @Override - public <MergedType> MergedType merge( - BiFunction<LeftType, RightType, MergedType> 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 <NewLeft> IPair<NewLeft, RightType> mapLeft( Function<LeftType, NewLeft> mapper) { Supplier<NewLeft> leftSupp = () -> { @@ -203,16 +176,43 @@ public class LazyPair<LeftType, RightType> } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - IPair<OtherLeft, OtherRight> otherPair, - BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - return new LazyPair<>( - leftCombiner.apply(leftVal, otherLeft), - rightCombiner.apply(rightVal, otherRight)); - }); - }); + public <MergedType> MergedType merge( + BiFunction<LeftType, RightType, MergedType> 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<ContainedType> implements IHolder<ContainedType> { private IList<ContainedType> heldValues; - private ListHolder(IList<ContainedType> toHold) { - heldValues = toHold; - } - /** * Create a new list holder * @@ -38,16 +34,27 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { } } + private ListHolder(IList<ContainedType> toHold) { + heldValues = toHold; + } + @Override public <BoundType> IHolder<BoundType> bind( Function<ContainedType, IHolder<BoundType>> binder) { - IList<IHolder<BoundType>> boundValues = heldValues - .map(binder); + IList<IHolder<BoundType>> boundValues = heldValues.map(binder); return new BoundListHolder<>(boundValues); } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new ListHolder<>(new FunctionalList<>(func.apply(val))); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { IList<MappedType> mappedValues = heldValues.map(mapper); @@ -68,12 +75,4 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { Function<ContainedType, UnwrappedType> unwrapper) { return unwrapper.apply(heldValues.randItem()); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> 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 @@ -35,6 +35,14 @@ public class Option<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new Option<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { if (held == null) { @@ -63,12 +71,4 @@ public class Option<ContainedType> implements IHolder<ContainedType> { return unwrapper.apply(held); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> 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<LeftType, RightType> } @Override - public <MergedType> MergedType - merge(BiFunction<LeftType, RightType, MergedType> 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 <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return new Pair<>(leftCombiner.apply(leftValue, otherLeft), + rightCombiner.apply(rightValue, otherRight)); + }); } @Override - public <NewLeft> IPair<NewLeft, RightType> - mapLeft(Function<LeftType, NewLeft> mapper) { + public <NewLeft> IPair<NewLeft, RightType> mapLeft( + Function<LeftType, NewLeft> mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } @@ -96,8 +91,8 @@ public class Pair<LeftType, RightType> } @Override - public <NewRight> IPair<LeftType, NewRight> - mapRight(Function<RightType, NewRight> mapper) { + public <NewRight> IPair<LeftType, NewRight> mapRight( + Function<RightType, NewRight> mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } @@ -106,14 +101,18 @@ public class Pair<LeftType, RightType> } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> - IPair<CombinedLeft, CombinedRight> - combine(IPair<OtherLeft, OtherRight> otherPair, - BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return new Pair<>(leftCombiner.apply(leftValue, otherLeft), - rightCombiner.apply(rightValue, otherRight)); - }); + public <MergedType> MergedType merge( + BiFunction<LeftType, RightType, MergedType> 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<T> implements Supplier<T> { + private static long nextID = 0; + private Supplier<T> source; private boolean gotten; @@ -13,8 +15,6 @@ public class SingleSupplier<T> implements Supplier<T> { // instantiation was, in case of duplicate initiations private Exception instSite; - private static long nextID = 0; - public SingleSupplier(Supplier<T> 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<ContainedType> implements IHolder<ContainedType> { private IHolder<IHolder<ContainedType>> held; + public WrappedLazy(IHolder<ContainedType> wrappedHolder) { + held = new Lazy<>(wrappedHolder); + } + // This has an extra parameter, because otherwise it erases to the same // as the public one private WrappedLazy(IHolder<IHolder<ContainedType>> wrappedHolder, @@ -13,10 +17,6 @@ class WrappedLazy<ContainedType> implements IHolder<ContainedType> { held = wrappedHolder; } - public WrappedLazy(IHolder<ContainedType> wrappedHolder) { - held = new Lazy<>(wrappedHolder); - } - @Override public <BoundType> IHolder<BoundType> bind( Function<ContainedType, IHolder<BoundType>> binder) { @@ -29,6 +29,14 @@ class WrappedLazy<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { IHolder<IHolder<MappedType>> newHolder = held @@ -58,12 +66,4 @@ class WrappedLazy<ContainedType> implements IHolder<ContainedType> { return containedHolder.unwrap(unwrapper); }); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> 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<ContainedType> implements IHolder<ContainedType> { private IHolder<IHolder<ContainedType>> held; + public WrappedOption(IHolder<ContainedType> seedValue) { + held = new Option<>(seedValue); + } + private WrappedOption(IHolder<IHolder<ContainedType>> toHold, @SuppressWarnings("unused") boolean dummy) { held = toHold; } - public WrappedOption(IHolder<ContainedType> seedValue) { - held = new Option<>(seedValue); - } - @Override public <BoundType> IHolder<BoundType> bind( Function<ContainedType, IHolder<BoundType>> binder) { @@ -33,6 +33,14 @@ class WrappedOption<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new Option<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { IHolder<IHolder<MappedType>> newHolder = held @@ -78,12 +86,4 @@ class WrappedOption<ContainedType> implements IHolder<ContainedType> { }); }); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> 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<KeyType, ValueType> - implements IMap<KeyType, ValueType> { +class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { private IMap<KeyType, ValueType> delegate; private IMap<KeyType, ValueType> 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<E> implements Cloneable, IList<E> { * IFunctionalList, java.util.function.BiFunction) */ @Override - public <T, F> IList<F> combineWith( - IList<T> rightList, + public <T, F> IList<F> combineWith(IList<T> rightList, BiFunction<E, T, F> itemCombiner) { if (rightList == null) { throw new NullPointerException( @@ -227,8 +226,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { * Function) */ @Override - public <T> IList<T> flatMap( - Function<E, IList<T>> elementExpander) { + public <T> IList<T> flatMap(Function<E, IList<T>> elementExpander) { if (elementExpander == null) { throw new NullPointerException("Expander must not be null"); } @@ -237,8 +235,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { this.wrappedList.size()); forEach(element -> { - IList<T> expandedElement = elementExpander - .apply(element); + IList<T> expandedElement = elementExpander.apply(element); if (expandedElement == null) { throw new NullPointerException( @@ -396,8 +393,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { * IFunctionalList) */ @Override - public <T> IList<IPair<E, T>> pairWith( - IList<T> rightList) { + public <T> IList<IPair<E, T>> pairWith(IList<T> rightList) { return combineWith(rightList, Pair<E, T>::new); } @@ -407,8 +403,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { * @see bjc.utils.funcdata.IFunctionalList#partition(int) */ @Override - public IList<IList<E>> partition( - int numberPerPartition) { + public IList<IList<E>> 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 <E> IList<E> toList( - Function<String, E> tokenTransformer) { + public <E> IList<E> toList(Function<String, E> 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 @@ -50,6 +50,29 @@ public interface IList<ContainedType> { boolean anyMatch(Predicate<ContainedType> matchPredicate); /** + * Reduce the contents of this list using a collector + * + * @param <StateType> + * The intermediate accumulation type + * @param <ReducedType> + * The final, reduced type + * @param collector + * The collector to use for reduction + * @return The reduced list + */ + public default <StateType, ReducedType> ReducedType collect( + Collector<ContainedType, StateType, ReducedType> collector) { + BiConsumer<StateType, ContainedType> 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. * Does not change the underlying list. @@ -136,8 +159,8 @@ public interface IList<ContainedType> { * The predicate to match by * @return A list containing all elements that match the predicate */ - IList<ContainedType> - getMatching(Predicate<ContainedType> matchPredicate); + IList<ContainedType> getMatching( + Predicate<ContainedType> matchPredicate); /** * Retrieve the size of the wrapped list @@ -164,8 +187,8 @@ public interface IList<ContainedType> { * The function to apply to each element in the list * @return A new list containing the mapped elements of this list. */ - <MappedType> IList<MappedType> - map(Function<ContainedType, MappedType> elementTransformer); + <MappedType> IList<MappedType> map( + Function<ContainedType, MappedType> elementTransformer); /** * Zip two lists into a list of pairs @@ -178,8 +201,8 @@ public interface IList<ContainedType> { * @return A list containing pairs of this element and the specified * list */ - <OtherType> IList<IPair<ContainedType, OtherType>> - pairWith(IList<OtherType> rightList); + <OtherType> IList<IPair<ContainedType, OtherType>> pairWith( + IList<OtherType> rightList); /** * Partition this list into a list of sublists @@ -199,16 +222,6 @@ public interface IList<ContainedType> { 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<Integer, Integer> rnd); - - /** * Select a random item from the list, using a default random number * generator * @@ -219,6 +232,16 @@ public interface IList<ContainedType> { } /** + * 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<Integer, Integer> rnd); + + /** * Reduce this list to a single value, using a accumulative approach. * * @param <StateType> @@ -310,27 +333,4 @@ public interface IList<ContainedType> { * @return An iterable view onto the list */ public Iterable<ContainedType> toIterable(); - - /** - * Reduce the contents of this list using a collector - * - * @param <StateType> - * The intermediate accumulation type - * @param <ReducedType> - * The final, reduced type - * @param collector - * The collector to use for reduction - * @return The reduced list - */ - public default <StateType, ReducedType> ReducedType collect( - Collector<ContainedType, StateType, ReducedType> collector) { - BiConsumer<StateType, ContainedType> 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<KeyType, ValueType> { * The function to use to transform values * @return The map where each value will be transformed after lookup */ - <V2> IMap<KeyType, V2> mapValues( - Function<ValueType, V2> transformer); + <V2> IMap<KeyType, V2> mapValues(Function<ValueType, V2> 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<OldKey, OldValue, NewValue> implements IMap<OldKey, NewValue> { - private IMap<OldKey, OldValue> mapToTransform; - private Function<OldValue, NewValue> transformer; + private IMap<OldKey, OldValue> mapToTransform; + private Function<OldValue, NewValue> transformer; public TransformedValueMap(IMap<OldKey, OldValue> destMap, Function<OldValue, NewValue> 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 <ContainedType> */ public class Tree<ContainedType> implements ITree<ContainedType> { - private ContainedType data; + private ContainedType data; private IList<ITree<ContainedType>> 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<ContainedType> implements ITree<ContainedType> { Function<IList<NewType>, NewType> nodeTransformer = nodeCollapser .apply(data); - IList<NewType> collapsedChildren = children - .map((child) -> { - return child.collapse(leafTransform, nodeCollapser, - (subTreeVal) -> subTreeVal); - }); + IList<NewType> 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<T> { * The distance from the pivot * @return Whether the adjusted pivot is with the list */ - private boolean adjustedPivotInBounds(IList<T> elements, - int pivot, int pivotAdjustment) { + private boolean adjustedPivotInBounds(IList<T> 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 @@ -14,6 +14,43 @@ import java.util.function.Function; */ public interface Bifunctor<LeftType, RightType> { /** + * Lift a pair of functions to a single function that maps over both + * parts of a pair + * + * @param <OldLeft> + * The old left type of the pair + * @param <OldRight> + * The old right type of the pair + * @param <NewLeft> + * The new left type of the pair + * @param <NewRight> + * 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 <OldLeft, OldRight, NewLeft, NewRight> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> bimap( + Function<OldLeft, NewLeft> leftFunc, + Function<OldRight, NewRight> rightFunc) { + Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> bimappedFunc = ( + argPair) -> { + Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> leftMapper = argPair + .<OldLeft, OldRight, NewLeft> fmapLeft(leftFunc); + + Bifunctor<NewLeft, OldRight> leftMappedFunctor = leftMapper + .apply(argPair); + Function<Bifunctor<NewLeft, OldRight>, Bifunctor<NewLeft, NewRight>> rightMapper = leftMappedFunctor + .<NewLeft, OldRight, NewRight> fmapRight(rightFunc); + + return rightMapper.apply(leftMappedFunctor); + }; + + return bimappedFunc; + } + + /** * Lift a function to operate over the left part of this pair * * @param <OldLeft> @@ -27,9 +64,8 @@ public interface Bifunctor<LeftType, RightType> { * pair * @return The function lifted to work over the left side of bifunctors */ - public <OldLeft, OldRight, NewLeft> - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> - fmapLeft(Function<OldLeft, NewLeft> func); + public <OldLeft, OldRight, NewLeft> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> fmapLeft( + Function<OldLeft, NewLeft> func); /** * Lift a function to operate over the right part of this pair @@ -46,50 +82,8 @@ public interface Bifunctor<LeftType, RightType> { * @return The function lifted to work over the right side of * bifunctors */ - public <OldLeft, OldRight, NewRight> - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<OldLeft, NewRight>> - fmapRight(Function<OldRight, NewRight> func); - - /** - * Lift a pair of functions to a single function that maps over both - * parts of a pair - * - * @param <OldLeft> - * The old left type of the pair - * @param <OldRight> - * The old right type of the pair - * @param <NewLeft> - * The new left type of the pair - * @param <NewRight> - * 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 <OldLeft, OldRight, NewLeft, NewRight> - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> - bimap(Function<OldLeft, NewLeft> leftFunc, - Function<OldRight, NewRight> rightFunc) { - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> bimappedFunc = - (argPair) -> { - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> leftMapper = - argPair.<OldLeft, OldRight, NewLeft> fmapLeft( - leftFunc); - - Bifunctor<NewLeft, OldRight> leftMappedFunctor = - leftMapper.apply(argPair); - Function<Bifunctor<NewLeft, OldRight>, Bifunctor<NewLeft, NewRight>> rightMapper = - leftMappedFunctor - .<NewLeft, OldRight, NewRight> fmapRight( - rightFunc); - - return rightMapper.apply(leftMappedFunctor); - }; - - return bimappedFunc; - } + public <OldLeft, OldRight, NewRight> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<OldLeft, NewRight>> fmapRight( + Function<OldRight, NewRight> 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 <InitialType, AuxType1, AuxType2, FinalType1, FinalType2> - Collector<InitialType, IHolder<IPair<AuxType1, AuxType2>>, IPair<FinalType1, FinalType2>> - compoundCollect( - Collector<InitialType, AuxType1, FinalType1> firstCollector, - Collector<InitialType, AuxType2, FinalType2> secondCollector) { + public static <InitialType, AuxType1, AuxType2, FinalType1, FinalType2> Collector<InitialType, IHolder<IPair<AuxType1, AuxType2>>, IPair<FinalType1, FinalType2>> compoundCollect( + Collector<InitialType, AuxType1, FinalType1> firstCollector, + Collector<InitialType, AuxType2, FinalType2> 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<InitialType, AuxType1, AuxType2, FinalType1, Final } @Override - public Supplier<IHolder<IPair<AuxType1, AuxType2>>> supplier() { - return () -> new Identity<>( - new Pair<>(firstCollector.supplier().get(), - secondCollector.supplier().get())); - } - - @Override - public BiConsumer<IHolder<IPair<AuxType1, AuxType2>>, InitialType> - accumulator() { - BiConsumer<AuxType1, InitialType> firstAccumulator = - firstCollector.accumulator(); - BiConsumer<AuxType2, InitialType> secondAccumulator = - secondCollector.accumulator(); + public BiConsumer<IHolder<IPair<AuxType1, AuxType2>>, InitialType> accumulator() { + BiConsumer<AuxType1, InitialType> firstAccumulator = firstCollector + .accumulator(); + BiConsumer<AuxType2, InitialType> secondAccumulator = secondCollector + .accumulator(); return (state, value) -> { state.doWith((statePair) -> { @@ -55,10 +48,15 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final } @Override + public Set<java.util.stream.Collector.Characteristics> characteristics() { + return characteristicSet; + } + + @Override public BinaryOperator<IHolder<IPair<AuxType1, AuxType2>>> combiner() { BinaryOperator<AuxType1> firstCombiner = firstCollector.combiner(); - BinaryOperator<AuxType2> secondCombiner = - secondCollector.combiner(); + BinaryOperator<AuxType2> secondCombiner = secondCollector + .combiner(); return (leftState, rightState) -> { return leftState.unwrap((leftPair) -> { @@ -71,8 +69,7 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final } @Override - public Function<IHolder<IPair<AuxType1, AuxType2>>, IPair<FinalType1, FinalType2>> - finisher() { + public Function<IHolder<IPair<AuxType1, AuxType2>>, IPair<FinalType1, FinalType2>> finisher() { return (state) -> { return state.unwrap((pair) -> { return pair.bind((leftVal, rightVal) -> { @@ -85,8 +82,9 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final } @Override - public Set<java.util.stream.Collector.Characteristics> - characteristics() { - return characteristicSet; + public Supplier<IHolder<IPair<AuxType1, AuxType2>>> 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<E> implements Consumer<E> { - private IList<IList<E>> returnedList; - private IHolder<IList<E>> currentPartition; - private IList<E> rejectedItems; - private IHolder<Integer> numberInCurrentPartition; - private int numberPerPartition; - private Function<E, Integer> elementCounter; + private IList<IList<E>> returnedList; + private IHolder<IList<E>> currentPartition; + private IList<E> rejectedItems; + private IHolder<Integer> numberInCurrentPartition; + private int numberPerPartition; + private Function<E, Integer> elementCounter; public GroupPartIteration(IList<IList<E>> returned, - IHolder<IList<E>> currPart, - IList<E> rejects, IHolder<Integer> numInCurrPart, - int nPerPart, Function<E, Integer> eleCount) { + IHolder<IList<E>> currPart, IList<E> rejects, + IHolder<Integer> numInCurrPart, int nPerPart, + Function<E, Integer> 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<String> deAffixTokens( - IList<String> input, + public static IList<String> deAffixTokens(IList<String> input, Deque<IPair<String, String>> 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<IList<String>> returnedList = new Identity<>( - input); + IHolder<IList<String>> 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 <E> IList<E> drawWithoutReplacement( - IList<E> list, int numberOfItems, - Function<Integer, Integer> rng) { + public static <E> IList<E> drawWithoutReplacement(IList<E> list, + int numberOfItems, Function<Integer, Integer> rng) { IList<E> 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 <E> IList<E> drawWithReplacement( - IList<E> list, int numberOfItems, - Function<Integer, Integer> rng) { + public static <E> IList<E> drawWithReplacement(IList<E> list, + int numberOfItems, Function<Integer, Integer> rng) { IList<E> 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 <E> IList<IList<E>> groupPartition( - IList<E> input, Function<E, Integer> elementCounter, - int numberPerPartition) { + public static <E> IList<IList<E>> groupPartition(IList<E> input, + Function<E, Integer> 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 <E> IList<E> mergeLists( - IList<E>... lists) { + public static <E> IList<E> mergeLists(IList<E>... lists) { IList<E> returnedList = new FunctionalList<>(); for (IList<E> list : lists) { @@ -279,8 +273,7 @@ public class ListUtils { * @return A list of tokens split on all the operators * */ - public static IList<String> splitTokens( - IList<String> input, + public static IList<String> splitTokens(IList<String> input, Deque<IPair<String, String>> 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<IList<String>> returnedList = new Identity<>( - input); + IHolder<IList<String>> 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 <ContainedType> 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 <ContainedType> + * 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 <ContainedType> String printDeque( Deque<ContainedType> 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<String> apply(String operatorName, - String operatorRegex) { + public IList<String> 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<String> apply(String operatorName, - String operatorRegex) { + public IList<String> 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<E> extends WeightedGrammar<E> { * @param cases * The cases to add for this rule. */ - public void makeRule(E rule, - IList<IList<E>> cases) { + public void makeRule(E rule, IList<IList<E>> 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<E> { /** * 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<E> { /** * The random number generator used for random numbers */ - private Random rng; + private Random rng; /** * All of the subgrammars of this grammar */ - protected IMap<E, WeightedGrammar<E>> subgrammars; + protected IMap<E, WeightedGrammar<E>> subgrammars; /** * Create a new weighted grammar. @@ -76,8 +76,7 @@ public class WeightedGrammar<E> { * @param cse * The case being added. */ - public void addCase(E ruleName, int probability, - IList<E> cse) { + public void addCase(E ruleName, int probability, IList<E> cse) { if (ruleName == null) { throw new NullPointerException("Rule name must be not null"); } else if (cse == null) { @@ -145,8 +144,7 @@ public class WeightedGrammar<E> { * The set of cases for the rule. * @return Whether or not the rule was succesfully added. */ - public boolean addRule(E name, - WeightedRandom<IList<E>> cases) { + public boolean addRule(E name, WeightedRandom<IList<E>> cases) { if (name == null) { throw new NullPointerException("Name must not be null"); } else if (cases == null) { @@ -222,16 +220,14 @@ public class WeightedGrammar<E> { * The rule to test. * @return A set of sentances generated by the specified rule. */ - public IList<IList<E>> generateDebugValues( - E ruleName) { + public IList<IList<E>> generateDebugValues(E ruleName) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } IList<IList<E>> returnedList = new FunctionalList<>(); - WeightedRandom<IList<E>> ruleGenerator = rules - .get(ruleName); + WeightedRandom<IList<E>> 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<E> { /** * The list of possible results to pick from */ - private IList<E> results; + private IList<E> 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 @@ -49,6 +49,19 @@ public class HolderOutputPanel extends JPanel { } /** + * 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 */ public void startUpdating() { @@ -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<E> { } 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 <StateType> * 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 <StateType> - BiConsumer<FunctionalStringTokenizer, StateType> - buildStringCollapser(String name, - BiConsumer<String, StateType> consumer) { + public static <StateType> BiConsumer<FunctionalStringTokenizer, StateType> buildInteger( + String name, BiConsumer<Integer, StateType> 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 <StateType> * 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 <StateType> - BiConsumer<FunctionalStringTokenizer, StateType> buildInteger( - String name, BiConsumer<Integer, StateType> consumer) { + public static <StateType> BiConsumer<FunctionalStringTokenizer, StateType> buildStringCollapser( + String name, BiConsumer<String, StateType> 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<TokenType> implements Consumer<TokenType> { }); } - private IPair<Deque<ITree<TokenType>>, ITree<TokenType>> - handleOperator(Deque<ITree<TokenType>> queuedASTs) { + private IPair<Deque<ITree<TokenType>>, ITree<TokenType>> handleOperator( + Deque<ITree<TokenType>> queuedASTs) { ITree<TokenType> 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<IPair<Deque<ITree<TokenType>>, ITree<TokenType>>> initialState = - new Identity<>(new Pair<>(new LinkedList<>(), null)); + IHolder<IPair<Deque<ITree<TokenType>>, ITree<TokenType>>> initialState = new Identity<>( + new Pair<>(new LinkedList<>(), null)); tokens.forEach( new TokenTransformer<>(initialState, operatorPredicate, |
