diff options
| author | Ben Culkin <scorpress@gmail.com> | 2022-09-16 18:58:48 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2022-09-16 18:58:48 -0400 |
| commit | a3d2728f84375566da3da560b3faad018d34005d (patch) | |
| tree | d50829207b8b4418c11d6385f49386259bb2cc8a | |
| parent | 6186f1d87c5e170fa89aa327001706b0692526fc (diff) | |
Cleanup
23 files changed, 508 insertions, 236 deletions
diff --git a/base/.settings/org.eclipse.jdt.core.prefs b/base/.settings/org.eclipse.jdt.core.prefs index de5c7e4..f8c3027 100644 --- a/base/.settings/org.eclipse.jdt.core.prefs +++ b/base/.settings/org.eclipse.jdt.core.prefs @@ -1,4 +1,14 @@ eclipse.preferences.version=1
+org.eclipse.jdt.core.builder.annotationPath.allLocations=disabled
+org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=disabled
+org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
+org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
+org.eclipse.jdt.core.compiler.annotation.nonnull.secondary=
+org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
+org.eclipse.jdt.core.compiler.annotation.nonnullbydefault.secondary=
+org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
+org.eclipse.jdt.core.compiler.annotation.nullable.secondary=
+org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
@@ -7,10 +17,102 @@ org.eclipse.jdt.core.compiler.compliance=17 org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.APILeak=info
+org.eclipse.jdt.core.compiler.problem.annotatedTypeArgumentToUnannotated=info
+org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
+org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning
+org.eclipse.jdt.core.compiler.problem.deadCode=warning
+org.eclipse.jdt.core.compiler.problem.deprecation=warning
+org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
+org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
+org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
+org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=info
+org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
+org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
+org.eclipse.jdt.core.compiler.problem.fieldHiding=info
+org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning
+org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning
+org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled
+org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning
+org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=warning
+org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore
+org.eclipse.jdt.core.compiler.problem.localVariableHiding=info
+org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning
+org.eclipse.jdt.core.compiler.problem.missingDefaultCase=info
+org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore
+org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled
+org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=info
+org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore
+org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
+org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning
+org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore
+org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
+org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
+org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
+org.eclipse.jdt.core.compiler.problem.nonnullParameterAnnotationDropped=warning
+org.eclipse.jdt.core.compiler.problem.nonnullTypeVariableFromLegacyInvocation=warning
+org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
+org.eclipse.jdt.core.compiler.problem.nullReference=warning
+org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
+org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning
+org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning
+org.eclipse.jdt.core.compiler.problem.parameterAssignment=warning
+org.eclipse.jdt.core.compiler.problem.pessimisticNullAnalysisForFreeTypeVariables=warning
+org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore
+org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore
+org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore
+org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning
+org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning
+org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore
+org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=info
+org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=info
+org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
+org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=info
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
+org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
+org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
+org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=enabled
+org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
+org.eclipse.jdt.core.compiler.problem.suppressWarningsNotFullyAnalysed=ignore
+org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled
+org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
+org.eclipse.jdt.core.compiler.problem.terminalDeprecation=warning
+org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning
+org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=disabled
+org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning
+org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning
+org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore
+org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning
+org.eclipse.jdt.core.compiler.problem.unlikelyCollectionMethodArgumentType=warning
+org.eclipse.jdt.core.compiler.problem.unlikelyCollectionMethodArgumentTypeStrict=disabled
+org.eclipse.jdt.core.compiler.problem.unlikelyEqualsArgumentType=info
+org.eclipse.jdt.core.compiler.problem.unnecessaryElse=info
+org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=info
+org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore
+org.eclipse.jdt.core.compiler.problem.unstableAutoModuleName=ignore
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=info
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled
+org.eclipse.jdt.core.compiler.problem.unusedExceptionParameter=ignore
+org.eclipse.jdt.core.compiler.problem.unusedImport=warning
+org.eclipse.jdt.core.compiler.problem.unusedLabel=warning
+org.eclipse.jdt.core.compiler.problem.unusedLocal=warning
+org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore
+org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore
+org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled
+org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled
+org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled
+org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
+org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=info
+org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
+org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=17
diff --git a/base/src/examples/java/bjc/utils/examples/AbbrevMapTest.java b/base/src/examples/java/bjc/utils/examples/AbbrevMapTest.java index a526408..9deaf34 100644 --- a/base/src/examples/java/bjc/utils/examples/AbbrevMapTest.java +++ b/base/src/examples/java/bjc/utils/examples/AbbrevMapTest.java @@ -22,6 +22,7 @@ public class AbbrevMapTest { * Unused CLI args. */ public static void main(final String[] args) { + @SuppressWarnings("resource") final Scanner scn = new Scanner(System.in); final AbbrevMap2 map = new AbbrevMap2(); diff --git a/base/src/examples/java/bjc/utils/examples/ShuntTest.java b/base/src/examples/java/bjc/utils/examples/ShuntTest.java index 39d37ee..b20012a 100644 --- a/base/src/examples/java/bjc/utils/examples/ShuntTest.java +++ b/base/src/examples/java/bjc/utils/examples/ShuntTest.java @@ -21,19 +21,17 @@ public class ShuntTest * Unused CLI args */ public static void main(final String[] args) { - Scanner inputSource = new Scanner(System.in); + try (Scanner inputSource = new Scanner(System.in)) { + System.out.print("Enter a expression to shunt: "); + String line = inputSource.nextLine(); - System.out.print("Enter a expression to shunt: "); - String line = inputSource.nextLine(); + ShuntingYard<String> yard = new ShuntingYard<>(true); - ShuntingYard<String> yard = new ShuntingYard<>(true); + FunctionalStringTokenizer tokenizer = new FunctionalStringTokenizer(line); + ListEx<String> preTokens = tokenizer.toList(strang -> strang); + ListEx<String> shuntedTokens = yard.postfix(preTokens, strang -> strang); - FunctionalStringTokenizer tokenizer = new FunctionalStringTokenizer(line); - ListEx<String> preTokens = tokenizer.toList(strang -> strang); - ListEx<String> shuntedTokens = yard.postfix(preTokens, strang -> strang); - - System.out.println(shuntedTokens.toString()); - - inputSource.close(); + System.out.println(shuntedTokens.toString()); + } } } diff --git a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java index 1c50df3..6b4bbb3 100644 --- a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java +++ b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java @@ -114,9 +114,9 @@ public class BlockReaderCLI { sources.put("stdio", new InputStreamReader(System.in)); - Scanner input = new Scanner(System.in); - reader.run(input, "console", true); - input.close(); + try (Scanner input = new Scanner(System.in)) { + reader.run(input, "console", true); + } } /** diff --git a/base/src/main/java/bjc/utils/graph/Graphs.java b/base/src/main/java/bjc/utils/graph/Graphs.java new file mode 100644 index 0000000..2844a68 --- /dev/null +++ b/base/src/main/java/bjc/utils/graph/Graphs.java @@ -0,0 +1,67 @@ +package bjc.utils.graph; + +import java.util.*; + +import bjc.data.Holder; +import bjc.data.Identity; + +public class Graphs { + /** + * Uses Prim's algorithm to calculate a MST for the graph. + * + * If the graph is non-connected, this will lead to unpredictable results. + * + * @return A list of edges that constitute the MST. + */ + public static <T, L> List<Edge<T, L>> getMinimumSpanningTree(Graph<T, L> grap, Comparator<L> comp) { + /* Set of all of the currently available edges. */ + final Queue<Edge<T, L>> available = new PriorityQueue<>(10, + (left, right) -> comp.compare(left.getDistance(), right.getDistance())); + + /* The MST of the graph. */ + final List<Edge<T, L>> minimums = new ArrayList<>(); + + /* The set of all of the visited vertices. */ + final Set<T> visited = new HashSet<>(); + + /* Start at the initial vertex and visit it */ + final Holder<T> source = new Identity<>(grap.getInitial()); + + visited.add(source.getValue()); + + /* Make sure we visit all the nodes. */ + while (visited.size() != grap.getVertexCount()) { + /* Grab all edges adjacent to the provided edge. */ + + grap.forAllEdgesMatchingAt(source.getValue(), + (target, weight) -> !visited.contains(target), + (target, weight) -> { + final T vert = source.unwrap(vertex -> vertex); + + available.add(new Edge<>(vert, target, weight)); + } + ); + + /* Get the edge with the minimum distance. */ + final Holder<Edge<T, L>> minimum = new Identity<>(available.poll()); + + /* + * Only consider edges where we haven't visited the target of the edge. + */ + while (visited.contains(minimum.getValue().getTarget())) { + minimum.transform(edge -> available.poll()); + } + + /* Add it to our MST. */ + minimums.add(minimum.getValue()); + + /* Advance to the next node. */ + source.transform(vertex -> minimum.unwrap(edge -> edge.getTarget())); + + /* Visit this node. */ + visited.add(source.getValue()); + } + + return minimums; + } +} diff --git a/base/src/main/java/bjc/utils/ioutils/DuplicateKeys.java b/base/src/main/java/bjc/utils/ioutils/DuplicateKeys.java new file mode 100644 index 0000000..604cc69 --- /dev/null +++ b/base/src/main/java/bjc/utils/ioutils/DuplicateKeys.java @@ -0,0 +1,21 @@ +package bjc.utils.ioutils; + +/** + * Exception thrown when there is a duplicate key, when they are forbidden. + * + * @author 15405 + * + */ +public class DuplicateKeys extends RuntimeException { + private static final long serialVersionUID = -5521190136366024804L; + + /** + * Create a new duplicate key exception. + * + * @param keyName + * The name of the key that has been duplicated. + */ + public DuplicateKeys(String keyName) { + super(String.format("Duplicate value encountered for key '%s'", keyName)); + } +}
\ No newline at end of file diff --git a/base/src/main/java/bjc/utils/ioutils/InvalidLineFormat.java b/base/src/main/java/bjc/utils/ioutils/InvalidLineFormat.java new file mode 100644 index 0000000..2763eb1 --- /dev/null +++ b/base/src/main/java/bjc/utils/ioutils/InvalidLineFormat.java @@ -0,0 +1,20 @@ +package bjc.utils.ioutils; + +/** + * Exception thrown when a line is formattted incorrectly. + * @author Ben Culkin + * + */ +public class InvalidLineFormat extends RuntimeException { + private static final long serialVersionUID = 5332131472090792841L; + + /** + * Create a new exception for an incorrectly formatted line. + * @param lne The line that was incorrectly formatted. + */ + public InvalidLineFormat(String lne) { + super(String.format( + "Line '%s' is improperly formatted.\n\tExpected format is a string key, followed by a single space, followed by the value", + "")); + } +}
\ No newline at end of file diff --git a/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java b/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java index d380866..0aa18b4 100644 --- a/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java +++ b/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java @@ -17,45 +17,6 @@ import java.util.Set; * */ public class SimpleProperties implements Map<String, String> { - /** - * Exception thrown when there is a duplicate key, when they are forbidden. - * - * @author 15405 - * - */ - public static class DuplicateKeys extends RuntimeException { - private static final long serialVersionUID = -5521190136366024804L; - - /** - * Create a new duplicate key exception. - * - * @param keyName - * The name of the key that has been duplicated. - */ - public DuplicateKeys(String keyName) { - super(String.format("Duplicate value encountered for key '%s'", keyName)); - } - } - - /** - * Exception thrown when a line is formattted incorrectly. - * @author Ben Culkin - * - */ - public static class InvalidLineFormat extends RuntimeException { - private static final long serialVersionUID = 5332131472090792841L; - - /** - * Create a new exception for an incorrectly formatted line. - * @param lne The line that was incorrectly formatted. - */ - public InvalidLineFormat(String lne) { - super(String.format( - "Line '%s' is improperly formatted.\n\tExpected format is a string key, followed by a single space, followed by the value", - "")); - } - } - private final Map<String, String> props; /** @@ -170,20 +131,17 @@ public class SimpleProperties implements Map<String, String> { return props.isEmpty(); } - @SuppressWarnings("unlikely-arg-type") - @Override + @Override public boolean containsKey(final Object key) { return props.containsKey(key); } - @SuppressWarnings("unlikely-arg-type") - @Override + @Override public boolean containsValue(final Object value) { return props.containsValue(value); } - @SuppressWarnings("unlikely-arg-type") - @Override + @Override public String get(final Object key) { return props.get(key); } @@ -193,8 +151,7 @@ public class SimpleProperties implements Map<String, String> { return props.put(key, value); } - @SuppressWarnings("unlikely-arg-type") - @Override + @Override public String remove(final Object key) { return props.remove(key); } diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java index 265a781..9617ebd 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java @@ -102,6 +102,8 @@ public class SerialBlockReader implements BlockReader { @Override public void close() throws IOException { while (!readerQueue.isEmpty()) { + // We are explicitly closing these + @SuppressWarnings("resource") final BlockReader reader = readerQueue.pop(); reader.close(); diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/package-info.java b/base/src/main/java/bjc/utils/ioutils/blocks/package-info.java index 3d05ed4..d321725 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/package-info.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/package-info.java @@ -2,16 +2,16 @@ * is structured as a series of 'blocks' or records. * <p> * - * The most fundamental unit here is that of {@link Block}. Each {@link - * BlockReader} will yield a sequence of these, which contain a piece of text + * The most fundamental unit here is that of {@link bjc.utils.ioutils.blocks.Block}. Each {@link + * bjc.utils.ioutils.blocks.BlockReader} will yield a sequence of these, which contain a piece of text * as its contents, as well as the beginning/ending line for that block. * - * There are a number of different types of {@link BlockReader}, which are + * There are a number of different types of {@link bjc.utils.ioutils.blocks.BlockReader}, which are * summarized here. * </p> * * <dl> - * <dt>{@link SimpleBlockReader}</dt> + * <dt>{@link bjc.utils.ioutils.blocks.SimpleBlockReader}</dt> * <dd> * The most basic form of BlockReader. This uses a regular expression to * delimit input reader from a {@link Reader} into a series of blocks. diff --git a/base/src/main/java/bjc/utils/parserutils/pattern/FunctionalPatternPart.java b/base/src/main/java/bjc/utils/parserutils/pattern/FunctionalPatternPart.java new file mode 100644 index 0000000..535825e --- /dev/null +++ b/base/src/main/java/bjc/utils/parserutils/pattern/FunctionalPatternPart.java @@ -0,0 +1,23 @@ +package bjc.utils.parserutils.pattern; + +import java.util.function.Supplier; + +final class FunctionalPatternPart implements PatternPart { + private final Supplier<String> func; + private final boolean canOptimize; + + FunctionalPatternPart(Supplier<String> func, boolean canOptimize) { + this.func = func; + this.canOptimize = canOptimize; + } + + @Override + public String toRegex() { + return func.get(); + } + + @Override + public boolean canOptimize() { + return canOptimize; + } +}
\ No newline at end of file diff --git a/base/src/main/java/bjc/utils/parserutils/pattern/JoinerPatternPart.java b/base/src/main/java/bjc/utils/parserutils/pattern/JoinerPatternPart.java new file mode 100644 index 0000000..2f3b16d --- /dev/null +++ b/base/src/main/java/bjc/utils/parserutils/pattern/JoinerPatternPart.java @@ -0,0 +1,28 @@ +package bjc.utils.parserutils.pattern; + +import java.util.StringJoiner; + +final class JoinerPatternPart implements PatternPart { + private final PatternPart[] parts; + private final String joiner; + + JoinerPatternPart(PatternPart[] parts, String joiner) { + this.parts = parts; + this.joiner = joiner; + } + + @Override + public String toRegex() { + StringJoiner sj = new StringJoiner(joiner); + for (PatternPart part : parts) sj.add(part.toRegex()); + return sj.toString(); + } + + @Override + public boolean canOptimize() { + for (PatternPart part : parts) + if (!part.canOptimize()) return false; + + return true; + } +}
\ No newline at end of file diff --git a/base/src/main/java/bjc/utils/parserutils/pattern/PatternPart.java b/base/src/main/java/bjc/utils/parserutils/pattern/PatternPart.java index 6661072..2fd59ca 100644 --- a/base/src/main/java/bjc/utils/parserutils/pattern/PatternPart.java +++ b/base/src/main/java/bjc/utils/parserutils/pattern/PatternPart.java @@ -1,12 +1,14 @@ package bjc.utils.parserutils.pattern; -import java.util.StringJoiner; import java.util.function.Supplier; import java.util.regex.*; /** * Builder interface for regex patterns. * + * Note that you may need to add explicit non-grouping to get things to work + * right, based on the precedence of your regex operators. + * * @author bjculkin * */ @@ -17,27 +19,37 @@ public interface PatternPart { * @return The regex this part represents. */ public String toRegex(); - + + /** + * Can this regex be optimized? + * + * @return Whether or not this regex can be optimized to a string + */ public boolean canOptimize(); - + + /** + * Create a new pattern part from component bits. + * + * @param canOptimize Whether this part can be optimized + * @param func The function that provides the regex text + * + * @return A pattern part w/ the given body + */ static PatternPart part(boolean canOptimize, Supplier<String> func) { - return new PatternPart() { - - @Override - public String toRegex() { - return func.get(); - } - - @Override - public boolean canOptimize() { - return canOptimize; - } - }; - } - + return new FunctionalPatternPart(func, canOptimize); + } + + /** + * Create a new variable pattern part. + * + * @param source The function which supplies the text. + * + * @return A part that retrieves its bits from the given source + */ static PatternPart var(Supplier<String> source) { return part(false, source); } + /** * Create a 'raw' pattern part, which just echoes the given string. * @@ -48,26 +60,20 @@ public interface PatternPart { static PatternPart raw(String str) { return part(true, () -> str); } - + + /** + * Create a pattern composed of other patterns, interspersed with the given + * string. + * + * @param joiner The string to use as a joiner. + * @param parts The composed pattern parts. + * + * @return The given patterns composed by the parts, joined by `joiner`. + */ static PatternPart joining(String joiner, PatternPart... parts) { - return new PatternPart() { - - @Override - public String toRegex() { - StringJoiner sj = new StringJoiner(joiner); - for (PatternPart part : parts) sj.add(part.toRegex()); - return sj.toString(); - } - - @Override - public boolean canOptimize() { - for (PatternPart part : parts) - if (!part.canOptimize()) return false; - - return true; - } - }; + return new JoinerPatternPart(parts, joiner); } + /** * Create a pattern part which matches the given string. * @@ -78,7 +84,7 @@ public interface PatternPart { static PatternPart literal(String str) { return part(true, () -> Pattern.quote(str)); } - + /** * Create a pattern part which matches a single digit. * @@ -87,60 +93,149 @@ public interface PatternPart { static PatternPart digit() { return raw("\\d"); } - + + /** + * Create a character class pattern + * + * @param chars The characters that make up the class. + * + * @return A pattern representing the character class. + */ static PatternPart cclass(char... chars) { return part(true, () -> { StringBuilder sb = new StringBuilder("["); - for (char ch : chars) sb.append(ch); + for (char ch : chars) + sb.append(ch); sb.append("]"); return sb.toString(); }); } - + + /** + * Represents an inverted character class. + * + * @param chars The characters for the class not to include. + * + * @return A pattern representing a class that doesn't match the characters. + */ static PatternPart notCClass(char... chars) { return part(true, () -> { StringBuilder sb = new StringBuilder("[^"); - for (char ch : chars) sb.append(ch); + for (char ch : chars) + sb.append(ch); sb.append("]"); return sb.toString(); }); } - + + /** + * Represents a pattern that matches any non-space character. + * + * @return A pattern that matches any non-space character. + */ static PatternPart nonspace() { return raw("\\S"); } - + + /** + * Concatenate a series of pattern parts with whitespace. + * + * @param parts The parts to join + * + * @return A pattern that matches each of the given parts, separated by + * whitespace. + */ static PatternPart concat(PatternPart... parts) { return joining(" ", parts); } - + + /** + * Create a pattern which matches one of the given patterns. + * + * @param parts The possible patterns to match. + * + * @return A pattern which matches one of the given patterns. + */ static PatternPart alternate(PatternPart... parts) { return joining("|", parts); } - + + /** + * Create a pattern which matches the given pattern zero or more times. + * + * @param part The pattern to repeat + * + * @return A pattern which matches the given one zero or more times. + */ static PatternPart repeat(PatternPart part) { return part(part.canOptimize(), () -> part.toRegex() + "*"); } - + + /** + * Create a pattern which matches the given one zero or more times. + * + * @param part The pattern to be optional. + * + * @return A pattern where the part is optional + */ static PatternPart optional(PatternPart part) { return part(part.canOptimize(), () -> part.toRegex() + "?"); } - + + /** + * Create a pattern which matches the given pattern one or more times. + * + * @param part The pattern to repeat. + * + * @return A pattern which matches the given one one or more times. + */ static PatternPart repeatAtLeastOnce(PatternPart part) { return part(part.canOptimize(), () -> part.toRegex() + "*"); } - + + /** + * Surround the given pattern with strings. + * + * @param lhs The left-hand side of the pattern. + * @param rhs The right-hand side of the pattern. + * @param part The pattern to match. + * + * @return A pattern surrounded by the given strings. + */ static PatternPart surround(String lhs, String rhs, PatternPart part) { return part(part.canOptimize(), () -> lhs + part.toRegex() + rhs); } + + /** + * Wrap the given pattern in a capturing group. + * + * @param part The pattern to wrap. + * + * @return The pattern, wrapped in a capturing group + */ static PatternPart group(PatternPart part) { return surround("(", ")", part); } + /** + * Wrap the given pattern in a named-capturing group. + * + * @param groupName The name of the group + * @param part The pattern to wrap. + * + * @return A pattern wrap in a named-capturing group. + */ static PatternPart namedGroup(String groupName, PatternPart part) { return surround("(<" + groupName + ">", ")", part); } - + + /** + * Wrap the given pattern in a non-capturing group. + * + * @param part The pattern to wrap. + * + * @return A pattern wrap in a non-capturing group. + */ static PatternPart nonCaptureGroup(PatternPart part) { return surround("(?:", ")", part); } diff --git a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java index 176f588..28e9cd7 100644 --- a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java @@ -79,8 +79,7 @@ public class MutablePatternMatcher<ReturnType, InputType> * * @return Whether or not the pattern was removed. */ - @SuppressWarnings("unlikely-arg-type") - public boolean removePattern(ComplexPattern<ReturnType, ?, InputType> pattern) { + public boolean removePattern(ComplexPattern<ReturnType, ?, InputType> pattern) { return patterns.remove(pattern); } } diff --git a/base/src/main/java/module-info.java b/base/src/main/java/module-info.java index 3901514..1032ce5 100644 --- a/base/src/main/java/module-info.java +++ b/base/src/main/java/module-info.java @@ -1,3 +1,11 @@ +/** + * Various utilities and other things. + * + * Just contains a whole bunch of useful things. + * + * @author bjculkin + * + */ module bjc.utils { exports bjc.utils.exceptions; exports bjc.utils.funcutils; @@ -22,11 +30,11 @@ module bjc.utils { exports bjc.utils.ioutils.properties; requires commons.lang3; - requires esodata; + requires transitive esodata; requires guava; requires hamcrest.core; requires icu4j; - requires java.desktop; + requires transitive java.desktop; requires java.logging; requires junit; }
\ No newline at end of file diff --git a/base/src/test/java/bjc/utils/test/ioutils/SimplePropertiesTest.java b/base/src/test/java/bjc/utils/test/ioutils/SimplePropertiesTest.java index 5f166ab..cf3809a 100644 --- a/base/src/test/java/bjc/utils/test/ioutils/SimplePropertiesTest.java +++ b/base/src/test/java/bjc/utils/test/ioutils/SimplePropertiesTest.java @@ -6,9 +6,9 @@ import java.io.StringReader; import org.junit.Test; +import bjc.utils.ioutils.DuplicateKeys; +import bjc.utils.ioutils.InvalidLineFormat; import bjc.utils.ioutils.SimpleProperties; -import bjc.utils.ioutils.SimpleProperties.DuplicateKeys; -import bjc.utils.ioutils.SimpleProperties.InvalidLineFormat; /** * Tests for SimpleProperties. diff --git a/clformat/.settings/org.eclipse.jdt.core.prefs b/clformat/.settings/org.eclipse.jdt.core.prefs index 74568b3..aeacc3c 100644 --- a/clformat/.settings/org.eclipse.jdt.core.prefs +++ b/clformat/.settings/org.eclipse.jdt.core.prefs @@ -1,109 +1,7 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=disabled -org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore -org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull -org.eclipse.jdt.core.compiler.annotation.nonnull.secondary= -org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault -org.eclipse.jdt.core.compiler.annotation.nonnullbydefault.secondary= -org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable -org.eclipse.jdt.core.compiler.annotation.nullable.secondary= -org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 org.eclipse.jdt.core.compiler.compliance=17 -org.eclipse.jdt.core.compiler.problem.APILeak=warning -org.eclipse.jdt.core.compiler.problem.annotatedTypeArgumentToUnannotated=info -org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning -org.eclipse.jdt.core.compiler.problem.autoboxing=ignore -org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning -org.eclipse.jdt.core.compiler.problem.deadCode=warning -org.eclipse.jdt.core.compiler.problem.deprecation=warning -org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled -org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled -org.eclipse.jdt.core.compiler.problem.discouragedReference=warning -org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled -org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore -org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore -org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled -org.eclipse.jdt.core.compiler.problem.fieldHiding=info -org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning -org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning -org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=enabled -org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning -org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=warning -org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore -org.eclipse.jdt.core.compiler.problem.localVariableHiding=info -org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning -org.eclipse.jdt.core.compiler.problem.missingDefaultCase=info -org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore -org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled -org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=info -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled -org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning -org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore -org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning -org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning -org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore -org.eclipse.jdt.core.compiler.problem.nonnullParameterAnnotationDropped=warning -org.eclipse.jdt.core.compiler.problem.nonnullTypeVariableFromLegacyInvocation=warning -org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error -org.eclipse.jdt.core.compiler.problem.nullReference=warning -org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error -org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning -org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning -org.eclipse.jdt.core.compiler.problem.parameterAssignment=warning -org.eclipse.jdt.core.compiler.problem.pessimisticNullAnalysisForFreeTypeVariables=warning -org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=info -org.eclipse.jdt.core.compiler.problem.potentialNullReference=info -org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore -org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning -org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning -org.eclipse.jdt.core.compiler.problem.redundantNullCheck=info -org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=info -org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=info -org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore -org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore -org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled -org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning -org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled -org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled -org.eclipse.jdt.core.compiler.problem.suppressWarningsNotFullyAnalysed=info -org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled -org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore -org.eclipse.jdt.core.compiler.problem.terminalDeprecation=warning -org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning -org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=disabled -org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning -org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning -org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=info -org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning -org.eclipse.jdt.core.compiler.problem.unlikelyCollectionMethodArgumentType=warning -org.eclipse.jdt.core.compiler.problem.unlikelyCollectionMethodArgumentTypeStrict=disabled -org.eclipse.jdt.core.compiler.problem.unlikelyEqualsArgumentType=info -org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore -org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=info -org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore -org.eclipse.jdt.core.compiler.problem.unstableAutoModuleName=warning -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=info -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled -org.eclipse.jdt.core.compiler.problem.unusedExceptionParameter=ignore -org.eclipse.jdt.core.compiler.problem.unusedImport=warning -org.eclipse.jdt.core.compiler.problem.unusedLabel=warning -org.eclipse.jdt.core.compiler.problem.unusedLocal=warning -org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled -org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning -org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=info -org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning -org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning org.eclipse.jdt.core.compiler.release=disabled org.eclipse.jdt.core.compiler.source=17 diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java b/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java index 7ed76d2..9fd56f5 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java @@ -128,6 +128,8 @@ public class CLTokenizer implements Iterator<SimpleDecree> { dir = new SimpleDecree(directiveName, isUser, CLParameters.fromDirective(directiveParameterString), CLModifiers.fromString(directiveModifierString)); + + dir.setPosition(mat.start(), mat.end()); } if (tmp.equals("")) { @@ -138,7 +140,7 @@ public class CLTokenizer implements Iterator<SimpleDecree> { return dcr; } - return new SimpleDecree(sb.toString()); + return new SimpleDecree(sb.toString(), mat.start(), mat.end()); } mat.appendTail(sb); @@ -182,7 +184,10 @@ public class CLTokenizer implements Iterator<SimpleDecree> { GroupDecree newGroup = new GroupDecree(); newGroup.opening = openedWith; - if (!hasNext()) throw new NoSuchElementException("No decrees available"); + if (!hasNext()) { + String fmt = "No decrees available for group starting with %s at %#s"; + throw new NoSuchElementException(String.format(fmt, openedWith.name, openedWith.position)); + } ClauseDecree curClause = new ClauseDecree(); @@ -233,8 +238,8 @@ public class CLTokenizer implements Iterator<SimpleDecree> { if (newGroup.closing == null) { String msg = String.format( - "Did not find closing directive for group (wanted \"%s\", last decree was \"%s\")", - desiredClosing, curDecree.name); + "Did not find closing directive for group (wanted \"%s\", last decree was \"%s\" at %#s)", + desiredClosing, curDecree.name, curDecree.position); throw new NoSuchElementException(msg); } diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/SimpleDecree.java b/clformat/src/main/java/bjc/utils/ioutils/format/SimpleDecree.java index a7db42f..35d4355 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/SimpleDecree.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/SimpleDecree.java @@ -1,5 +1,8 @@ package bjc.utils.ioutils.format; +import bjc.data.Pair; +import bjc.data.SimplePair; + /** * A decree is the building blocks of what we need to pick and call a directive. * @@ -35,6 +38,11 @@ public class SimpleDecree implements Decree { public CLModifiers modifiers; /** + * The position in the input this decree had + */ + public Pair<Integer, Integer> position; + + /** * Create a new blank decree. */ public SimpleDecree() { @@ -54,6 +62,20 @@ public class SimpleDecree implements Decree { } /** + * Create a new literal text directive. + * + * @param txt + * The text of the directive. + * @param start The starting position of this directive + * @param end The ending position of this directive + */ + public SimpleDecree(String txt, int start, int end) { + this(txt); + + this.setPosition(start, end); + } + + /** * Create a new directive. * * @param name @@ -120,6 +142,16 @@ public class SimpleDecree implements Decree { else return name.equals(nam); } + /** + * Set the position of this decree. + * + * @param start The starting position of this decree + * @param end The ending position of this decree + */ + public void setPosition(int start, int end) { + position = new SimplePair<>(start, end); + } + @Override public String toString() { return String.format( diff --git a/clformat/src/main/java/module-info.java b/clformat/src/main/java/module-info.java index d32a9a7..51396bf 100644 --- a/clformat/src/main/java/module-info.java +++ b/clformat/src/main/java/module-info.java @@ -1,10 +1,21 @@ +/** + * Represents an implementation of string formatting which is inspired by Common + * Lisps FORMAT. + * + * I say inspired because there are a number of extensions to it, as well as a + * few things that either aren't implemented, or are implemented in a different + * way. + * + * @author bjculkin + * + */ module clformat { exports bjc.utils.ioutils.format.directives; exports bjc.utils.ioutils.format; exports bjc.utils.ioutils.format.exceptions; - requires bjc.utils; - requires esodata; + requires transitive bjc.utils; + requires transitive esodata; requires inflexion; requires junit; }
\ No newline at end of file diff --git a/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java b/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java index 629e802..250e52c 100644 --- a/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java +++ b/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java @@ -101,13 +101,12 @@ public class CLFormatterTest { assertEquals("XIV xiv", format("~@R ~(~@R~)", 14, 14)); } - // @Test +// @Test public void testListPrinting() { // Test printing a list - // String fmtStr = "Items:~#[ none~; ~A~; ~A and ~A~:;~@{~#[~; and~] - // ~A~^,~}~]."; - String fmtStr - = "Items:~#[ none~; ~A~; ~A and ~A~:;~@{~#*[ ~A,~; and ~A~; ~A~]~}~]."; + String fmtStr = "Items:~#[ none~; ~A~; ~A and ~A~:;~@{~#[~; and~]~A~^,~}~]."; +// String fmtStr +// = "Items:~#[ none~; ~A~; ~A and ~A~:;~@{~#*[ ~A,~; and ~A~; ~A~]~}~]."; fmt.DEBUG = true; assertEquals("Items: none.", format(fmtStr)); diff --git a/commander/src/example/java/bjc/commander/CommanderCLI.java b/commander/src/example/java/bjc/commander/CommanderCLI.java index 0f208b6..e462fb5 100644 --- a/commander/src/example/java/bjc/commander/CommanderCLI.java +++ b/commander/src/example/java/bjc/commander/CommanderCLI.java @@ -19,9 +19,9 @@ public class CommanderCLI { reader.ioReaders.put("stdio", new InputStreamReader(System.in)); - Scanner input = new Scanner(System.in); - reader.run(input, "console", true); - input.close(); + try (Scanner input = new Scanner(System.in)) { + reader.run(input, "console", true); + } } } diff --git a/commander/src/main/java/module-info.java b/commander/src/main/java/module-info.java index 258d184..64b2250 100644 --- a/commander/src/main/java/module-info.java +++ b/commander/src/main/java/module-info.java @@ -1,6 +1,12 @@ +/** + * CLI interface for various other libraries. + * + * @author bjcul + * + */ module commander { exports bjc.commander; - requires bjc.utils; + requires transitive bjc.utils; requires java.logging; }
\ No newline at end of file |
