From fefd6eb2917b9a0856c247353545cc13876b6eda Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 1 Dec 2020 20:19:34 -0500 Subject: An assortment of changes/new things --- .../main/java/bjc/utils/funcutils/Callables.java | 60 ++++++++++++ .../java/bjc/utils/funcutils/ChainIterator.java | 54 +++++++++++ .../main/java/bjc/utils/funcutils/FuncUtils.java | 34 ++++--- .../main/java/bjc/utils/funcutils/Isomorphism.java | 59 ------------ .../java/bjc/utils/funcutils/IteratorUtils.java | 102 ++++++++++----------- .../bjc/utils/funcutils/QueueBackedIterator.java | 36 ++++++++ .../main/java/bjc/utils/funcutils/Strategy.java | 81 ++++++++++++++++ .../java/bjc/utils/patterns/ComplexPattern.java | 90 +++++++++++++----- .../bjc/utils/patterns/MutablePatternMatcher.java | 13 +-- base/src/main/java/bjc/utils/patterns/Pattern.java | 6 -- 10 files changed, 376 insertions(+), 159 deletions(-) create mode 100644 base/src/main/java/bjc/utils/funcutils/Callables.java create mode 100644 base/src/main/java/bjc/utils/funcutils/ChainIterator.java delete mode 100644 base/src/main/java/bjc/utils/funcutils/Isomorphism.java create mode 100644 base/src/main/java/bjc/utils/funcutils/QueueBackedIterator.java create mode 100644 base/src/main/java/bjc/utils/funcutils/Strategy.java (limited to 'base/src/main/java') diff --git a/base/src/main/java/bjc/utils/funcutils/Callables.java b/base/src/main/java/bjc/utils/funcutils/Callables.java new file mode 100644 index 0000000..5895347 --- /dev/null +++ b/base/src/main/java/bjc/utils/funcutils/Callables.java @@ -0,0 +1,60 @@ +package bjc.utils.funcutils; + +import java.util.concurrent.*; +import java.util.function.*; + +/** + * Utility function for dealing with callables and other things. + * + * @author Ben Culkin + * + */ +public class Callables +{ + /** + * Perform a 'bind' that appends a function to a callable. + * + * @param The type originally returned by the callable. + * @param The type returned by the function. + * + * @param call The original callable. + * @param func The function to use to transform the result. + * + * @return A callable which applies the given function to the result of them. + */ + public static Callable bind( + Callable call, Function> func) + { + return () -> func.apply(call.call()).call(); + } + + /** + * Convert a normal function to a function on callables. + * + * @param The input to the function. + * @param The output from the function. + * + * @param func The function to convert. + * + * @return The function, made to work over callables. + */ + public static Function, Callable> + fmap(Function func) + { + return (inp) -> () -> func.apply(inp.call()); + } + + /** + * Convert a future into a callable. + * + * @param The type returned by the future. + * + * @param fut The future to convert. + * + * @return A future which yields that value. + */ + public static Callable obtain(Future fut) + { + return () -> fut.get(); + } +} diff --git a/base/src/main/java/bjc/utils/funcutils/ChainIterator.java b/base/src/main/java/bjc/utils/funcutils/ChainIterator.java new file mode 100644 index 0000000..36f94e5 --- /dev/null +++ b/base/src/main/java/bjc/utils/funcutils/ChainIterator.java @@ -0,0 +1,54 @@ +package bjc.utils.funcutils; + +import java.util.*; +import java.util.function.*; + +/** + * A chain iterator. This is essentially flatMap in iterator form. + * + * @author bjculkin + * + * @param + * The type of the input values. + * + * @param + * The type of the output values. + */ +public class ChainIterator implements Iterator { + private Iterator mainItr; + private Function> trans; + + private Iterator curItr; + + /** + * Create a new chain iterator. + * + * @param mainItr + * The main iterator for input. + * + * @param trans + * The transformation to use to produce the outputs. + */ + public ChainIterator(Iterator mainItr, Function> trans) { + this.mainItr = mainItr; + this.trans = trans; + } + + @Override + public boolean hasNext() { + if (curItr != null) { + return curItr.hasNext() ? true : mainItr.hasNext(); + } + + return mainItr.hasNext(); + } + + @Override + public T2 next() { + if (curItr == null || !curItr.hasNext()) { + curItr = trans.apply(mainItr.next()); + } + + return curItr.next(); + } +} \ No newline at end of file diff --git a/base/src/main/java/bjc/utils/funcutils/FuncUtils.java b/base/src/main/java/bjc/utils/funcutils/FuncUtils.java index 70e521a..2c65876 100644 --- a/base/src/main/java/bjc/utils/funcutils/FuncUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/FuncUtils.java @@ -29,9 +29,12 @@ public class FuncUtils { * * @return The function transformed into a unary function returning a function. */ - public static Function> - curry2(final BiFunction func) { - return arg1 -> arg2 -> func.apply(arg1, arg2); + public static Function> curry2( + final BiFunction func) + { + return arg1 -> + arg2 -> + func.apply(arg1, arg2); } /** @@ -43,14 +46,17 @@ public class FuncUtils { * @param cons * The action to perform. */ - public static void doTimes(final int nTimes, final Consumer cons) { - for (int i = 0; i < nTimes; i++) { - cons.accept(i); - } + public static void doTimes( + final int nTimes, + final Consumer cons) + { + for (int i = 0; i < nTimes; i++) cons.accept(i); } /** * Return an operator that executes until it converges. + * + * @param The type the operator is on. * * @param op * The operator to execute. @@ -62,12 +68,15 @@ public class FuncUtils { * @return The requested operator. */ public static UnaryOperator converge(final UnaryOperator op, - final int maxTries) { + final int maxTries) + { return converge(op, Object::equals, maxTries); } /** * Return an operator that executes until it converges. + * + * @param The type the operator is on. * * @param op * The operator to execute. @@ -81,11 +90,14 @@ public class FuncUtils { * * @return The requested operator. */ - public static UnaryOperator converge(final UnaryOperator op, - final BiPredicate converged, final int maxTries) { + public static UnaryOperator converge( + final UnaryOperator op, + final BiPredicate converged, + final int maxTries) + { return val -> { T newVal = op.apply(val); - T oldVal; + T oldVal = newVal; int tries = 0; diff --git a/base/src/main/java/bjc/utils/funcutils/Isomorphism.java b/base/src/main/java/bjc/utils/funcutils/Isomorphism.java deleted file mode 100644 index c219d7f..0000000 --- a/base/src/main/java/bjc/utils/funcutils/Isomorphism.java +++ /dev/null @@ -1,59 +0,0 @@ -package bjc.utils.funcutils; - -import java.util.function.Function; - -/** - * A pair of functions to transform between a pair of types. - * - * @author bjculkin - * - * @param - * The source type of the isomorphism. - * - * @param - * The destination type of isomorphism. - */ -public class Isomorphism { - /* The function to the destination type. */ - private Function toFunc; - /* The function to the source type. */ - private Function fromFunc; - - /** - * Create a new isomorphism. - * - * @param to - * The 'forward' function, from the source to the definition. - * - * @param from - * The 'backward' function, from the definition to the source. - */ - public Isomorphism(Function to, Function from) { - toFunc = to; - fromFunc = from; - } - - /** - * Apply the isomorphism forward. - * - * @param val - * The source value. - * - * @return The destination value. - */ - public D to(S val) { - return toFunc.apply(val); - } - - /** - * Apply the isomorphism backward. - * - * @param val - * The destination value. - * - * @return The source value. - */ - public S from(D val) { - return fromFunc.apply(val); - } -} diff --git a/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java b/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java index 8d51996..ea67295 100644 --- a/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java @@ -3,7 +3,7 @@ package bjc.utils.funcutils; import java.util.*; import java.util.function.*; -import bjc.data.ArrayIterator; +import bjc.data.*; /** * Utility methods for dealing with iterators. @@ -12,58 +12,10 @@ import bjc.data.ArrayIterator; * */ public class IteratorUtils { - /** - * A chain iterator. This is essentially flatMap in iterator form. - * - * @author bjculkin - * - * @param - * The type of the input values. - * - * @param - * The type of the output values. - */ - public static class ChainIterator implements Iterator { - private Iterator mainItr; - private Function> trans; - - private Iterator curItr; - - /** - * Create a new chain iterator. - * - * @param mainItr - * The main iterator for input. - * - * @param trans - * The transformation to use to produce the outputs. - */ - public ChainIterator(Iterator mainItr, Function> trans) { - this.mainItr = mainItr; - this.trans = trans; - } - - @Override - public boolean hasNext() { - if (curItr != null) { - return curItr.hasNext() ? true : mainItr.hasNext(); - } - - return mainItr.hasNext(); - } - - @Override - public T2 next() { - if (curItr == null || !curItr.hasNext()) { - curItr = trans.apply(mainItr.next()); - } - - return curItr.next(); - } - } - /** * Convert an iterator to an iterable. + * + * @param The type being iterated over. * * @param itr * The iterator to convert. @@ -77,6 +29,8 @@ public class IteratorUtils { /** * Convert an iterable to an iterator. * + * @param The type being iterated over. + * * @param itr * The iterable to convert. * @@ -89,18 +43,23 @@ public class IteratorUtils { /** * Convert an array to an iterator. * + * @param The type being iterated over. + * * @param parms * The array to iterate over. * * @return An iterator over the provided array. */ @SafeVarargs - public static Iterator AI(E... parms) { + public static Iterator I(E... parms) { return new ArrayIterator<>(parms); } /** * Create a chain iterator. + * + * @param The initial type being iterated over. + * @param The resulting type being iterated over. * * @param itrA * The iterator for input values. @@ -114,4 +73,43 @@ public class IteratorUtils { Function> itrB) { return new ChainIterator<>(itrA, itrB); } + + /** + * Perform a left-fold over an iterator. + * + * @param The type of elements in the iterator. + * @param The result from the fold. + * + * @param itr The items to iterate over. + * @param zero The initial element for the fold. + * @param folder The function that does the folding. + * + * @return The result of folding over the iterator. + */ + public static ResultType foldLeft( + Iterable itr, + ResultType zero, + BiFunction folder) + { + ResultType state = zero; + for (ElementType elem : itr) { + state = folder.apply(elem, state); + } + return state; + } + + /** + * Creates an 'entangled' pair of a consumer and an iterator. + * + * @param The type of value involved. + * + * @return A pair consisting of a consumer of values, and an iterator that + * yields the consumed values. + */ + public static + IPair, Iterator> entangle() + { + Queue backer = new ArrayDeque<>(); + return IPair.pair(backer::add, new QueueBackedIterator<>(backer)); + } } diff --git a/base/src/main/java/bjc/utils/funcutils/QueueBackedIterator.java b/base/src/main/java/bjc/utils/funcutils/QueueBackedIterator.java new file mode 100644 index 0000000..8b9f401 --- /dev/null +++ b/base/src/main/java/bjc/utils/funcutils/QueueBackedIterator.java @@ -0,0 +1,36 @@ +package bjc.utils.funcutils; + +import java.util.*; + +/** + * An iterator backed by a queue. + * + * @author Ben Culkin + * + * @param The type of element + */ +public class QueueBackedIterator + implements Iterator +{ + private final Queue backer; + + /** + * Create a new queue-backed iterator. + * + * @param backer The queue which backs this iterator. + */ + public QueueBackedIterator(Queue backer) + { + this.backer = backer; + } + + @Override + public boolean hasNext() { + return !backer.isEmpty(); + } + + @Override + public ElementType next() { + return backer.remove(); + } +} \ No newline at end of file diff --git a/base/src/main/java/bjc/utils/funcutils/Strategy.java b/base/src/main/java/bjc/utils/funcutils/Strategy.java new file mode 100644 index 0000000..316879f --- /dev/null +++ b/base/src/main/java/bjc/utils/funcutils/Strategy.java @@ -0,0 +1,81 @@ +package bjc.utils.funcutils; + +import java.util.concurrent.*; +import java.util.function.*; + +/** + * Strategy for dealing with parallel execution. + * + * @author Ben Culkin + * + * @param The type returned by the tasks. + * + */ +public interface Strategy extends Function, Future> +{ + /** + * Convert a function into one which operates concurrently, using this strategy. + * + * @param The type of the function argument. + * + * @param func The type of the function. + * + * @return A function which executes concurrently. + */ + public default Function> using(Function func) + { + return (input) -> this.apply(() -> func.apply(input)); + } + + /** + * A strategy which will run tasks in serial. + * + * @param The type returned by the task. + * + * @return A strategy which executes things serially. + */ + public static Strategy serial() + { + return (call) -> { + FutureTask task = new FutureTask<>(call); + task.run(); + return task; + }; + } + /** + * A strategy which creates a fresh thread to execute a task on. + * + * @param The type returned by the task. + * + * @return A strategy which uses threads to create tasks. + */ + public static Strategy simpleThread() + { + // I leave this as an example as of what is possible with combinators. + // return (call) -> invoke(introducing( + // () -> new FutureTask<>(call), + // (task, input) -> doWith( + // (FutureTask tsk) -> + // new Thread(task).start()).apply(task) + // )); + return (call) -> { + FutureTask task = new FutureTask<>(call); + new Thread(task).start(); + return task; + }; + } + + /** + * A strategy that uses an executor service. + * + * @param The type returned by the task. + * + * @param svc The executor service to use. + * + * @return A strategy which uses the provided executor. + */ + public static Strategy executorService(ExecutorService svc) + { + return svc::submit; + } +} diff --git a/base/src/main/java/bjc/utils/patterns/ComplexPattern.java b/base/src/main/java/bjc/utils/patterns/ComplexPattern.java index 3926f2c..e9035df 100644 --- a/base/src/main/java/bjc/utils/patterns/ComplexPattern.java +++ b/base/src/main/java/bjc/utils/patterns/ComplexPattern.java @@ -35,6 +35,8 @@ public interface ComplexPattern { */ ReturnType apply(InputType input, PredType state); + /* Pattern producing functions */ + /** * Create a pattern composed from a predicate & a function. * @@ -49,7 +51,8 @@ public interface ComplexPattern { */ static ComplexPattern from( Function> matcher, - BiFunction accepter) { + BiFunction accepter) + { return new FunctionalPattern<>(matcher, accepter); } @@ -68,7 +71,8 @@ public interface ComplexPattern { @SuppressWarnings("unchecked") static ComplexPattern ofClass( Class clasz, - Function action) { + Function action) + { return from( (input) -> IPair.pair(clasz.isInstance(input), null), (input, ignored) -> action.apply((ClassType)input) @@ -89,11 +93,12 @@ public interface ComplexPattern { static ComplexPattern matchesObject( InpType obj, Function action - ) { + ) + { return from( - (input) -> IPair.pair(obj.equals(input), null), - (input, ignored) -> action.apply(input) - ); + (input) -> IPair.pair(obj.equals(input), null), + (input, ignored) -> action.apply(input) + ); } /** @@ -113,13 +118,16 @@ public interface ComplexPattern { static ComplexPattern equalsString( String pattern, BiFunction action - ) { + ) + { + Function> matcher = (input) -> { + String objString = input.toString(); + + return IPair.pair(pattern.equals(objString), objString); + }; + return from( - (input) -> { - String objString = input.toString(); - - return IPair.pair(pattern.equals(objString), objString); - }, + matcher, (input, objString) -> action.apply(input, objString) ); } @@ -140,21 +148,21 @@ public interface ComplexPattern { String regex, Predicate cond, BiFunction action - ) { + ) + { java.util.regex.Pattern regexPat = java.util.regex.Pattern.compile(regex); + Function> matcher = (input) -> { + String inpString = input.toString(); + + Matcher mat = regexPat.matcher(inpString); + + if (cond.test(mat)) return IPair.pair(true, mat); + else return IPair.pair(false, null); + }; + return from( - (input) -> { - String inpString = input.toString(); - - Matcher mat = regexPat.matcher(inpString); - - if (cond.test(mat)) { - return IPair.pair(true, mat); - } else { - return IPair.pair(false, null); - } - }, + matcher, (input, res) -> action.apply(input, res) ); } @@ -162,6 +170,7 @@ public interface ComplexPattern { // @TODO Nov 21, 2020 Ben Culkin :MorePatterns // Try and write something to iterate over Iterator in a type-safe manner // Also, something for doing a sub-pattern match + /** * Create a pattern which will always execute. * @@ -174,10 +183,41 @@ public interface ComplexPattern { */ static ComplexPattern otherwise( Function action - ) { + ) + { return from( (input) -> IPair.pair(true, null), (input, ignored) -> action.apply(input) ); } + + /** + * Create a pattern which checks if the string form of a given object starts + * with a specific string. + * + * @param The type returned by the matcher. + * @param The type being matched against. + * + * @param pattern The string to check against. + * @param action The action to execute. + * + * @return A pattern which functions as described. + */ + static ComplexPattern startsWith( + String pattern, + Function action) + { + return from((input) -> { + String objString = input.toString(); + + if (objString.startsWith(pattern)) { + return IPair.pair( + true, + objString.substring( + pattern.length())); + } else { + return IPair.pair(false, null); + } + }, (ignored, input) -> action.apply(input)); + } } \ No newline at end of file diff --git a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java index 7900262..8e040fe 100644 --- a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java @@ -13,9 +13,10 @@ import bjc.data.*; * @author Ben Culkin * * @param The type returned by the pattern matcher. + * @param The type of the input to match against. */ public class MutablePatternMatcher - implements IPatternMatcher{ + implements IPatternMatcher { private final List> patterns; /** @@ -45,15 +46,15 @@ public class MutablePatternMatcher @Override public ReturnType matchFor(InputType input) throws NonExhaustiveMatch { Iterator> iterator; - for (iterator = new NonCMEIterator<>(patterns); - iterator.hasNext();) { + iterator = new NonCMEIterator<>(patterns); + while(iterator.hasNext()) { ComplexPattern pattern = iterator.next(); IPair matches = pattern.matches(input); - if (matches.getLeft()) { - pattern.apply(input, matches.getRight()); - } + matches.doWith((bool, obj) -> { + if (bool) pattern.apply(input, obj); + }); } throw new NonExhaustiveMatch("Non-exhaustive match against " + input); diff --git a/base/src/main/java/bjc/utils/patterns/Pattern.java b/base/src/main/java/bjc/utils/patterns/Pattern.java index e03623e..c9902e8 100644 --- a/base/src/main/java/bjc/utils/patterns/Pattern.java +++ b/base/src/main/java/bjc/utils/patterns/Pattern.java @@ -1,11 +1,5 @@ package bjc.utils.patterns; -import java.util.*; -import java.util.function.*; -import java.util.regex.*; - -import bjc.data.*; - /** * A simpler version of ComplexPattern, which always applies against Object * -- cgit v1.2.3 From a2c7425458f645802a352abc4783e0afc73dba13 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Thu, 3 Dec 2020 19:22:35 -0500 Subject: Adapt to esodata changes --- .../java/bjc/utils/cli/GenericCommandMode.java | 8 +-- .../bjc/utils/cli/objects/DelimSplitterCLI.java | 22 +++---- .../utils/components/FileComponentRepository.java | 14 ++-- .../bjc/utils/components/IComponentRepository.java | 8 +-- .../components/MemoryComponentRepository.java | 10 +-- .../java/bjc/utils/funcutils/CollectorUtils.java | 8 +-- .../bjc/utils/funcutils/CompoundCollector.java | 22 +++---- .../main/java/bjc/utils/funcutils/EnumUtils.java | 4 +- .../java/bjc/utils/funcutils/IteratorUtils.java | 4 +- .../main/java/bjc/utils/funcutils/ListUtils.java | 38 +++++------ .../main/java/bjc/utils/funcutils/TreeUtils.java | 22 +++---- .../src/main/java/bjc/utils/gen/RandomGrammar.java | 12 ++-- .../main/java/bjc/utils/gen/WeightedGrammar.java | 74 +++++++++++----------- .../main/java/bjc/utils/gen/WeightedRandom.java | 28 ++++---- .../main/java/bjc/utils/graph/AdjacencyMap.java | 20 +++--- base/src/main/java/bjc/utils/graph/Graph.java | 16 ++--- .../java/bjc/utils/gui/ExtensionFileFilter.java | 4 +- .../bjc/utils/gui/awt/ExtensionFileFilter.java | 4 +- .../bjc/utils/gui/panels/DropdownListPanel.java | 4 +- .../bjc/utils/gui/panels/HolderOutputPanel.java | 8 +-- .../bjc/utils/gui/panels/ListParameterPanel.java | 4 +- .../java/bjc/utils/ioutils/RegexStringEditor.java | 12 ++-- .../bjc/utils/ioutils/RuleBasedConfigReader.java | 20 +++--- base/src/main/java/bjc/utils/misc/Direction.java | 6 +- .../java/bjc/utils/misc/RelativeDirection.java | 4 +- .../java/bjc/utils/parserutils/ShuntingYard.java | 10 +-- .../java/bjc/utils/parserutils/TokenUtils.java | 4 +- .../bjc/utils/parserutils/TreeConstructor.java | 40 ++++++------ .../utils/parserutils/delims/DelimiterGroup.java | 42 ++++++------ .../bjc/utils/parserutils/delims/RegexOpener.java | 10 +-- .../parserutils/delims/SequenceDelimiter.java | 14 ++-- .../utils/parserutils/delims/StringDelimiter.java | 4 +- .../parserutils/splitter/ChainTokenSplitter.java | 8 +-- .../splitter/ConfigurableTokenSplitter.java | 4 +- .../splitter/ExcludingTokenSplitter.java | 6 +- .../splitter/FilteredTokenSplitter.java | 4 +- .../splitter/IdentityTokenSplitter.java | 2 +- .../parserutils/splitter/SimpleTokenSplitter.java | 4 +- .../utils/parserutils/splitter/TokenSplitter.java | 4 +- .../splitter/TransformTokenSplitter.java | 4 +- .../java/bjc/utils/patterns/ComplexPattern.java | 24 +++---- .../java/bjc/utils/patterns/FunctionalPattern.java | 6 +- .../bjc/utils/patterns/MutablePatternMatcher.java | 2 +- .../java/bjc/utils/patterns/PatternMatcher.java | 2 +- .../java/bjc/utils/patterns/SimplePatttern.java | 4 +- 45 files changed, 287 insertions(+), 287 deletions(-) (limited to 'base/src/main/java') diff --git a/base/src/main/java/bjc/utils/cli/GenericCommandMode.java b/base/src/main/java/bjc/utils/cli/GenericCommandMode.java index a96a8f0..60c51a8 100644 --- a/base/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/base/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -5,7 +5,7 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; import bjc.funcdata.FunctionalMap; -import bjc.funcdata.IMap; +import bjc.funcdata.MapEx; /** * A general command mode, with a customizable set of commands. @@ -18,12 +18,12 @@ import bjc.funcdata.IMap; */ public class GenericCommandMode implements CommandMode { /* Contains the commands this mode handles. */ - private final IMap commandHandlers; + private final MapEx commandHandlers; /* Commands to execute in every mode. */ - private final IMap defaultHandlers; + private final MapEx defaultHandlers; /* Contains help topics without an associated command */ - private final IMap helpTopics; + private final MapEx helpTopics; /* The action to execute upon encountering an unknown command */ private BiConsumer unknownCommandHandler; diff --git a/base/src/main/java/bjc/utils/cli/objects/DelimSplitterCLI.java b/base/src/main/java/bjc/utils/cli/objects/DelimSplitterCLI.java index a6820f2..53d6d1e 100644 --- a/base/src/main/java/bjc/utils/cli/objects/DelimSplitterCLI.java +++ b/base/src/main/java/bjc/utils/cli/objects/DelimSplitterCLI.java @@ -10,9 +10,9 @@ import java.util.List; import java.util.Map; import java.util.Scanner; -import bjc.data.ITree; +import bjc.data.Tree; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; import bjc.utils.funcutils.StringUtils; import bjc.utils.ioutils.MirrorDB; import bjc.utils.parserutils.delims.DelimiterException; @@ -359,7 +359,7 @@ public class DelimSplitterCLI { private void handleDelim(final String args) { try { - final ITree res = dlm.delimitSequence(args.split(" ")); + final Tree res = dlm.delimitSequence(args.split(" ")); printDelimSeq(res); } catch (final DelimiterException dex) { @@ -372,14 +372,14 @@ public class DelimSplitterCLI { for (int i = 0; i < argArray.length; i++) { final String arg = argArray[i]; - final IList strangs = split.split(arg); + final ListEx strangs = split.split(arg); System.out.printf("%d '%s' %s\n", i, arg, strangs); } } private void handleTest(final String inp, final boolean splitWS) { - IList strings; + ListEx strings; try { strings = split.split(inp); @@ -400,7 +400,7 @@ public class DelimSplitterCLI { strings = new FunctionalList<>(tks); } try { - final ITree delim + final Tree delim = dlm.delimitSequence(strings.toArray(new String[0])); printDelimSeq(delim); @@ -410,7 +410,7 @@ public class DelimSplitterCLI { } } - private void printDelimSeq(final ITree delim) { + private void printDelimSeq(final Tree delim) { System.out.println("Delimited tokens:\n" + delim.getChild(1).toString()); System.out.print("Delimited expr: "); printDelimTree(delim); @@ -420,7 +420,7 @@ public class DelimSplitterCLI { System.out.println(); } - private void printDelimTree(final ITree tree) { + private void printDelimTree(final Tree tree) { final StringBuilder sb = new StringBuilder(); intPrintDelimTree(tree.getChild(1), sb); @@ -428,13 +428,13 @@ public class DelimSplitterCLI { System.out.println(sb.toString().replaceAll("\\s+", " ")); } - private void intPrintDelimTree(final ITree tree, final StringBuilder sb) { + private void intPrintDelimTree(final Tree tree, final StringBuilder sb) { tree.doForChildren(child -> { intPrintDelimNode(child, sb); }); } - private void intPrintDelimNode(final ITree tree, final StringBuilder sb) { + private void intPrintDelimNode(final Tree tree, final StringBuilder sb) { if (tree.getHead().equals("contents")) { intPrintDelimTree(tree, sb); return; @@ -458,7 +458,7 @@ public class DelimSplitterCLI { case 3: intPrintDelimNode(tree.getChild(0), sb); - final ITree contents = tree.getChild(1); + final Tree contents = tree.getChild(1); intPrintDelimTree(contents.getChild(0), sb); intPrintDelimNode(tree.getChild(2), sb); diff --git a/base/src/main/java/bjc/utils/components/FileComponentRepository.java b/base/src/main/java/bjc/utils/components/FileComponentRepository.java index 6e6e604..fa98d03 100644 --- a/base/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/base/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -9,11 +9,11 @@ import java.util.function.Function; import java.util.logging.Level; import java.util.logging.Logger; -import bjc.data.IHolder; +import bjc.data.Holder; import bjc.data.Identity; import bjc.funcdata.FunctionalMap; -import bjc.funcdata.IList; -import bjc.funcdata.IMap; +import bjc.funcdata.ListEx; +import bjc.funcdata.MapEx; import bjc.utils.funcutils.FileUtils; /** @@ -31,7 +31,7 @@ public class FileComponentRepository = Logger.getLogger("FileComponentRepository"); /* The internal storage of components. */ - private IMap components; + private MapEx components; /* The path that all the components came from. */ private Path sourceDirectory; @@ -69,7 +69,7 @@ public class FileComponentRepository sourceDirectory = directory.toPath().toAbsolutePath(); /* Marker for making sure we don't skip the parent. */ - final IHolder isFirstDir = new Identity<>(true); + final Holder isFirstDir = new Identity<>(true); /* * Predicate to use to traverse all the files in a directory, but not recurse @@ -110,7 +110,7 @@ public class FileComponentRepository } @Override - public IMap getAll() { + public MapEx getAll() { return components; } @@ -120,7 +120,7 @@ public class FileComponentRepository } @Override - public IList getList() { + public ListEx getList() { return components.valueList(); } diff --git a/base/src/main/java/bjc/utils/components/IComponentRepository.java b/base/src/main/java/bjc/utils/components/IComponentRepository.java index 5ebb1de..7a40541 100644 --- a/base/src/main/java/bjc/utils/components/IComponentRepository.java +++ b/base/src/main/java/bjc/utils/components/IComponentRepository.java @@ -1,7 +1,7 @@ package bjc.utils.components; -import bjc.funcdata.IList; -import bjc.funcdata.IMap; +import bjc.funcdata.ListEx; +import bjc.funcdata.MapEx; /** * A collection of implementations of a particular type of @@ -19,7 +19,7 @@ public interface IComponentRepository * @return A map from component name to component, containing all of the * components in the repositories. */ - public IMap getAll(); + public MapEx getAll(); /** * Get a component with a specific name. @@ -36,7 +36,7 @@ public interface IComponentRepository * * @return A list of all the registered components. */ - public default IList getList() { + public default ListEx getList() { return getAll().valueList(); } diff --git a/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java b/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java index bba0867..2e11616 100644 --- a/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java +++ b/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java @@ -1,6 +1,6 @@ package bjc.utils.components; -import bjc.funcdata.IMap; +import bjc.funcdata.MapEx; /** * A repository of components stored in memory. @@ -12,7 +12,7 @@ import bjc.funcdata.IMap; */ public class MemoryComponentRepository implements IComponentRepository { - private final IMap repo; + private final MapEx repo; private final String source; @@ -22,7 +22,7 @@ public class MemoryComponentRepository repo) { + public MemoryComponentRepository(MapEx repo) { this(repo, "memory"); } @@ -34,14 +34,14 @@ public class MemoryComponentRepository repo, String source) { + public MemoryComponentRepository(MapEx repo, String source) { this.repo = repo; this.source = source; } @Override - public IMap getAll() { + public MapEx getAll() { return repo; } diff --git a/base/src/main/java/bjc/utils/funcutils/CollectorUtils.java b/base/src/main/java/bjc/utils/funcutils/CollectorUtils.java index 81313c8..a92c2d1 100644 --- a/base/src/main/java/bjc/utils/funcutils/CollectorUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/CollectorUtils.java @@ -2,8 +2,8 @@ package bjc.utils.funcutils; import java.util.stream.Collector; -import bjc.data.IHolder; -import bjc.data.IPair; +import bjc.data.Holder; +import bjc.data.Pair; /** * Utilities for producing implementations of {@link Collector} @@ -38,8 +38,8 @@ public class CollectorUtils { * @return A collector that functions as mentioned above. */ public static - Collector>, - IPair> + Collector>, + Pair> compoundCollect(final Collector first, final Collector second) { return new CompoundCollector<>(first, second); diff --git a/base/src/main/java/bjc/utils/funcutils/CompoundCollector.java b/base/src/main/java/bjc/utils/funcutils/CompoundCollector.java index 5e51c20..5aa266e 100644 --- a/base/src/main/java/bjc/utils/funcutils/CompoundCollector.java +++ b/base/src/main/java/bjc/utils/funcutils/CompoundCollector.java @@ -7,10 +7,10 @@ import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collector; -import bjc.data.IHolder; -import bjc.data.IPair; -import bjc.data.Identity; +import bjc.data.Holder; import bjc.data.Pair; +import bjc.data.Identity; +import bjc.data.SimplePair; /** * Implementation of a collecter that uses two collectors. @@ -18,8 +18,8 @@ import bjc.data.Pair; * @author Ben Culkin */ final class CompoundCollector - implements Collector>, - IPair> { + implements Collector>, + Pair> { /* Our characteristics. */ private final Set characteristicSet; @@ -48,7 +48,7 @@ final class CompoundCollector>, InitialType> accumulator() { + public BiConsumer>, InitialType> accumulator() { final BiConsumer firstAccumulator = first.accumulator(); final BiConsumer secondAccumulator = second.accumulator(); @@ -68,7 +68,7 @@ final class CompoundCollector>> combiner() { + public BinaryOperator>> combiner() { final BinaryOperator firstCombiner = first.combiner(); final BinaryOperator secondCombiner = second.combiner(); @@ -80,25 +80,25 @@ final class CompoundCollector>, IPair> + public Function>, Pair> finisher() { return state -> state.unwrap(pair -> { return pair.bind((left, right) -> { final FinalType1 finalLeft = first.finisher().apply(left); final FinalType2 finalRight = second.finisher().apply(right); - return new Pair<>(finalLeft, finalRight); + return new SimplePair<>(finalLeft, finalRight); }); }); } @Override - public Supplier>> supplier() { + public Supplier>> supplier() { return () -> { final AuxType1 initialLeft = first.supplier().get(); final AuxType2 initialRight = second.supplier().get(); - return new Identity<>(new Pair<>(initialLeft, initialRight)); + return new Identity<>(new SimplePair<>(initialLeft, initialRight)); }; } } diff --git a/base/src/main/java/bjc/utils/funcutils/EnumUtils.java b/base/src/main/java/bjc/utils/funcutils/EnumUtils.java index e8898ca..6d53952 100644 --- a/base/src/main/java/bjc/utils/funcutils/EnumUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/EnumUtils.java @@ -4,7 +4,7 @@ import java.util.Random; import java.util.function.Consumer; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * Utility methods on enums. @@ -34,7 +34,7 @@ public class EnumUtils { final int nValues, final Consumer action, final Random rnd) { final E[] enumValues = clasz.getEnumConstants(); - final IList valueList = new FunctionalList<>(enumValues); + final ListEx valueList = new FunctionalList<>(enumValues); final int randomValueCount = enumValues.length - nValues; diff --git a/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java b/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java index ea67295..662b1bf 100644 --- a/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java @@ -107,9 +107,9 @@ public class IteratorUtils { * yields the consumed values. */ public static - IPair, Iterator> entangle() + Pair, Iterator> entangle() { Queue backer = new ArrayDeque<>(); - return IPair.pair(backer::add, new QueueBackedIterator<>(backer)); + return Pair.pair(backer::add, new QueueBackedIterator<>(backer)); } } diff --git a/base/src/main/java/bjc/utils/funcutils/ListUtils.java b/base/src/main/java/bjc/utils/funcutils/ListUtils.java index e3662af..17642ce 100644 --- a/base/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -7,7 +7,7 @@ import java.util.List; import java.util.function.*; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * Utilities for manipulating FunctionalLists and regular Collections lists that @@ -27,7 +27,7 @@ public class ListUtils { * * @return The collapsed string of tokens. */ - public static String collapseTokens(final IList input) { + public static String collapseTokens(final ListEx input) { if (input == null) throw new NullPointerException("Input must not be null"); return collapseTokens(input, ""); @@ -45,7 +45,7 @@ public class ListUtils { * * @return The collapsed string of tokens. */ - public static String collapseTokens(final IList input, + public static String collapseTokens(final ListEx input, final String seperator) { if (input == null) throw new NullPointerException("Input must not be null"); else if (seperator == null) throw new NullPointerException("Seperator must not be null"); @@ -89,9 +89,9 @@ public class ListUtils { * @return A new list containing the desired number of items randomly selected * from the specified list without replacement. */ - public static IList drawWithoutReplacement(final IList list, + public static ListEx drawWithoutReplacement(final ListEx list, final int number, final Function rng) { - final IList selected = new FunctionalList<>(new ArrayList<>(number)); + final ListEx selected = new FunctionalList<>(new ArrayList<>(number)); final int total = list.getSize(); @@ -133,9 +133,9 @@ public class ListUtils { * @return A new list containing the desired number of items randomly selected * from the specified list. */ - public static IList drawWithReplacement(final IList list, final int number, + public static ListEx drawWithReplacement(final ListEx list, final int number, final Function rng) { - final IList selected = new FunctionalList<>(new ArrayList<>(number)); + final ListEx selected = new FunctionalList<>(new ArrayList<>(number)); for (int i = 0; i < number; i++) selected.add(list.randItem(rng)); @@ -161,7 +161,7 @@ public class ListUtils { * * @return A list partitioned according to the above rules. */ - public static IList> groupPartition(final IList input, + public static ListEx> groupPartition(final ListEx input, final Function counter, final int partitionSize) { if (input == null) { throw new NullPointerException("Input list must not be null"); @@ -176,10 +176,10 @@ public class ListUtils { } /* List that holds our results. */ - final IList> returned = new FunctionalList<>(); + final ListEx> returned = new FunctionalList<>(); /* List that holds elements rejected during current pass. */ - final IList rejected = new FunctionalList<>(); + final ListEx rejected = new FunctionalList<>(); final GroupPartIteration it = new GroupPartIteration<>(returned, rejected, partitionSize, counter); @@ -215,10 +215,10 @@ public class ListUtils { * @return A list containing all the elements of the lists. */ @SafeVarargs - public static IList mergeLists(final IList... lists) { - final IList returned = new FunctionalList<>(); + public static ListEx mergeLists(final ListEx... lists) { + final ListEx returned = new FunctionalList<>(); - for (final IList list : lists) { + for (final ListEx list : lists) { for (final E itm : list.toIterable()) returned.add(itm); } @@ -249,12 +249,12 @@ public class ListUtils { * If the list couldn't be padded to the * desired size. */ - public static IList padList(final IList list, + public static ListEx padList(final ListEx list, final Function counter, final int size, final Supplier padder) { int count = 0; - final IList returned = new FunctionalList<>(); + final ListEx returned = new FunctionalList<>(); for (final E itm : list.toIterable()) { count += counter.apply(itm); @@ -433,12 +433,12 @@ public class ListUtils { */ class GroupPartIteration implements Consumer { /* The list we're returning. */ - private final IList> returnedList; + private final ListEx> returnedList; /* The current partition of the list. */ - public IList currentPartition; + public ListEx currentPartition; /* The items rejected from the current partition. */ - private final IList rejectedItems; + private final ListEx rejectedItems; /* The number of items in the current partition. */ private int numberInCurrentPartition; @@ -465,7 +465,7 @@ class GroupPartIteration implements Consumer { * @param eleCount * The function to use to determine the value of an item. */ - public GroupPartIteration(final IList> returned, final IList rejects, + public GroupPartIteration(final ListEx> returned, final ListEx rejects, final int nPerPart, final Function eleCount) { this.returnedList = returned; this.rejectedItems = rejects; diff --git a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java index 59f60a2..41a01d8 100644 --- a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java @@ -21,8 +21,8 @@ public class TreeUtils { * The path to mark nodes with. * @return The list of marked paths. */ - public static IList> outlineTree(ITree tre, Predicate leafMarker) { - IList> paths = new FunctionalList<>(); + public static ListEx> outlineTree(Tree tre, Predicate leafMarker) { + ListEx> paths = new FunctionalList<>(); LinkedList path = new LinkedList<>(); path.add(tre.getHead()); @@ -33,11 +33,11 @@ public class TreeUtils { } /* Find a path in a tree. */ - private static void findPath(ITree subtree, LinkedList path, - Predicate leafMarker, IList> paths) { + private static void findPath(Tree subtree, LinkedList path, + Predicate leafMarker, ListEx> paths) { if (subtree.getChildrenCount() == 0 && leafMarker.test(subtree.getHead())) { /* We're at a matching leaf node. Add it. */ - IList finalPath = new FunctionalList<>(); + ListEx finalPath = new FunctionalList<>(); for (T ePath : path) finalPath.add(ePath); @@ -63,10 +63,10 @@ public class TreeUtils { * @param expander The function to expand nodes. * @return A transformed copy of the tree. */ - public static ITree substitute( - ITree tree, + public static Tree substitute( + Tree tree, Predicate marker, - Function> expander) { + Function> expander) { tree.topDownTransform((contents) -> { if (marker.test(contents)) return TopDownTransformResult.TRANSFORM; else return TopDownTransformResult.PASSTHROUGH; @@ -84,9 +84,9 @@ public class TreeUtils { * @param environment A map which contains the variables to substitute. * @return A transformed copy of the tree. */ - public static ITree substitute( - ITree tree, - IMap> environment) { + public static Tree substitute( + Tree tree, + MapEx> environment) { return substitute( tree, environment::containsKey, diff --git a/base/src/main/java/bjc/utils/gen/RandomGrammar.java b/base/src/main/java/bjc/utils/gen/RandomGrammar.java index 050165b..620cc5b 100644 --- a/base/src/main/java/bjc/utils/gen/RandomGrammar.java +++ b/base/src/main/java/bjc/utils/gen/RandomGrammar.java @@ -1,7 +1,7 @@ package bjc.utils.gen; import bjc.funcdata.FunctionalMap; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * A weighted grammar where all the rules have a equal chance of occuring. @@ -27,8 +27,8 @@ public class RandomGrammar extends WeightedGrammar { * The cases to add for this rule. */ @SafeVarargs - public final void addCases(final E rule, final IList... cases) { - for (final IList currentCase : cases) { + public final void addCases(final E rule, final ListEx... cases) { + for (final ListEx currentCase : cases) { super.addCase(rule, 1, currentCase); } } @@ -43,10 +43,10 @@ public class RandomGrammar extends WeightedGrammar { * The cases to add for this rule. */ @SafeVarargs - public final void makeRule(final E rule, final IList... cases) { + public final void makeRule(final E rule, final ListEx... cases) { super.addRule(rule); - for (final IList currentCase : cases) { + for (final ListEx currentCase : cases) { super.addCase(rule, 1, currentCase); } } @@ -60,7 +60,7 @@ public class RandomGrammar extends WeightedGrammar { * @param cases * The cases to add for this rule. */ - public void makeRule(final E rule, final IList> cases) { + public void makeRule(final E rule, final ListEx> cases) { if (cases == null) throw new NullPointerException("Cases must not be null"); diff --git a/base/src/main/java/bjc/utils/gen/WeightedGrammar.java b/base/src/main/java/bjc/utils/gen/WeightedGrammar.java index 540efbc..48cc658 100644 --- a/base/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/base/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -6,12 +6,12 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; -import bjc.data.IPair; import bjc.data.Pair; +import bjc.data.SimplePair; import bjc.funcdata.FunctionalList; import bjc.funcdata.FunctionalMap; -import bjc.funcdata.IList; -import bjc.funcdata.IMap; +import bjc.funcdata.ListEx; +import bjc.funcdata.MapEx; /** * A random grammar, where certain rules will come up more often than others. @@ -26,22 +26,22 @@ public class WeightedGrammar { protected String initialRule; /** The rules currently in this grammar */ - protected IMap>> rules; + protected MapEx>> rules; /** The random number generator used for random numbers */ private Random rng; /** All of the subgrammars of this grammar */ - protected IMap> subgrammars; + protected MapEx> subgrammars; /** Rules that require special handling */ - private IMap>> specialRules; + private MapEx>> specialRules; /** Predicate for marking special tokens */ private Predicate specialMarker; /** Action for special tokens */ - private BiFunction, IList> specialAction; + private BiFunction, ListEx> specialAction; /** Create a new weighted grammar. */ public WeightedGrammar() { @@ -75,7 +75,7 @@ public class WeightedGrammar { * The action to take on those tokens. */ public void configureSpecial(final Predicate marker, - final BiFunction, IList> action) { + final BiFunction, ListEx> action) { specialMarker = marker; specialAction = action; } @@ -89,7 +89,7 @@ public class WeightedGrammar { * @param cse * The case for the rule. */ - public void addSpecialRule(final E ruleName, final Supplier> cse) { + public void addSpecialRule(final E ruleName, final Supplier> cse) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } else if (cse == null) { @@ -111,7 +111,7 @@ public class WeightedGrammar { * @param cse * The case being added. */ - public void addCase(final E ruleName, final int probability, final IList cse) { + public void addCase(final E ruleName, final int probability, final ListEx cse) { if (ruleName == null) { throw new NullPointerException("Rule name must be not null"); } else if (cse == null) { @@ -180,7 +180,7 @@ public class WeightedGrammar { * * @return Whether or not the rule was succesfully added. */ - public boolean addRule(final E name, final WeightedRandom> cases) { + public boolean addRule(final E name, final WeightedRandom> cases) { if (name == null) { throw new NullPointerException("Name must not be null"); } else if (cases == null) { @@ -255,13 +255,13 @@ public class WeightedGrammar { * * @return A set of sentences generated by the specified rule. */ - public IList> generateDebugValues(final E ruleName) { + public ListEx> generateDebugValues(final E ruleName) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); - final IList> returnedList = new FunctionalList<>(); + final ListEx> returnedList = new FunctionalList<>(); - final WeightedRandom> ruleGenerator = rules.get(ruleName).get(); + final WeightedRandom> ruleGenerator = rules.get(ruleName).get(); for (int i = 0; i < 10; i++) { returnedList.add(ruleGenerator.generateValue()); @@ -288,7 +288,7 @@ public class WeightedGrammar { * * @return A randomly generated sentence from the specified initial rule. */ - public IList generateGenericValues(final E initRules, + public ListEx generateGenericValues(final E initRules, final Function tokenTransformer, final T spacer) { if (initRules == null) { throw new NullPointerException("Initial rule must not be null"); @@ -298,9 +298,9 @@ public class WeightedGrammar { throw new NullPointerException("Spacer must not be null"); } - final IList returnedList = new FunctionalList<>(); + final ListEx returnedList = new FunctionalList<>(); - IList genRules = new FunctionalList<>(initRules); + ListEx genRules = new FunctionalList<>(initRules); if (specialMarker != null) { if (specialMarker.test(initRules)) { @@ -371,8 +371,8 @@ public class WeightedGrammar { * * @return A list of random grammar elements generated by the specified rule. */ - public IList generateListValues(final E initRule, final E spacer) { - final IList retList + public ListEx generateListValues(final E initRule, final E spacer) { + final ListEx retList = generateGenericValues(initRule, strang -> strang, spacer); return retList; @@ -401,8 +401,8 @@ public class WeightedGrammar { * * @return The set of all rule names in this grammar. */ - public IList getRuleNames() { - final IList ruleNames = new FunctionalList<>(); + public ListEx getRuleNames() { + final ListEx ruleNames = new FunctionalList<>(); ruleNames.addAll(rules.keyList()); ruleNames.addAll(specialRules.keyList()); @@ -471,19 +471,19 @@ public class WeightedGrammar { throw new IllegalArgumentException( "Number of times to prefix must be positive."); - final WeightedRandom> rule = rules.get(ruleName).get(); + final WeightedRandom> rule = rules.get(ruleName).get(); - final IList>> newResults = new FunctionalList<>(); + final ListEx>> newResults = new FunctionalList<>(); /* * @NOTE Can this be simplified? */ rule.getValues().forEach(pair -> { - final IList> newRule = new FunctionalList<>(); + final ListEx> newRule = new FunctionalList<>(); for (int i = 1; i <= numberOfTimes; i++) { - final IList newCase = pair.merge((left, right) -> { - final IList returnVal = new FunctionalList<>(); + final ListEx newCase = pair.merge((left, right) -> { + final ListEx returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -502,7 +502,7 @@ public class WeightedGrammar { newRule.forEach((list) -> { final Integer currentProb = pair.merge((left, right) -> left); - newResults.add(new Pair<>(currentProb + additionalProbability, list)); + newResults.add(new SimplePair<>(currentProb + additionalProbability, list)); }); }); @@ -534,13 +534,13 @@ public class WeightedGrammar { throw new NullPointerException("Prefix token must not be null"); } - final WeightedRandom> rule = rules.get(ruleName).get(); + final WeightedRandom> rule = rules.get(ruleName).get(); - final IList>> newResults = new FunctionalList<>(); + final ListEx>> newResults = new FunctionalList<>(); rule.getValues().forEach(pair -> { - final IList newCase = pair.merge((left, right) -> { - final IList returnVal = new FunctionalList<>(); + final ListEx newCase = pair.merge((left, right) -> { + final ListEx returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -551,7 +551,7 @@ public class WeightedGrammar { newCase.prepend(prefixToken); - newResults.add(new Pair<>( + newResults.add(new SimplePair<>( pair.merge((left, right) -> left) + additionalProbability, newCase)); }); @@ -589,13 +589,13 @@ public class WeightedGrammar { throw new NullPointerException("Prefix token must not be null"); } - final WeightedRandom> rule = rules.get(ruleName).get(); + final WeightedRandom> rule = rules.get(ruleName).get(); - final IList>> newResults = new FunctionalList<>(); + final ListEx>> newResults = new FunctionalList<>(); rule.getValues().forEach(par -> { - final IList newCase = par.merge((left, right) -> { - final IList returnVal = new FunctionalList<>(); + final ListEx newCase = par.merge((left, right) -> { + final ListEx returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -606,7 +606,7 @@ public class WeightedGrammar { newCase.add(suffixToken); - newResults.add(new Pair<>( + newResults.add(new SimplePair<>( par.merge((left, right) -> left) + additionalProbability, newCase)); }); diff --git a/base/src/main/java/bjc/utils/gen/WeightedRandom.java b/base/src/main/java/bjc/utils/gen/WeightedRandom.java index 969020e..01a961f 100644 --- a/base/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/base/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -2,10 +2,10 @@ package bjc.utils.gen; import java.util.Random; -import bjc.data.IPair; import bjc.data.Pair; +import bjc.data.SimplePair; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * Represents a random number generator where certain results are weighted more @@ -17,7 +17,7 @@ import bjc.funcdata.IList; * The type of values that are randomly selected. */ public class WeightedRandom { - private final IList> values; + private final ListEx> values; /* The source for any needed random numbers */ private Random source; @@ -50,7 +50,7 @@ public class WeightedRandom { this(BASE); } - private WeightedRandom(Random src, IList> vals, int chance) { + private WeightedRandom(Random src, ListEx> vals, int chance) { source = src; values = vals; @@ -68,7 +68,7 @@ public class WeightedRandom { * The result to get when the chance comes up. */ public void addProbability(final int chance, final E result) { - values.add(new Pair<>(chance, result)); + values.add(new SimplePair<>(chance, result)); totalChance += chance; } @@ -91,7 +91,7 @@ public class WeightedRandom { */ public E generateValue(Random rn) { int target = rn.nextInt(totalChance); - for (IPair val : values) { + for (Pair val : values) { int prob = val.getLeft(); if (target < prob) { @@ -114,8 +114,8 @@ public class WeightedRandom { * * @return A list of all the values that can be generated */ - public IList getResults() { - return values.map(IPair::getRight); + public ListEx getResults() { + return values.map(Pair::getRight); } /** @@ -124,7 +124,7 @@ public class WeightedRandom { * * @return A list of pairs of values and value probabilities */ - public IList> getValues() { + public ListEx> getValues() { return values; } @@ -157,7 +157,7 @@ public class WeightedRandom { public E getDescent(int factor, Random rn) { if (values.getSize() == 0) return null; - for (IPair val : values) { + for (Pair val : values) { if (rn.nextInt(factor) == 0) continue; if (exhaust) { @@ -169,7 +169,7 @@ public class WeightedRandom { return val.getRight(); } - IPair val = values.getByIndex(values.getSize() - 1); + Pair val = values.getByIndex(values.getSize() - 1); if (exhaust) values.removeMatching(val); return val.getRight(); @@ -231,7 +231,7 @@ public class WeightedRandom { // System.err.printf("\tTRACE: got %d success for binomial trials (%d <= 1d%d, // %d times)\n", numSuc, target, bound, trials); - IPair val = values.getByIndex(Math.min(numSuc, values.getSize() - 1)); + Pair val = values.getByIndex(Math.min(numSuc, values.getSize() - 1)); if (exhaust) { totalChance -= val.getLeft(); @@ -248,8 +248,8 @@ public class WeightedRandom { * @return A new WeightedRandom that is exhaustible. */ public WeightedRandom exhaustible() { - IList> lst = new FunctionalList<>(); - for (IPair val : values) lst.add(val); + ListEx> lst = new FunctionalList<>(); + for (Pair val : values) lst.add(val); WeightedRandom res = new WeightedRandom<>(source, lst, totalChance); diff --git a/base/src/main/java/bjc/utils/graph/AdjacencyMap.java b/base/src/main/java/bjc/utils/graph/AdjacencyMap.java index 46d27f5..978b21d 100644 --- a/base/src/main/java/bjc/utils/graph/AdjacencyMap.java +++ b/base/src/main/java/bjc/utils/graph/AdjacencyMap.java @@ -6,12 +6,12 @@ import java.io.PrintStream; import java.util.InputMismatchException; import java.util.Scanner; -import bjc.data.IHolder; +import bjc.data.Holder; import bjc.data.Identity; import bjc.funcdata.FunctionalList; import bjc.funcdata.FunctionalMap; -import bjc.funcdata.IList; -import bjc.funcdata.IMap; +import bjc.funcdata.ListEx; +import bjc.funcdata.MapEx; import bjc.utils.funcutils.FuncUtils; /** @@ -62,13 +62,13 @@ public class AdjacencyMap { if (vertexCount <= 0) throw new InputMismatchException( "The number of vertices must be greater than 0"); - final IList vertices = new FunctionalList<>(); + final ListEx vertices = new FunctionalList<>(); FuncUtils.doTimes(vertexCount, vertexNo -> vertices.add(vertexNo)); adjacency = new AdjacencyMap<>(vertices); - final IHolder row = new Identity<>(0); + final Holder row = new Identity<>(0); input.forEachRemaining(strang -> { readRow(adjacency, vertexCount, row, strang); @@ -80,7 +80,7 @@ public class AdjacencyMap { /* Read a row of edges. */ private static void readRow(final AdjacencyMap adjacency, - final int vertexCount, final IHolder row, final String strang) { + final int vertexCount, final Holder row, final String strang) { final String[] parts = strang.split(" "); if (parts.length != vertexCount) { @@ -115,7 +115,7 @@ public class AdjacencyMap { } /** The backing storage of the map */ - private final IMap> adjacency = new FunctionalMap<>(); + private final MapEx> adjacency = new FunctionalMap<>(); /** * Create a new map from a set of vertices @@ -123,11 +123,11 @@ public class AdjacencyMap { * @param vertices * The set of vertices to create a map from */ - public AdjacencyMap(final IList vertices) { + public AdjacencyMap(final ListEx vertices) { if (vertices == null) throw new NullPointerException("Vertices must not be null"); vertices.forEach(vertex -> { - final IMap row = new FunctionalMap<>(); + final MapEx row = new FunctionalMap<>(); vertices.forEach(target -> { row.put(target, 0); @@ -143,7 +143,7 @@ public class AdjacencyMap { * @return Whether or not the graph is directed */ public boolean isDirected() { - final IHolder result = new Identity<>(true); + final Holder result = new Identity<>(true); adjacency.forEach((sourceKey, sourceValue) -> { sourceValue.forEach((targetKey, targetValue) -> { diff --git a/base/src/main/java/bjc/utils/graph/Graph.java b/base/src/main/java/bjc/utils/graph/Graph.java index 88b0d08..f32e07f 100644 --- a/base/src/main/java/bjc/utils/graph/Graph.java +++ b/base/src/main/java/bjc/utils/graph/Graph.java @@ -10,11 +10,11 @@ import java.util.Set; import java.util.function.BiConsumer; import java.util.function.BiPredicate; -import bjc.data.IHolder; +import bjc.data.Holder; import bjc.data.Identity; import bjc.funcdata.FunctionalMap; -import bjc.funcdata.IList; -import bjc.funcdata.IMap; +import bjc.funcdata.ListEx; +import bjc.funcdata.MapEx; /** * A directed weighted graph, where the vertices have some arbitrary label. @@ -47,7 +47,7 @@ public class Graph { } /** The backing representation of the graph. */ - private final IMap> backing; + private final MapEx> backing; /** Create a new empty graph. */ public Graph() { @@ -120,7 +120,7 @@ public class Graph { * The vertex to use as a source. * @return All of the edges with the specified vertex as a source. */ - public IMap getEdges(final T source) { + public MapEx getEdges(final T source) { /* Can't find edges for a null source. */ if (source == null) { throw new NullPointerException("The source cannot be null."); @@ -159,7 +159,7 @@ public class Graph { final Set visited = new HashSet<>(); /* Start at the initial vertex and visit it */ - final IHolder source = new Identity<>(getInitial()); + final Holder source = new Identity<>(getInitial()); visited.add(source.getValue()); @@ -177,7 +177,7 @@ public class Graph { ); /* Get the edge with the minimum distance. */ - final IHolder> minimum = new Identity<>(available.poll()); + final Holder> minimum = new Identity<>(available.poll()); /* * Only consider edges where we haven't visited the target of the edge. @@ -213,7 +213,7 @@ public class Graph { * * @return A unmodifiable set of all the vertices in the graph. */ - public IList getVertices() { + public ListEx getVertices() { return backing.keyList(); } diff --git a/base/src/main/java/bjc/utils/gui/ExtensionFileFilter.java b/base/src/main/java/bjc/utils/gui/ExtensionFileFilter.java index d9ebda5..c07543f 100644 --- a/base/src/main/java/bjc/utils/gui/ExtensionFileFilter.java +++ b/base/src/main/java/bjc/utils/gui/ExtensionFileFilter.java @@ -6,7 +6,7 @@ import java.util.List; import javax.swing.filechooser.FileFilter; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * A file filter based on extensions. @@ -20,7 +20,7 @@ public class ExtensionFileFilter extends FileFilter { /** * The list holding all filtered extensions */ - private final IList extensions; + private final ListEx extensions; /** * Create a new filter only showing files with the specified extensions. diff --git a/base/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java b/base/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java index 04b0b68..9b55306 100644 --- a/base/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java +++ b/base/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java @@ -5,7 +5,7 @@ import java.io.FilenameFilter; import java.util.List; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * Filter a set of filenames by extension. @@ -16,7 +16,7 @@ import bjc.funcdata.IList; */ public class ExtensionFileFilter implements FilenameFilter { /* The list of extensions to filter */ - private final IList extensions; + private final ListEx extensions; /** * Create a new filter only showing files with the specified extensions. diff --git a/base/src/main/java/bjc/utils/gui/panels/DropdownListPanel.java b/base/src/main/java/bjc/utils/gui/panels/DropdownListPanel.java index 8f65577..465be02 100644 --- a/base/src/main/java/bjc/utils/gui/panels/DropdownListPanel.java +++ b/base/src/main/java/bjc/utils/gui/panels/DropdownListPanel.java @@ -9,7 +9,7 @@ import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListSelectionModel; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; import bjc.utils.gui.layout.AutosizeLayout; import bjc.utils.gui.layout.HLayout; @@ -35,7 +35,7 @@ public class DropdownListPanel extends JPanel { * The items to choose from */ public DropdownListPanel(final String type, final DefaultListModel model, - final IList choices) { + final ListEx choices) { setLayout(new AutosizeLayout()); final JPanel itemInputPanel = new JPanel(); diff --git a/base/src/main/java/bjc/utils/gui/panels/HolderOutputPanel.java b/base/src/main/java/bjc/utils/gui/panels/HolderOutputPanel.java index d0b5383..dbf70f0 100644 --- a/base/src/main/java/bjc/utils/gui/panels/HolderOutputPanel.java +++ b/base/src/main/java/bjc/utils/gui/panels/HolderOutputPanel.java @@ -4,11 +4,11 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; -import bjc.data.IHolder; +import bjc.data.Holder; import bjc.utils.gui.layout.HLayout; /** - * A panel that outputs a value bound to a {@link IHolder} + * A panel that outputs a value bound to a {@link Holder} * * @author ben * @@ -19,7 +19,7 @@ public class HolderOutputPanel extends JPanel { private Timer updater; private final JLabel value; private final int nDelay; - private final IHolder val; + private final Holder val; /** * Create a new display panel, backed by a holder @@ -31,7 +31,7 @@ public class HolderOutputPanel extends JPanel { * @param nDelay * The delay in ms between value updates */ - public HolderOutputPanel(final String lab, final IHolder valueHolder, + public HolderOutputPanel(final String lab, final Holder valueHolder, final int nDelay) { this.val = valueHolder; this.nDelay = nDelay; diff --git a/base/src/main/java/bjc/utils/gui/panels/ListParameterPanel.java b/base/src/main/java/bjc/utils/gui/panels/ListParameterPanel.java index 3b48309..4ba5a01 100644 --- a/base/src/main/java/bjc/utils/gui/panels/ListParameterPanel.java +++ b/base/src/main/java/bjc/utils/gui/panels/ListParameterPanel.java @@ -9,7 +9,7 @@ import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListSelectionModel; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; import bjc.utils.gui.SimpleJList; import bjc.utils.gui.layout.HLayout; import bjc.utils.gui.layout.VLayout; @@ -54,7 +54,7 @@ public class ListParameterPanel extends JPanel { * The default values to put in the list */ public ListParameterPanel(final Supplier add, final Consumer edit, - final Consumer remove, final IList defaults) { + final Consumer remove, final ListEx defaults) { setLayout(new VLayout(2)); JList list; diff --git a/base/src/main/java/bjc/utils/ioutils/RegexStringEditor.java b/base/src/main/java/bjc/utils/ioutils/RegexStringEditor.java index 3dad724..e7d8c54 100644 --- a/base/src/main/java/bjc/utils/ioutils/RegexStringEditor.java +++ b/base/src/main/java/bjc/utils/ioutils/RegexStringEditor.java @@ -8,7 +8,7 @@ import java.util.regex.Pattern; import bjc.data.Toggle; import bjc.data.ValueToggle; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; import bjc.functypes.ID; /** @@ -84,7 +84,7 @@ public class RegexStringEditor { /* * Get all of the occurances. */ - final IList occurances = listOccurances(input, rPatt); + final ListEx occurances = listOccurances(input, rPatt); /* * Execute the correct action on every occurance. @@ -118,13 +118,13 @@ public class RegexStringEditor { * * @return The string, with both actions applied. */ - public static IList mapOccurances(final String input, final Pattern rPatt, + public static ListEx mapOccurances(final String input, final Pattern rPatt, final UnaryOperator betweenAction, final UnaryOperator onAction) { /* * Get all of the occurances. */ - final IList occurances = listOccurances(input, rPatt); + final ListEx occurances = listOccurances(input, rPatt); /* * Execute the correct action on every occurance. @@ -146,8 +146,8 @@ public class RegexStringEditor { * @return The string, as a list of match/non-match segments, starting/ending * with a non-match segment. */ - public static IList listOccurances(final String input, final Pattern rPatt) { - final IList res = new FunctionalList<>(); + public static ListEx listOccurances(final String input, final Pattern rPatt) { + final ListEx res = new FunctionalList<>(); /* * Create the matcher and work buffer. diff --git a/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java b/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java index bc19dd9..12534bd 100644 --- a/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java +++ b/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java @@ -6,14 +6,14 @@ import java.util.Scanner; import java.util.function.BiConsumer; import java.util.function.Consumer; -import bjc.data.IHolder; -import bjc.data.IPair; -import bjc.data.Identity; +import bjc.data.Holder; import bjc.data.Pair; +import bjc.data.Identity; +import bjc.data.SimplePair; import bjc.utils.exceptions.UnknownPragma; import bjc.funcdata.FunctionalMap; import bjc.funcdata.FunctionalStringTokenizer; -import bjc.funcdata.IMap; +import bjc.funcdata.MapEx; /** * This class parses a rules based config file, and uses it to drive a provided @@ -31,7 +31,7 @@ public class RuleBasedConfigReader { * * Takes the tokenizer, and a pair of the read token and application state */ - private BiConsumer> start; + private BiConsumer> start; /* * Function to use when continuing a rule. @@ -52,7 +52,7 @@ public class RuleBasedConfigReader { * * Pragma actions are functions taking a tokenizer and application state */ - private final IMap> pragmas; + private final MapEx> pragmas; /** * Create a new rule-based config reader @@ -65,7 +65,7 @@ public class RuleBasedConfigReader { * The action to fire when ending a rule */ public RuleBasedConfigReader( - final BiConsumer> start, + final BiConsumer> start, final BiConsumer continueRule, final Consumer end) { this.start = start; @@ -161,7 +161,7 @@ public class RuleBasedConfigReader { /* * This is true when a rule's open */ - final IHolder isRuleOpen = new Identity<>(false); + final Holder isRuleOpen = new Identity<>(false); /* * Do something for every line of the file @@ -237,7 +237,7 @@ public class RuleBasedConfigReader { * The action to execute on starting of a rule */ public void setStartRule( - final BiConsumer> start) { + final BiConsumer> start) { if (start == null) throw new NullPointerException("Action on rule start must be non-null"); @@ -284,7 +284,7 @@ public class RuleBasedConfigReader { /* * Start a rule */ - start.accept(tokenizer, new Pair<>(nextToken, state)); + start.accept(tokenizer, new SimplePair<>(nextToken, state)); isRuleOpen = true; } diff --git a/base/src/main/java/bjc/utils/misc/Direction.java b/base/src/main/java/bjc/utils/misc/Direction.java index cc24b49..2026349 100644 --- a/base/src/main/java/bjc/utils/misc/Direction.java +++ b/base/src/main/java/bjc/utils/misc/Direction.java @@ -7,7 +7,7 @@ import org.apache.commons.lang3.text.WordUtils; import bjc.utils.exceptions.DirectionInvalid; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * A set of cardinal directions @@ -54,7 +54,7 @@ public enum Direction { * * @return A list of all the cardinal directions */ - public static IList cardinals() { + public static ListEx cardinals() { return new FunctionalList<>(NORTH, SOUTH, EAST, WEST); } @@ -84,7 +84,7 @@ public enum Direction { + nCardinals); } - IList cards = cardinals(); + ListEx cards = cardinals(); for (int i = 0; i <= 4 - nCardinals; i++) { Direction rDir = cards.randItem(RNG::nextInt); diff --git a/base/src/main/java/bjc/utils/misc/RelativeDirection.java b/base/src/main/java/bjc/utils/misc/RelativeDirection.java index 4239b0d..87d9e10 100644 --- a/base/src/main/java/bjc/utils/misc/RelativeDirection.java +++ b/base/src/main/java/bjc/utils/misc/RelativeDirection.java @@ -5,7 +5,7 @@ import java.util.function.Consumer; import bjc.utils.exceptions.DirectionInvalid; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * Represents a direction that is relative to another direction @@ -62,7 +62,7 @@ public enum RelativeDirection { + numDirections); } - IList relativeDirs = new FunctionalList<>(values()); + ListEx relativeDirs = new FunctionalList<>(values()); if (ignoreBackwards) { relativeDirs.removeMatching(BACKWARD); diff --git a/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java b/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java index ac82388..f73c315 100644 --- a/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -6,8 +6,8 @@ import java.util.function.Function; import bjc.funcdata.FunctionalList; import bjc.funcdata.FunctionalMap; -import bjc.funcdata.IList; -import bjc.funcdata.IMap; +import bjc.funcdata.ListEx; +import bjc.funcdata.MapEx; import bjc.utils.funcutils.StringUtils; /** @@ -59,7 +59,7 @@ public class ShuntingYard { /* * Holds all the shuntable operations. */ - private IMap operators; + private MapEx operators; /** * Create a new shunting yard with a default set of operators. @@ -158,7 +158,7 @@ public class ShuntingYard { * * @return A list of tokens in postfix notation. */ - public IList postfix(final IList input, + public ListEx postfix(final ListEx input, final Function transformer) { /* * Check our input @@ -169,7 +169,7 @@ public class ShuntingYard { /* * Here's what we're handing back */ - final IList output = new FunctionalList<>(); + final ListEx output = new FunctionalList<>(); /* * The stack to put operators on diff --git a/base/src/main/java/bjc/utils/parserutils/TokenUtils.java b/base/src/main/java/bjc/utils/parserutils/TokenUtils.java index c6fdf5e..860bbdf 100644 --- a/base/src/main/java/bjc/utils/parserutils/TokenUtils.java +++ b/base/src/main/java/bjc/utils/parserutils/TokenUtils.java @@ -10,7 +10,7 @@ import java.util.regex.Pattern; import bjc.data.*; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; import bjc.utils.parserutils.splitter.TokenSplitter; /** @@ -28,7 +28,7 @@ public class TokenUtils { */ public static class StringTokenSplitter implements TokenSplitter { @Override - public IList split(final String input) { + public ListEx split(final String input) { return new FunctionalList<>(TokenUtils.removeDQuotedStrings(input)); } } diff --git a/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java b/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java index 6780768..bd907b5 100644 --- a/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -5,7 +5,7 @@ import java.util.LinkedList; import java.util.function.*; import bjc.data.*; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; import bjc.utils.parserutils.TreeConstructor.*; /** @@ -22,7 +22,7 @@ public class TreeConstructor { * The token type of the tree. */ public interface QueueFlattener - extends Function>, ITree> { + extends Function>, Tree> { /* * Alias */ @@ -30,14 +30,14 @@ public class TreeConstructor { /* Alias for constructor state. */ static final class ConstructorState - extends Pair>, ITree> { - public ConstructorState(final Deque> left, - final ITree right) { + extends SimplePair>, Tree> { + public ConstructorState(final Deque> left, + final Tree right) { super(left, right); } public ConstructorState( - final IPair>, ITree> par) { + final Pair>, Tree> par) { super(par.getLeft(), par.getRight()); } } @@ -59,8 +59,8 @@ public class TreeConstructor { * * @return A AST from the expression. */ - public static ITree constructTree( - final IList tokens, final Predicate isOperator) { + public static Tree constructTree( + final ListEx tokens, final Predicate isOperator) { /* Construct a tree with no special operators */ return constructTree(tokens, isOperator, op -> false, null); } @@ -92,8 +92,8 @@ public class TreeConstructor { * @return A AST from the expression. * */ - public static ITree constructTree( - final IList tokens, final Predicate isOperator, + public static Tree constructTree( + final ListEx tokens, final Predicate isOperator, final Predicate isSpecialOperator, final Function> handleSpecialOperator) { /* @@ -112,7 +112,7 @@ public class TreeConstructor { = new ConstructorState<>(new LinkedList<>(), null); /* Here is the state for the tree construction */ - final IHolder> initialState = new Identity<>(cstate); + final Holder> initialState = new Identity<>(cstate); /* Transform each of the tokens */ final TokenTransformer trans = new TokenTransformer<>(initialState, @@ -152,11 +152,11 @@ class TokenTransformer implements Consumer { } private ConstructorState - handleOperator(final Deque> queuedASTs) { + handleOperator(final Deque> queuedASTs) { /* * The AST we're going to hand back */ - ITree newAST; + Tree newAST; /* * Handle special operators @@ -178,13 +178,13 @@ class TokenTransformer implements Consumer { /* * Grab the two operands */ - final ITree right = queuedASTs.pop(); - final ITree left = queuedASTs.pop(); + final Tree right = queuedASTs.pop(); + final Tree left = queuedASTs.pop(); /* * Create a new AST */ - newAST = new Tree<>(element, left, right); + newAST = new SimpleTree<>(element, left, right); } /* @@ -200,7 +200,7 @@ class TokenTransformer implements Consumer { } /* The initial state of the transformer. */ - private final IHolder> initialState; + private final Holder> initialState; /* The predicate tot use to detect operators. */ private final Predicate operatorPredicate; @@ -227,7 +227,7 @@ class TokenTransformer implements Consumer { * The function used for handling special * operators. */ - public TokenTransformer(final IHolder> initialState, + public TokenTransformer(final Holder> initialState, final Predicate operatorPredicate, final Predicate isSpecialOperator, final Function> handleSpecialOperator) { @@ -245,7 +245,7 @@ class TokenTransformer implements Consumer { if (operatorPredicate.test(element)) { initialState.transform(new OperatorHandler(element)); } else { - final ITree newAST = new Tree<>(element); + final Tree newAST = new SimpleTree<>(element); /* * Insert the new tree into the AST @@ -254,7 +254,7 @@ class TokenTransformer implements Consumer { pair.bindLeft(queue -> { queue.push(newAST); - return new Pair<>(queue, newAST); + return new SimplePair<>(queue, newAST); }) ) ); diff --git a/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java b/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java index ba61531..75f777c 100644 --- a/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java +++ b/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java @@ -11,12 +11,12 @@ import java.util.Set; import java.util.function.BiPredicate; import java.util.function.Function; -import bjc.data.IPair; -import bjc.data.ITree; import bjc.data.Pair; import bjc.data.Tree; +import bjc.data.SimplePair; +import bjc.data.SimpleTree; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * Represents a possible delimiter group to match. @@ -37,12 +37,12 @@ public class DelimiterGroup { /* * The contents of this group. */ - private final Deque> contents; + private final Deque> contents; /* * The contents of the current subgroup. */ - private IList> currentGroup; + private ListEx> currentGroup; /* * The token that opened the group, and any opening parameters. @@ -74,7 +74,7 @@ public class DelimiterGroup { * @param itm * The item to add to this group instance. */ - public void addItem(final ITree itm) { + public void addItem(final Tree itm) { currentGroup.add(itm); } @@ -91,8 +91,8 @@ public class DelimiterGroup { /* * Add all of the contents to the subgroup. */ - final ITree subgroupContents = new Tree<>(chars.contents); - for (final ITree itm : currentGroup) { + final Tree subgroupContents = new SimpleTree<>(chars.contents); + for (final Tree itm : currentGroup) { subgroupContents.addChild(itm); } @@ -100,7 +100,7 @@ public class DelimiterGroup { * Handle subordinate sub-groups. */ while (!contents.isEmpty()) { - final ITree possibleSubordinate = contents.peek(); + final Tree possibleSubordinate = contents.peek(); /* * Subordinate lower priority subgroups. @@ -118,8 +118,8 @@ public class DelimiterGroup { } } - final Tree subgroup - = new Tree<>(chars.subgroup, subgroupContents, new Tree<>(marker)); + final SimpleTree subgroup + = new SimpleTree<>(chars.subgroup, subgroupContents, new SimpleTree<>(marker)); contents.push(subgroup); @@ -137,7 +137,7 @@ public class DelimiterGroup { * * @return This group as a tree. */ - public ITree toTree(final T closer, final SequenceCharacteristics chars) { + public Tree toTree(final T closer, final SequenceCharacteristics chars) { /* * Mark any implied subgroups. */ @@ -146,7 +146,7 @@ public class DelimiterGroup { } /* The resulting tree. */ - final ITree res = new Tree<>(chars.contents); + final Tree res = new SimpleTree<>(chars.contents); /* * Add either the contents of the current group, or subgroups if they're @@ -162,7 +162,7 @@ public class DelimiterGroup { currentGroup.forEach(res::addChild); } - return new Tree<>(groupName, new Tree<>(opener), res, new Tree<>(closer)); + return new SimpleTree<>(groupName, new SimpleTree<>(opener), res, new SimpleTree<>(closer)); } @Override @@ -259,18 +259,18 @@ public class DelimiterGroup { * * @return The name of the group T opens, or null if it doesn't open one. */ - public IPair doesOpen(final T marker) { + public Pair doesOpen(final T marker) { if (openDelimiters.containsKey(marker)) - return new Pair<>(openDelimiters.get(marker), null); + return new SimplePair<>(openDelimiters.get(marker), null); - for (final Function> pred : predOpeners) { - final IPair par = pred.apply(marker); + for (final Function> pred : predOpeners) { + final Pair par = pred.apply(marker); if (par.getLeft() != null) return par; } - return new Pair<>(null, null); + return new SimplePair<>(null, null); } /** @@ -312,7 +312,7 @@ public class DelimiterGroup { private final Map impliedSubgroups; /* Allows more complex openings */ - private final List>> predOpeners; + private final List>> predOpeners; /* Allow more complex closings */ private final List> predClosers; @@ -562,7 +562,7 @@ public class DelimiterGroup { * @param pred * The predicate that defines the opener and its parameters. */ - public void addPredOpener(final Function> pred) { + public void addPredOpener(final Function> pred) { predOpeners.add(pred); } diff --git a/base/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java b/base/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java index f08201c..db5c3ca 100644 --- a/base/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java +++ b/base/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java @@ -4,8 +4,8 @@ import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; -import bjc.data.IPair; import bjc.data.Pair; +import bjc.data.SimplePair; /** * A predicated opener for use with {@link RegexCloser} @@ -13,7 +13,7 @@ import bjc.data.Pair; * @author bjculkin * */ -public class RegexOpener implements Function> { +public class RegexOpener implements Function> { /* The name of the group. */ private final String name; /* The pattern that marks an opening group. */ @@ -35,7 +35,7 @@ public class RegexOpener implements Function> { } @Override - public IPair apply(final String str) { + public Pair apply(final String str) { final Matcher m = patt.matcher(str); if (m.matches()) { @@ -47,9 +47,9 @@ public class RegexOpener implements Function> { parms[i] = m.group(i); } - return new Pair<>(name, parms); + return new SimplePair<>(name, parms); } - return new Pair<>(null, null); + return new SimplePair<>(null, null); } } diff --git a/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java b/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java index a73ac88..b3f6dc4 100644 --- a/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java +++ b/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java @@ -9,13 +9,13 @@ import com.google.common.collect.HashMultiset; import com.google.common.collect.Multimap; import com.google.common.collect.Multiset; -import bjc.data.IPair; -import bjc.data.ITree; +import bjc.data.Pair; import bjc.data.Tree; +import bjc.data.SimpleTree; import bjc.esodata.PushdownMap; import bjc.esodata.SimpleStack; import bjc.esodata.Stack; -import bjc.funcdata.IMap; +import bjc.funcdata.MapEx; import bjc.utils.funcutils.StringUtils; /** @@ -82,7 +82,7 @@ public class SequenceDelimiter { * delimitation. * */ - public ITree delimitSequence(final SequenceCharacteristics chars, + public Tree delimitSequence(final SequenceCharacteristics chars, @SuppressWarnings("unchecked") final T... seq) throws DelimiterException { if (initialGroup == null) { throw new NullPointerException("Initial group must be specified."); @@ -105,7 +105,7 @@ public class SequenceDelimiter { allowedDelimiters.push(HashMultimap.create()); /* Map of who forbid what for debugging purposes. */ - final IMap whoForbid = new PushdownMap<>(); + final MapEx whoForbid = new PushdownMap<>(); /* * Process each member of the sequence. @@ -114,7 +114,7 @@ public class SequenceDelimiter { final T tok = seq[i]; /* Check if this token could open a group. */ - final IPair possibleOpenPar = groupStack.top().doesOpen(tok); + final Pair possibleOpenPar = groupStack.top().doesOpen(tok); T possibleOpen = possibleOpenPar.getLeft(); if (possibleOpen == null) { @@ -217,7 +217,7 @@ public class SequenceDelimiter { groupStack.top().markSubgroup(tok, chars); } else { /* Add an item to the group. */ - groupStack.top().addItem(new Tree<>(tok)); + groupStack.top().addItem(new SimpleTree<>(tok)); } } diff --git a/base/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java b/base/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java index 6035ede..575066d 100644 --- a/base/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java +++ b/base/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java @@ -1,6 +1,6 @@ package bjc.utils.parserutils.delims; -import bjc.data.ITree; +import bjc.data.Tree; /** * A sequence delimiter specialized for strings. @@ -26,7 +26,7 @@ public class StringDelimiter extends SequenceDelimiter { * * @see SequenceDelimiter */ - public ITree delimitSequence(final String... seq) throws DelimiterException { + public Tree delimitSequence(final String... seq) throws DelimiterException { return super.delimitSequence( new SequenceCharacteristics<>("root", "contents", "subgroup"), seq); } diff --git a/base/src/main/java/bjc/utils/parserutils/splitter/ChainTokenSplitter.java b/base/src/main/java/bjc/utils/parserutils/splitter/ChainTokenSplitter.java index 0844b5b..c60b6f2 100644 --- a/base/src/main/java/bjc/utils/parserutils/splitter/ChainTokenSplitter.java +++ b/base/src/main/java/bjc/utils/parserutils/splitter/ChainTokenSplitter.java @@ -1,7 +1,7 @@ package bjc.utils.parserutils.splitter; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * A token splitter that chains several other splitters together. @@ -10,7 +10,7 @@ import bjc.funcdata.IList; * */ public class ChainTokenSplitter implements TokenSplitter { - private final IList spliters; + private final ListEx spliters; /** * Create a new chain token splitter. @@ -40,8 +40,8 @@ public class ChainTokenSplitter implements TokenSplitter { } @Override - public IList split(final String input) { - final IList initList = new FunctionalList<>(input); + public ListEx split(final String input) { + final ListEx initList = new FunctionalList<>(input); return spliters.reduceAux(initList, (splitter, strangs) -> strangs.flatMap(splitter::split)); } diff --git a/base/src/main/java/bjc/utils/parserutils/splitter/ConfigurableTokenSplitter.java b/base/src/main/java/bjc/utils/parserutils/splitter/ConfigurableTokenSplitter.java index 16c1dc3..26d9dbe 100644 --- a/base/src/main/java/bjc/utils/parserutils/splitter/ConfigurableTokenSplitter.java +++ b/base/src/main/java/bjc/utils/parserutils/splitter/ConfigurableTokenSplitter.java @@ -6,7 +6,7 @@ import java.util.LinkedHashSet; import java.util.Set; import java.util.regex.Pattern; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * Split a string into pieces around a regular expression, and offer an easy way @@ -111,7 +111,7 @@ public class ConfigurableTokenSplitter extends SimpleTokenSplitter { } @Override - public IList split(final String input) { + public ListEx split(final String input) { if (spliter == null) throw new IllegalStateException("Must compile splitter before use"); diff --git a/base/src/main/java/bjc/utils/parserutils/splitter/ExcludingTokenSplitter.java b/base/src/main/java/bjc/utils/parserutils/splitter/ExcludingTokenSplitter.java index 9a0cd65..52ce4bf 100644 --- a/base/src/main/java/bjc/utils/parserutils/splitter/ExcludingTokenSplitter.java +++ b/base/src/main/java/bjc/utils/parserutils/splitter/ExcludingTokenSplitter.java @@ -5,7 +5,7 @@ import java.util.Set; import java.util.function.Predicate; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * A token splitter that will not split certain tokens. @@ -15,7 +15,7 @@ import bjc.funcdata.IList; */ public class ExcludingTokenSplitter implements TokenSplitter { private final Set literalExclusions; - private final IList> predExclusions; + private final ListEx> predExclusions; private final TokenSplitter spliter; @@ -59,7 +59,7 @@ public class ExcludingTokenSplitter implements TokenSplitter { } @Override - public IList split(final String input) { + public ListEx split(final String input) { if (literalExclusions.contains(input)) return new FunctionalList<>(input); else if (predExclusions.anyMatch(pred -> pred.test(input))) diff --git a/base/src/main/java/bjc/utils/parserutils/splitter/FilteredTokenSplitter.java b/base/src/main/java/bjc/utils/parserutils/splitter/FilteredTokenSplitter.java index 85d72e2..70abbbc 100644 --- a/base/src/main/java/bjc/utils/parserutils/splitter/FilteredTokenSplitter.java +++ b/base/src/main/java/bjc/utils/parserutils/splitter/FilteredTokenSplitter.java @@ -2,7 +2,7 @@ package bjc.utils.parserutils.splitter; import java.util.function.Predicate; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * A token splitter that removes tokens that match a predicate from the stream @@ -31,7 +31,7 @@ public class FilteredTokenSplitter implements TokenSplitter { } @Override - public IList split(String input) { + public ListEx split(String input) { return source.split(input).getMatching(filter); } } diff --git a/base/src/main/java/bjc/utils/parserutils/splitter/IdentityTokenSplitter.java b/base/src/main/java/bjc/utils/parserutils/splitter/IdentityTokenSplitter.java index 6be0e7f..b3b1b29 100644 --- a/base/src/main/java/bjc/utils/parserutils/splitter/IdentityTokenSplitter.java +++ b/base/src/main/java/bjc/utils/parserutils/splitter/IdentityTokenSplitter.java @@ -10,7 +10,7 @@ import bjc.funcdata.*; */ public class IdentityTokenSplitter implements TokenSplitter { @Override - public IList split(String input) { + public ListEx split(String input) { return new FunctionalList<>(input); } } diff --git a/base/src/main/java/bjc/utils/parserutils/splitter/SimpleTokenSplitter.java b/base/src/main/java/bjc/utils/parserutils/splitter/SimpleTokenSplitter.java index 6d88b20..df9ce70 100644 --- a/base/src/main/java/bjc/utils/parserutils/splitter/SimpleTokenSplitter.java +++ b/base/src/main/java/bjc/utils/parserutils/splitter/SimpleTokenSplitter.java @@ -2,7 +2,7 @@ package bjc.utils.parserutils.splitter; import java.util.regex.Pattern; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; import bjc.functypes.ID; import bjc.utils.ioutils.RegexStringEditor; @@ -39,7 +39,7 @@ public class SimpleTokenSplitter implements TokenSplitter { } @Override - public IList split(final String input) { + public ListEx split(final String input) { if (keepDelim) { return RegexStringEditor.mapOccurances(input, spliter, ID.id(), ID.id()); } diff --git a/base/src/main/java/bjc/utils/parserutils/splitter/TokenSplitter.java b/base/src/main/java/bjc/utils/parserutils/splitter/TokenSplitter.java index 59e73e8..e833c21 100644 --- a/base/src/main/java/bjc/utils/parserutils/splitter/TokenSplitter.java +++ b/base/src/main/java/bjc/utils/parserutils/splitter/TokenSplitter.java @@ -1,6 +1,6 @@ package bjc.utils.parserutils.splitter; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * Split a string into a list of pieces. @@ -17,5 +17,5 @@ public interface TokenSplitter { * * @return The pieces of the string. */ - public IList split(String input); + public ListEx split(String input); } diff --git a/base/src/main/java/bjc/utils/parserutils/splitter/TransformTokenSplitter.java b/base/src/main/java/bjc/utils/parserutils/splitter/TransformTokenSplitter.java index b9fbedc..63c3206 100644 --- a/base/src/main/java/bjc/utils/parserutils/splitter/TransformTokenSplitter.java +++ b/base/src/main/java/bjc/utils/parserutils/splitter/TransformTokenSplitter.java @@ -2,7 +2,7 @@ package bjc.utils.parserutils.splitter; import java.util.function.UnaryOperator; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * A token splitter that performs a transform on the tokens from another @@ -31,7 +31,7 @@ public class TransformTokenSplitter implements TokenSplitter { } @Override - public IList split(String input) { + public ListEx split(String input) { return source.split(input).map(transform); } diff --git a/base/src/main/java/bjc/utils/patterns/ComplexPattern.java b/base/src/main/java/bjc/utils/patterns/ComplexPattern.java index e9035df..c6d72ec 100644 --- a/base/src/main/java/bjc/utils/patterns/ComplexPattern.java +++ b/base/src/main/java/bjc/utils/patterns/ComplexPattern.java @@ -23,7 +23,7 @@ public interface ComplexPattern { * @return Whether or not this pattern is matched, as well as a state value * that will get passed to the pattern if it did match. */ - IPair matches(InputType input); + Pair matches(InputType input); /** * Apply this pattern, once it has matched. @@ -50,7 +50,7 @@ public interface ComplexPattern { * @return A pattern composed from the passed in functions. */ static ComplexPattern from( - Function> matcher, + Function> matcher, BiFunction accepter) { return new FunctionalPattern<>(matcher, accepter); @@ -74,7 +74,7 @@ public interface ComplexPattern { Function action) { return from( - (input) -> IPair.pair(clasz.isInstance(input), null), + (input) -> Pair.pair(clasz.isInstance(input), null), (input, ignored) -> action.apply((ClassType)input) ); } @@ -96,7 +96,7 @@ public interface ComplexPattern { ) { return from( - (input) -> IPair.pair(obj.equals(input), null), + (input) -> Pair.pair(obj.equals(input), null), (input, ignored) -> action.apply(input) ); } @@ -120,10 +120,10 @@ public interface ComplexPattern { BiFunction action ) { - Function> matcher = (input) -> { + Function> matcher = (input) -> { String objString = input.toString(); - return IPair.pair(pattern.equals(objString), objString); + return Pair.pair(pattern.equals(objString), objString); }; return from( @@ -152,13 +152,13 @@ public interface ComplexPattern { { java.util.regex.Pattern regexPat = java.util.regex.Pattern.compile(regex); - Function> matcher = (input) -> { + Function> matcher = (input) -> { String inpString = input.toString(); Matcher mat = regexPat.matcher(inpString); - if (cond.test(mat)) return IPair.pair(true, mat); - else return IPair.pair(false, null); + if (cond.test(mat)) return Pair.pair(true, mat); + else return Pair.pair(false, null); }; return from( @@ -186,7 +186,7 @@ public interface ComplexPattern { ) { return from( - (input) -> IPair.pair(true, null), + (input) -> Pair.pair(true, null), (input, ignored) -> action.apply(input) ); } @@ -211,12 +211,12 @@ public interface ComplexPattern { String objString = input.toString(); if (objString.startsWith(pattern)) { - return IPair.pair( + return Pair.pair( true, objString.substring( pattern.length())); } else { - return IPair.pair(false, null); + return Pair.pair(false, null); } }, (ignored, input) -> action.apply(input)); } diff --git a/base/src/main/java/bjc/utils/patterns/FunctionalPattern.java b/base/src/main/java/bjc/utils/patterns/FunctionalPattern.java index e4b4a3d..1e9dbab 100644 --- a/base/src/main/java/bjc/utils/patterns/FunctionalPattern.java +++ b/base/src/main/java/bjc/utils/patterns/FunctionalPattern.java @@ -7,11 +7,11 @@ import bjc.data.*; class FunctionalPattern implements ComplexPattern { - private final Function> matcher; + private final Function> matcher; private final BiFunction accepter; FunctionalPattern( - Function> matcher, + Function> matcher, BiFunction accepter) { super(); this.matcher = matcher; @@ -19,7 +19,7 @@ class FunctionalPattern } @Override - public IPair matches(InputType input) { + public Pair matches(InputType input) { return matcher.apply(input); } diff --git a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java index 8e040fe..17de37a 100644 --- a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java @@ -50,7 +50,7 @@ public class MutablePatternMatcher while(iterator.hasNext()) { ComplexPattern pattern = iterator.next(); - IPair matches = pattern.matches(input); + Pair matches = pattern.matches(input); matches.doWith((bool, obj) -> { if (bool) pattern.apply(input, obj); diff --git a/base/src/main/java/bjc/utils/patterns/PatternMatcher.java b/base/src/main/java/bjc/utils/patterns/PatternMatcher.java index e2ae9f6..f144d36 100644 --- a/base/src/main/java/bjc/utils/patterns/PatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/PatternMatcher.java @@ -30,7 +30,7 @@ public class PatternMatcher @Override public ReturnType matchFor(InputType input) throws NonExhaustiveMatch { for (ComplexPattern pattern : patterns) { - IPair matches = pattern.matches(input); + Pair matches = pattern.matches(input); if (matches.getLeft()) { pattern.apply(input, matches.getRight()); } diff --git a/base/src/main/java/bjc/utils/patterns/SimplePatttern.java b/base/src/main/java/bjc/utils/patterns/SimplePatttern.java index 1601894..db53287 100644 --- a/base/src/main/java/bjc/utils/patterns/SimplePatttern.java +++ b/base/src/main/java/bjc/utils/patterns/SimplePatttern.java @@ -34,7 +34,7 @@ public interface SimplePatttern extends Pattern { } @Override - default IPair matches(Object input) { - return new Pair<>(doesMatch(input), null); + default Pair matches(Object input) { + return new SimplePair<>(doesMatch(input), null); } } \ No newline at end of file -- cgit v1.2.3 From f3814a84f8471684cd483347db4fb7b107c2e635 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Thu, 3 Dec 2020 19:28:15 -0500 Subject: Rename interfaces to match Java style Rename several interfaces that were in the style IWhatever, which Java doesn't use --- .../bjc/utils/components/ComponentDescription.java | 6 +- .../bjc/utils/components/ComponentRepository.java | 49 +++++++++++ .../bjc/utils/components/DescribedComponent.java | 64 +++++++++++++++ .../utils/components/FileComponentRepository.java | 4 +- .../bjc/utils/components/IComponentRepository.java | 49 ----------- .../bjc/utils/components/IDescribedComponent.java | 64 --------------- .../components/MemoryComponentRepository.java | 4 +- .../src/main/java/bjc/utils/funcutils/Builder.java | 34 ++++++++ .../main/java/bjc/utils/funcutils/IBuilder.java | 34 -------- .../java/bjc/utils/parserutils/IPrecedent.java | 28 ------- .../main/java/bjc/utils/parserutils/Precedent.java | 28 +++++++ .../java/bjc/utils/parserutils/ShuntingYard.java | 8 +- .../utils/patterns/FunctionalPatternMatcher.java | 2 +- .../java/bjc/utils/patterns/IPatternMatcher.java | 85 ------------------- .../bjc/utils/patterns/MutablePatternMatcher.java | 2 +- .../java/bjc/utils/patterns/PatternMatcher.java | 96 ++++++++++++++++------ .../bjc/utils/patterns/SimplePatternMatcher.java | 41 +++++++++ 17 files changed, 299 insertions(+), 299 deletions(-) create mode 100644 base/src/main/java/bjc/utils/components/ComponentRepository.java create mode 100644 base/src/main/java/bjc/utils/components/DescribedComponent.java delete mode 100644 base/src/main/java/bjc/utils/components/IComponentRepository.java delete mode 100644 base/src/main/java/bjc/utils/components/IDescribedComponent.java create mode 100644 base/src/main/java/bjc/utils/funcutils/Builder.java delete mode 100644 base/src/main/java/bjc/utils/funcutils/IBuilder.java delete mode 100644 base/src/main/java/bjc/utils/parserutils/IPrecedent.java create mode 100644 base/src/main/java/bjc/utils/parserutils/Precedent.java delete mode 100644 base/src/main/java/bjc/utils/patterns/IPatternMatcher.java create mode 100644 base/src/main/java/bjc/utils/patterns/SimplePatternMatcher.java (limited to 'base/src/main/java') diff --git a/base/src/main/java/bjc/utils/components/ComponentDescription.java b/base/src/main/java/bjc/utils/components/ComponentDescription.java index 189ef90..2feed8d 100644 --- a/base/src/main/java/bjc/utils/components/ComponentDescription.java +++ b/base/src/main/java/bjc/utils/components/ComponentDescription.java @@ -5,7 +5,7 @@ package bjc.utils.components; * * @author ben */ -public class ComponentDescription implements IDescribedComponent { +public class ComponentDescription implements DescribedComponent { /* Check arguments are good. */ @SuppressWarnings("unused") private static void sanityCheckArgs(final String name, final String author, @@ -58,7 +58,7 @@ public class ComponentDescription implements IDescribedComponent { @Override public String getAuthor() { if (author == null) { - return IDescribedComponent.super.getAuthor(); + return DescribedComponent.super.getAuthor(); } return author; @@ -67,7 +67,7 @@ public class ComponentDescription implements IDescribedComponent { @Override public String getDescription() { if (description == null) { - return IDescribedComponent.super.getDescription(); + return DescribedComponent.super.getDescription(); } return description; diff --git a/base/src/main/java/bjc/utils/components/ComponentRepository.java b/base/src/main/java/bjc/utils/components/ComponentRepository.java new file mode 100644 index 0000000..120edc8 --- /dev/null +++ b/base/src/main/java/bjc/utils/components/ComponentRepository.java @@ -0,0 +1,49 @@ +package bjc.utils.components; + +import bjc.funcdata.ListEx; +import bjc.funcdata.MapEx; + +/** + * A collection of implementations of a particular type of + * {@link DescribedComponent}. + * + * @author ben + * + * @param + * The type of components contained in this repository. + */ +public interface ComponentRepository { + /** + * 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 MapEx getAll(); + + /** + * Get a component with a specific name. + * + * @param name + * The name of the component to retrieve. + * + * @return The named component, or null if no component with that name exists. + */ + public ComponentType getByName(String name); + + /** + * Get a list of all the registered components. + * + * @return A list of all the registered components. + */ + public default ListEx getList() { + return getAll().valueList(); + } + + /** + * Get the source from which these components came. + * + * @return The source from which these components came. + */ + public String getSource(); +} diff --git a/base/src/main/java/bjc/utils/components/DescribedComponent.java b/base/src/main/java/bjc/utils/components/DescribedComponent.java new file mode 100644 index 0000000..dcbaf59 --- /dev/null +++ b/base/src/main/java/bjc/utils/components/DescribedComponent.java @@ -0,0 +1,64 @@ +package bjc.utils.components; + +/** + * Represents a optional component that has status information associated with + * it. + * + * @author ben + * + */ +public interface DescribedComponent extends Comparable { + /** + * Get the author of this component. + * + * Providing this is optional, with "Anonymous" as the default author. + * + * @return The author of the component. + */ + default String getAuthor() { + return "Anonymous"; + } + + /** + * Get the description of this component. + * + * Providing this is optional, with the default being a note that no description + * was provided. + * + * @return The description of the component + */ + default String getDescription() { + return "No description provided."; + } + + /** + * Get the name of this component. + * + * This is the only thing required of all components. + * + * @return The name of the component. + */ + String getName(); + + /** + * Get the version of this component. + * + * Providing this is optional, with "1" as the default version. + * + * @return The version of this component. + */ + default int getVersion() { + return 1; + } + + @Override + default int compareTo(final DescribedComponent o) { + int res = getName().compareTo(o.getName()); + + if (res == 0) { + res = getVersion() - o.getVersion(); + } + + return res; + } +} diff --git a/base/src/main/java/bjc/utils/components/FileComponentRepository.java b/base/src/main/java/bjc/utils/components/FileComponentRepository.java index fa98d03..e0e929f 100644 --- a/base/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/base/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -24,8 +24,8 @@ import bjc.utils.funcutils.FileUtils; * @param * The type of component being read in. */ -public class FileComponentRepository - implements IComponentRepository { +public class FileComponentRepository + implements ComponentRepository { /* The logger to use for storing data about this class. */ private static final Logger CLASS_LOGGER = Logger.getLogger("FileComponentRepository"); diff --git a/base/src/main/java/bjc/utils/components/IComponentRepository.java b/base/src/main/java/bjc/utils/components/IComponentRepository.java deleted file mode 100644 index 7a40541..0000000 --- a/base/src/main/java/bjc/utils/components/IComponentRepository.java +++ /dev/null @@ -1,49 +0,0 @@ -package bjc.utils.components; - -import bjc.funcdata.ListEx; -import bjc.funcdata.MapEx; - -/** - * A collection of implementations of a particular type of - * {@link IDescribedComponent}. - * - * @author ben - * - * @param - * 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 MapEx getAll(); - - /** - * Get a component with a specific name. - * - * @param name - * The name of the component to retrieve. - * - * @return The named component, or null if no component with that name exists. - */ - public ComponentType getByName(String name); - - /** - * Get a list of all the registered components. - * - * @return A list of all the registered components. - */ - public default ListEx getList() { - return getAll().valueList(); - } - - /** - * Get the source from which these components came. - * - * @return The source from which these components came. - */ - public String getSource(); -} diff --git a/base/src/main/java/bjc/utils/components/IDescribedComponent.java b/base/src/main/java/bjc/utils/components/IDescribedComponent.java deleted file mode 100644 index ae3e06c..0000000 --- a/base/src/main/java/bjc/utils/components/IDescribedComponent.java +++ /dev/null @@ -1,64 +0,0 @@ -package bjc.utils.components; - -/** - * Represents a optional component that has status information associated with - * it. - * - * @author ben - * - */ -public interface IDescribedComponent extends Comparable { - /** - * Get the author of this component. - * - * Providing this is optional, with "Anonymous" as the default author. - * - * @return The author of the component. - */ - default String getAuthor() { - return "Anonymous"; - } - - /** - * Get the description of this component. - * - * Providing this is optional, with the default being a note that no description - * was provided. - * - * @return The description of the component - */ - default String getDescription() { - return "No description provided."; - } - - /** - * Get the name of this component. - * - * This is the only thing required of all components. - * - * @return The name of the component. - */ - String getName(); - - /** - * Get the version of this component. - * - * Providing this is optional, with "1" as the default version. - * - * @return The version of this component. - */ - default int getVersion() { - return 1; - } - - @Override - default int compareTo(final IDescribedComponent o) { - int res = getName().compareTo(o.getName()); - - if (res == 0) { - res = getVersion() - o.getVersion(); - } - - return res; - } -} diff --git a/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java b/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java index 2e11616..f83c293 100644 --- a/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java +++ b/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java @@ -10,8 +10,8 @@ import bjc.funcdata.MapEx; * @param * The type of component stored in the repository. */ -public class MemoryComponentRepository - implements IComponentRepository { +public class MemoryComponentRepository + implements ComponentRepository { private final MapEx repo; private final String source; diff --git a/base/src/main/java/bjc/utils/funcutils/Builder.java b/base/src/main/java/bjc/utils/funcutils/Builder.java new file mode 100644 index 0000000..72c045d --- /dev/null +++ b/base/src/main/java/bjc/utils/funcutils/Builder.java @@ -0,0 +1,34 @@ +package bjc.utils.funcutils; + +/** + * Generic interface for objects that implement the builder pattern. + * + * @author ben + * + * @param + * The type of object being built. + */ +public interface Builder { + /** + * Build the object this builder is building. + * + * @return The built object. + * + * @throws IllegalStateException + * If the data in the builder cannot be built into + * its corresponding object at this point in time. + */ + public E build(); + + /** + * Reset the state of this builder to its initial state. + * + * @throws UnsupportedOperationException + * If the builder doesn't support + * resetting its state. + */ + public default void reset() { + throw new UnsupportedOperationException( + "Builder doesn't support state resetting"); + } +} diff --git a/base/src/main/java/bjc/utils/funcutils/IBuilder.java b/base/src/main/java/bjc/utils/funcutils/IBuilder.java deleted file mode 100644 index b1a2020..0000000 --- a/base/src/main/java/bjc/utils/funcutils/IBuilder.java +++ /dev/null @@ -1,34 +0,0 @@ -package bjc.utils.funcutils; - -/** - * Generic interface for objects that implement the builder pattern. - * - * @author ben - * - * @param - * The type of object being built. - */ -public interface IBuilder { - /** - * Build the object this builder is building. - * - * @return The built object. - * - * @throws IllegalStateException - * If the data in the builder cannot be built into - * its corresponding object at this point in time. - */ - public E build(); - - /** - * Reset the state of this builder to its initial state. - * - * @throws UnsupportedOperationException - * If the builder doesn't support - * resetting its state. - */ - public default void reset() { - throw new UnsupportedOperationException( - "Builder doesn't support state resetting"); - } -} diff --git a/base/src/main/java/bjc/utils/parserutils/IPrecedent.java b/base/src/main/java/bjc/utils/parserutils/IPrecedent.java deleted file mode 100644 index eb164b3..0000000 --- a/base/src/main/java/bjc/utils/parserutils/IPrecedent.java +++ /dev/null @@ -1,28 +0,0 @@ -package bjc.utils.parserutils; - -/** - * Represents something that has a set precedence - * - * @author ben - * - */ -@FunctionalInterface -public interface IPrecedent { - /** - * Create a new object with set precedence - * - * @param precedence - * The precedence of the object to handle - * @return A new object with set precedence - */ - public static IPrecedent newSimplePrecedent(final int precedence) { - return () -> precedence; - } - - /** - * Get the precedence of the attached object - * - * @return The precedence of the attached object - */ - public int getPrecedence(); -} diff --git a/base/src/main/java/bjc/utils/parserutils/Precedent.java b/base/src/main/java/bjc/utils/parserutils/Precedent.java new file mode 100644 index 0000000..33b032c --- /dev/null +++ b/base/src/main/java/bjc/utils/parserutils/Precedent.java @@ -0,0 +1,28 @@ +package bjc.utils.parserutils; + +/** + * Represents something that has a set precedence + * + * @author ben + * + */ +@FunctionalInterface +public interface Precedent { + /** + * Create a new object with set precedence + * + * @param precedence + * The precedence of the object to handle + * @return A new object with set precedence + */ + public static Precedent newSimplePrecedent(final int precedence) { + return () -> precedence; + } + + /** + * Get the precedence of the attached object + * + * @return The precedence of the attached object + */ + public int getPrecedence(); +} diff --git a/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java b/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java index f73c315..f0475ff 100644 --- a/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -25,7 +25,7 @@ public class ShuntingYard { * @author ben * */ - public static enum Operator implements IPrecedent { + public static enum Operator implements Precedent { /** * Represents addition. */ @@ -59,7 +59,7 @@ public class ShuntingYard { /* * Holds all the shuntable operations. */ - private MapEx operators; + private MapEx operators; /** * Create a new shunting yard with a default set of operators. @@ -95,7 +95,7 @@ public class ShuntingYard { /* * Create the precedence marker */ - final IPrecedent prec = IPrecedent.newSimplePrecedent(precedence); + final Precedent prec = Precedent.newSimplePrecedent(precedence); this.addOp(operator, prec); } @@ -109,7 +109,7 @@ public class ShuntingYard { * @param precedence * The precedence of the operator. */ - public void addOp(final String operator, final IPrecedent precedence) { + public void addOp(final String operator, final Precedent precedence) { /* * Complain about trying to add an incorrect operator */ diff --git a/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java b/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java index 5a214d3..e370fa0 100644 --- a/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java @@ -11,7 +11,7 @@ import bjc.functypes.*; * @param The type to match against. */ public class FunctionalPatternMatcher - implements IPatternMatcher { + implements PatternMatcher { private final ThrowFunction matcher; diff --git a/base/src/main/java/bjc/utils/patterns/IPatternMatcher.java b/base/src/main/java/bjc/utils/patterns/IPatternMatcher.java deleted file mode 100644 index b688a47..0000000 --- a/base/src/main/java/bjc/utils/patterns/IPatternMatcher.java +++ /dev/null @@ -1,85 +0,0 @@ -package bjc.utils.patterns; - -import java.util.function.*; - -import bjc.functypes.*; - -/** - * Represents a pattern matcher against a series of patterns. - * - * @author Ben Culkin - * - * @param The type returned from matching the patterns. - * @param The type to match against. - */ -@FunctionalInterface -public interface IPatternMatcher { - /** - * Match an input object against a set of patterns. - * - * @param input The object to match against. - * - * @return The result of matching against the object. - * - * @throws NonExhaustiveMatch If none of the patterns in this set match - */ - ReturnType matchFor(InputType input) throws NonExhaustiveMatch; - - /** - * Create a pattern matcher against a static set of patterns. - * - * @param The type returned from matching the patterns. - * @param The type to match against. - * - * @param patterns The set of patterns to match on. - * - * @return A pattern matcher which matches on the given patterns. - */ - @SafeVarargs - static IPatternMatcher matchingOn( - ComplexPattern... patterns) { - return new PatternMatcher<>(patterns); - } - - /** - * Create a pattern matcher from a handler function. - * - * @param The type returned by the matcher. - * @param The type to match against. - * - * @param handler The handler function. - * - * @return A pattern matcher defined by the given handler. - */ - static IPatternMatcher from( - ThrowFunction handler) { - return new FunctionalPatternMatcher<>(handler); - } - - /** - * Create a pattern matcher which applies a transform to its input. - * - * @param The new input type to use. - * @param transformer The function to convert from the new input to the old input. - * - * @return A pattern matcher which takes values of the new type instead. - */ - default IPatternMatcher transformInput( - Function transformer) { - return from(inp -> matchFor(transformer.apply(inp))); - } - - /** - * Create a pattern matcher which applies a transform to its output. - * - * @param The new output type to use. - * - * @param transformer The function to convert from the new output to the old output. - * - * @return A pattern matcher which takes values of the new type instead. - */ - default IPatternMatcher transformOutput( - Function transformer) { - return from(inp -> transformer.apply(matchFor(inp))); - } -} \ No newline at end of file diff --git a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java index 17de37a..28e9cd7 100644 --- a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java @@ -16,7 +16,7 @@ import bjc.data.*; * @param The type of the input to match against. */ public class MutablePatternMatcher - implements IPatternMatcher { + implements PatternMatcher { private final List> patterns; /** diff --git a/base/src/main/java/bjc/utils/patterns/PatternMatcher.java b/base/src/main/java/bjc/utils/patterns/PatternMatcher.java index f144d36..40bf42b 100644 --- a/base/src/main/java/bjc/utils/patterns/PatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/PatternMatcher.java @@ -1,41 +1,85 @@ package bjc.utils.patterns; -import bjc.data.*; +import java.util.function.*; + +import bjc.functypes.*; /** - * Implements pattern-matching (of a sort) against a collection of patterns. + * Represents a pattern matcher against a series of patterns. * * @author Ben Culkin - * - * @param The type returned by the pattern. + * + * @param The type returned from matching the patterns. + * @param The type to match against. */ -public class PatternMatcher - implements IPatternMatcher { - private final ComplexPattern[] patterns; +@FunctionalInterface +public interface PatternMatcher { + /** + * Match an input object against a set of patterns. + * + * @param input The object to match against. + * + * @return The result of matching against the object. + * + * @throws NonExhaustiveMatch If none of the patterns in this set match + */ + ReturnType matchFor(InputType input) throws NonExhaustiveMatch; /** - * Create a new pattern matcher. + * Create a pattern matcher against a static set of patterns. * - * @param patterns The set of patterns to match against. + * @param The type returned from matching the patterns. + * @param The type to match against. + * + * @param patterns The set of patterns to match on. + * + * @return A pattern matcher which matches on the given patterns. */ - @SuppressWarnings("unchecked") @SafeVarargs - public PatternMatcher(ComplexPattern...patterns) { - // Note: this may seem a somewhat questionable cast, but because we never - // actually do anything with the value who has a type matching the second - // parameter, this should be safe - this.patterns = (ComplexPattern[]) patterns; + static PatternMatcher matchingOn( + ComplexPattern... patterns) { + return new SimplePatternMatcher<>(patterns); + } + + /** + * Create a pattern matcher from a handler function. + * + * @param The type returned by the matcher. + * @param The type to match against. + * + * @param handler The handler function. + * + * @return A pattern matcher defined by the given handler. + */ + static PatternMatcher from( + ThrowFunction handler) { + return new FunctionalPatternMatcher<>(handler); + } + + /** + * Create a pattern matcher which applies a transform to its input. + * + * @param The new input type to use. + * @param transformer The function to convert from the new input to the old input. + * + * @return A pattern matcher which takes values of the new type instead. + */ + default PatternMatcher transformInput( + Function transformer) { + return from(inp -> matchFor(transformer.apply(inp))); } - @Override - public ReturnType matchFor(InputType input) throws NonExhaustiveMatch { - for (ComplexPattern pattern : patterns) { - Pair matches = pattern.matches(input); - if (matches.getLeft()) { - pattern.apply(input, matches.getRight()); - } - } - - throw new NonExhaustiveMatch("Non-exhaustive match against " + input); + /** + * Create a pattern matcher which applies a transform to its output. + * + * @param The new output type to use. + * + * @param transformer The function to convert from the new output to the old output. + * + * @return A pattern matcher which takes values of the new type instead. + */ + default PatternMatcher transformOutput( + Function transformer) { + return from(inp -> transformer.apply(matchFor(inp))); } -} +} \ No newline at end of file diff --git a/base/src/main/java/bjc/utils/patterns/SimplePatternMatcher.java b/base/src/main/java/bjc/utils/patterns/SimplePatternMatcher.java new file mode 100644 index 0000000..fea947a --- /dev/null +++ b/base/src/main/java/bjc/utils/patterns/SimplePatternMatcher.java @@ -0,0 +1,41 @@ +package bjc.utils.patterns; + +import bjc.data.*; + +/** + * Implements pattern-matching (of a sort) against a collection of patterns. + * + * @author Ben Culkin + * + * @param The type returned by the pattern. + */ +public class SimplePatternMatcher + implements PatternMatcher { + private final ComplexPattern[] patterns; + + /** + * Create a new pattern matcher. + * + * @param patterns The set of patterns to match against. + */ + @SuppressWarnings("unchecked") + @SafeVarargs + public SimplePatternMatcher(ComplexPattern...patterns) { + // Note: this may seem a somewhat questionable cast, but because we never + // actually do anything with the value who has a type matching the second + // parameter, this should be safe + this.patterns = (ComplexPattern[]) patterns; + } + + @Override + public ReturnType matchFor(InputType input) throws NonExhaustiveMatch { + for (ComplexPattern pattern : patterns) { + Pair matches = pattern.matches(input); + if (matches.getLeft()) { + pattern.apply(input, matches.getRight()); + } + } + + throw new NonExhaustiveMatch("Non-exhaustive match against " + input); + } +} -- cgit v1.2.3