Class AbstractPatternFilter<T>

java.lang.Object
org.codehaus.mojo.jaxb2.shared.filters.AbstractFilter<T>
org.codehaus.mojo.jaxb2.shared.filters.pattern.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
  • Field Details

    • acceptCandidateOnPatternMatch

      private boolean acceptCandidateOnPatternMatch
    • converter

      private StringConverter<T> converter
    • regularExpressions

      private List<Pattern> regularExpressions
    • patternPrefix

      private String patternPrefix
    • patterns

      private List<String> patterns
  • Constructor Details

    • AbstractPatternFilter

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

    • setPatternPrefix

      public final void setPatternPrefix(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:
    • setPatterns

      public void setPatterns(List<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:
    • 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:
    • 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 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 List<Pattern> convert(List<String> patternStrings, 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.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractFilter<T>