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/bjc/utils') 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