Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Simplify predicates

AuthorDaveJarvis <email>
Date2020-08-17 19:27:32 GMT-0700
Commit4bd7bfd10048114accaca4ca81bfe82a3d665692
Parent3360063
src/main/java/com/scrivenvar/AbstractFileFactory.java
package com.scrivenvar;
-import com.scrivenvar.predicates.files.FileTypePredicate;
import com.scrivenvar.service.Settings;
import java.nio.file.Path;
-import java.util.List;
import static com.scrivenvar.Constants.GLOB_PREFIX_FILE;
import static com.scrivenvar.Constants.SETTINGS;
import static com.scrivenvar.FileType.UNKNOWN;
+import static com.scrivenvar.predicates.PredicateFactory.createFileTypePredicate;
import static java.lang.String.format;
final var keys = settings.getKeys( prefix );
- boolean found = false;
- FileType fileType = UNKNOWN;
+ var found = false;
+ var fileType = UNKNOWN;
while( keys.hasNext() && !found ) {
- final String key = keys.next();
- final List<String> patterns = settings.getStringSettingList( key );
- final FileTypePredicate predicate = new FileTypePredicate( patterns );
+ final var key = keys.next();
+ final var patterns = settings.getStringSettingList( key );
+ final var predicate = createFileTypePredicate( patterns );
if( found = predicate.test( path.toFile() ) ) {
src/main/java/com/scrivenvar/FileEditorTabPane.java
package com.scrivenvar;
-import com.scrivenvar.predicates.files.FileTypePredicate;
import com.scrivenvar.service.Options;
import com.scrivenvar.service.Settings;
import static com.scrivenvar.FileType.*;
import static com.scrivenvar.Messages.get;
+import static com.scrivenvar.predicates.PredicateFactory.createFileTypePredicate;
import static com.scrivenvar.service.events.Notifier.YES;
final List<String> extensions =
createExtensionFilter( DEFINITION ).getExtensions();
- final FileTypePredicate predicate =
- new FileTypePredicate( extensions );
+ final var predicate = createFileTypePredicate( extensions );
// The user might have opened multiple definitions files. These will
// be discarded from the text editable files.
- final List<File> definitions
+ final var definitions
= files.stream().filter( predicate ).collect( Collectors.toList() );
// Create a modifiable list to remove any definition files that were
// opened.
- final List<File> editors = new ArrayList<>( files );
+ final var editors = new ArrayList<>( files );
if( !editors.isEmpty() ) {
src/main/java/com/scrivenvar/predicates/PredicateFactory.java
+/*
+ * Copyright 2020 White Magic Software, Ltd.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * o Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * o Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.scrivenvar.predicates;
+
+import java.io.File;
+import java.util.Collection;
+import java.util.function.Predicate;
+
+import static java.lang.String.join;
+import static java.nio.file.FileSystems.getDefault;
+
+/**
+ * Provides a number of simple {@link Predicate} instances for various types
+ * of string comparisons, including basic strings and file name strings.
+ */
+public class PredicateFactory {
+ /**
+ * Creates an instance of {@link Predicate} that matches a globbed file
+ * name pattern.
+ *
+ * @param pattern The file name pattern to match.
+ * @return A {@link Predicate} that can answer whether a given file name
+ * matches the given glob pattern.
+ */
+ public static Predicate<File> createFileTypePredicate(
+ final String pattern ) {
+ final var matcher = getDefault().getPathMatcher(
+ "glob:**{" + pattern + "}"
+ );
+
+ return file -> matcher.matches( file.toPath() );
+ }
+
+ /**
+ * Creates an instance of {@link Predicate} that matches any file name from
+ * a {@link Collection} of file name patterns. The given patterns are joined
+ * with commas into a single comma-separated list.
+ *
+ * @param patterns The file name patterns to be matched.
+ * @return A {@link Predicate} that can answer whether a given file name
+ * matches the given glob patterns.
+ */
+ public static Predicate<File> createFileTypePredicate(
+ final Collection<String> patterns ) {
+ return createFileTypePredicate( join( ",", patterns ) );
+ }
+
+ /**
+ * Creates an instance of {@link Predicate} that compares whether the given
+ * {@code reference} string is contained by the comparator. Comparison is
+ * case-insensitive. The test will also pass if the comparate is empty.
+ *
+ * @param comparator The string to check as being contained.
+ * @return A {@link Predicate} that can answer whether the given string
+ * is contained within the comparator, or the comparate is empty.
+ */
+ public static Predicate<String> createStringContainsPredicate(
+ final String comparator ) {
+ return comparate -> comparate.isEmpty() ||
+ comparate.toLowerCase().contains( comparator.toLowerCase() );
+ }
+ /**
+ * Creates an instance of {@link Predicate} that compares whether the given
+ * {@code reference} string is starts with the comparator. Comparison is
+ * case-insensitive.
+ *
+ * @param comparator The string to check as being contained.
+ * @return A {@link Predicate} that can answer whether the given string
+ * is contained within the comparator.
+ */
+ public static Predicate<String> createStringStartsPredicate(
+ final String comparator ) {
+ return comparate ->
+ comparate.toLowerCase().startsWith( comparator.toLowerCase() );
+ }
+}
src/main/java/com/scrivenvar/predicates/files/FileTypePredicate.java
-/*
- * Copyright 2020 White Magic Software, Ltd.
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.scrivenvar.predicates.files;
-
-import java.io.File;
-import java.nio.file.FileSystems;
-import java.nio.file.PathMatcher;
-import java.util.Collection;
-import java.util.function.Predicate;
-
-/**
- * Responsible for testing whether a given path (to a file) matches one of the
- * filename extension patterns provided during construction.
- */
-public class FileTypePredicate implements Predicate<File> {
-
- private final PathMatcher mMatcher;
-
- /**
- * Constructs a new instance given a set of file extension globs.
- *
- * @param patterns Comma-separated list of globbed extensions including the
- * Kleene star (e.g., <code>*.md,*.markdown,*.txt</code>).
- */
- public FileTypePredicate( final String patterns ) {
- mMatcher = FileSystems.getDefault().getPathMatcher(
- "glob:**{" + patterns + "}"
- );
- }
-
- /**
- * Constructs a new instance given a list of file extension globs, each must
- * include the Kleene star (a.k.a. asterisk).
- *
- * @param patterns Collection of globbed extensions.
- */
- public FileTypePredicate( final Collection<String> patterns ) {
- this( String.join( ",", patterns ) );
- }
-
- /**
- * Returns true if the file matches the patterns defined during construction.
- *
- * @param file The filename to match against the given glob patterns.
- *
- * @return false The filename does not match the glob patterns.
- */
- @Override
- public boolean test( final File file ) {
- return getMatcher().matches( file.toPath() );
- }
-
- private PathMatcher getMatcher() {
- return mMatcher;
- }
-}
src/main/java/com/scrivenvar/predicates/strings/ContainsPredicate.java
-/*
- * Copyright 2020 White Magic Software, Ltd.
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.scrivenvar.predicates.strings;
-
-/**
- * Determines if one string contains another.
- */
-public class ContainsPredicate extends StringPredicate {
-
- /**
- * Calls the superclass to construct the instance.
- *
- * @param comparate Not null.
- */
- public ContainsPredicate( final String comparate ) {
- super( comparate );
- }
-
- /**
- * Answers whether the given strings match each other. What match means will
- * depend on user preferences. The empty condition is required to return the
- * first node in a list of child nodes when the user has not yet selected a
- * node.
- *
- * @param comparator The string to compare against the comparate.
- *
- * @return true if s1 and s2 are a match according to some criteria,or s2 is
- * empty.
- */
- @Override
- public boolean test( final String comparator ) {
- final String comparate = getComparate().toLowerCase();
- return comparator.contains( comparate.toLowerCase() )
- || comparate.isEmpty();
- }
-}
src/main/java/com/scrivenvar/predicates/strings/StartsPredicate.java
-/*
- * Copyright 2020 White Magic Software, Ltd.
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.scrivenvar.predicates.strings;
-
-/**
- * Determines if a string starts with another.
- */
-public class StartsPredicate extends StringPredicate {
-
- /**
- * Constructs a new instance using a comparate that will be compared with
- * the comparator during the test.
- *
- * @param comparate The string to compare against the comparator.
- */
- public StartsPredicate( final String comparate ) {
- super( comparate );
- }
-
- /**
- * Compares two strings.
- *
- * @param comparator A non-null string, possibly empty.
- *
- * @return true The comparator starts with the comparate, ignoring case.
- */
- @Override
- public boolean test( final String comparator ) {
- final String comparate = getComparate().toLowerCase();
- return comparator.startsWith( comparate.toLowerCase() );
- }
-}
src/main/java/com/scrivenvar/predicates/strings/StringPredicate.java
-/*
- * Copyright 2020 White Magic Software, Ltd.
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.scrivenvar.predicates.strings;
-
-import java.util.function.Predicate;
-
-/**
- * General predicate for different types of string comparisons.
- */
-public abstract class StringPredicate implements Predicate<String> {
-
- private final String comparate;
-
- public StringPredicate( final String comparate ) {
- this.comparate = comparate;
- }
-
- protected String getComparate() {
- return this.comparate;
- }
-}
Delta111 lines added, 257 lines removed, 146-line decrease