Class AbstractPatternFilter<T>

  • All Implemented Interfaces:
    Filter<T>
    Direct Known Subclasses:
    PatternFileFilter, PatternURLFilter

    public abstract class AbstractPatternFilter<T>
    extends AbstractFilter<T>

    AbstractFilter implementation containing a Java Pattern which should be used to determine if candidate T objects match any of the supplied regularExpressions. Since Java regexp Patterns only match strings, a pluggable StringConverter is used to convert T-type objects to strings for the actual matching.

    The structure of setter methods is provided to enable simpler configuration using the default Maven/Plexus dependency injection mechanism. The internal state of each AbstractPatternFilter is not intended to be changed after its creation.

    Since:
    2.0
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List<java.util.regex.Pattern> convert​(java.util.List<java.lang.String> patternStrings, java.lang.String prepend)
      Collects a List containing java.text.Pattern objects by concatenating prepend + current_pattern_string and Pattern-compiling the result.
      protected java.lang.String convert​(T nonNullT)
      Method implementation which converts a non-null T object to a String to be matched by the Java Regexp Pattern contained within this AbstractPatternFilter implementation.
      protected boolean onCandidate​(T nonNullCandidate)
      Each nonNullCandidate is matched against all Patterns supplied to this AbstractPatternFilter.
      protected void onInitialize()
      Compiles the List of Patterns used by this AbstractPatternFilter to match candidates.
      void setAcceptCandidateOnPatternMatch​(boolean acceptCandidateOnPatternMatch)
      Assigns the acceptCandidateOnPatternMatch parameter which defines the function of this AbstractPatternFilter's accept method.
      void setConverter​(StringConverter<T> converter)
      Assigns the StringConverter used to convert T-type objects to Strings.
      void setPatternPrefix​(java.lang.String patternPrefix)
      Assigns a prefix to be prepended to any patterns submitted to this AbstractPatternFilter.
      void setPatterns​(java.util.List<java.lang.String> patterns)
      Injects a List of patterns complying with the Java Regexp Pattern specification.
      java.lang.String toString()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • acceptCandidateOnPatternMatch

        private boolean acceptCandidateOnPatternMatch
      • regularExpressions

        private java.util.List<java.util.regex.Pattern> regularExpressions
      • patternPrefix

        private java.lang.String patternPrefix
      • patterns

        private java.util.List<java.lang.String> patterns
    • Constructor Detail

      • AbstractPatternFilter

        public AbstractPatternFilter()
        DI-friendly constructor.
    • Method Detail

      • setPatternPrefix

        public final void setPatternPrefix​(java.lang.String patternPrefix)
        Assigns a prefix to be prepended to any patterns submitted to this AbstractPatternFilter.
        Parameters:
        patternPrefix - A prefix to be prepended to each pattern to render a Pattern. If a null argument is supplied, nothing will be prepended.
        See Also:
        convert(java.util.List, String)
      • setPatterns

        public void setPatterns​(java.util.List<java.lang.String> patterns)
        Injects a List of patterns complying with the Java Regexp Pattern specification. Each injected pattern will be compiled to a Pattern by concatenating the patternPrefix string, given at construction time, to obtain the full regexp pattern. The resulting Patterns are matched for candidates normally in the onCandidate(Object) method.
        Parameters:
        patterns - The List of PatternStrings to compile.
        See Also:
        convert(java.util.List, String), setPatternPrefix(String)
      • setAcceptCandidateOnPatternMatch

        public final void setAcceptCandidateOnPatternMatch​(boolean acceptCandidateOnPatternMatch)
        Assigns the acceptCandidateOnPatternMatch parameter which defines the function of this AbstractPatternFilter's accept method.
        Parameters:
        acceptCandidateOnPatternMatch - if true, this AbstractPatternFilter will matchAtLeastOnce candidate objects that match at least one of the supplied regularExpressions. if false, this AbstractPatternFilter will noFilterMatches candidates that match at least one of the supplied regularExpressions.
      • setConverter

        public void setConverter​(StringConverter<T> converter)
        Assigns the StringConverter used to convert T-type objects to Strings. This StringConverter is used to acquire input comparison values for all Patterns to T-object candidates.
        Parameters:
        converter - The StringConverter used to convert T-type objects to Strings which should be matched by all supplied Patterns to T-object candidates.
      • onInitialize

        protected void onInitialize()
        Compiles the List of Patterns used by this AbstractPatternFilter to match candidates. If no patterns are supplied (by configuration or constructor call), no regularExpressions Pattern List will be created for use by this AbstractPatternFilter. Instead, some logging is emitted onto the console.
        Overrides:
        onInitialize in class AbstractFilter<T>
        See Also:
        patterns
      • onCandidate

        protected boolean onCandidate​(T nonNullCandidate)

        Each nonNullCandidate is matched against all Patterns supplied to this AbstractPatternFilter. The match table of this AbstractPatternFilter on each candidate is as follows:

        Truth table for the onCandidate method
        at least 1 filter matches acceptCandidateOnPatternMatch result
        true true true
        false true false
        true false false
        false false true

        Method that is invoked to determine if a candidate instance should be accepted or not.

        Specified by:
        onCandidate in class AbstractFilter<T>
        Parameters:
        nonNullCandidate - The candidate that should be tested for acceptance by this Filter. Never null.
        Returns:
        true if the candidate is accepted by this Filter and false otherwise.
      • convert

        protected java.lang.String convert​(T nonNullT)

        Method implementation which converts a non-null T object to a String to be matched by the Java Regexp Pattern contained within this AbstractPatternFilter implementation. Override for a non-standard conversion.

        Parameters:
        nonNullT - A non-null T object.
        Returns:
        A string to be converted.
      • convert

        public static java.util.List<java.util.regex.Pattern> convert​(java.util.List<java.lang.String> patternStrings,
                                                                      java.lang.String prepend)
        Collects a List containing java.text.Pattern objects by concatenating prepend + current_pattern_string and Pattern-compiling the result.
        Parameters:
        patternStrings - The List of PatternStrings to compile.
        prepend - A string to prepend each pattern. If a null argument is supplied, nothing will be prepended.
        Returns:
        a List containing java.text.Pattern objects by concatenating prepend + current_pattern_string and Pattern-compiling the result.