Class FilterBuilder

  • Direct Known Subclasses:
    MatchingRuleAssertionFilterBuilder

    public class FilterBuilder
    extends Object
    A builder for constructing well formed search filters according to RFC 4515. This builder is most convenient when you use static imports. For example:
     import static org.apache.directory.ldap.client.api.search.FilterBuilder.and;
     import static org.apache.directory.ldap.client.api.search.FilterBuilder.equal;
     import static org.apache.directory.ldap.client.api.search.FilterBuilder.or;
     
     ...
     
             String filter = 
                     or(
                         and( 
                             equal( "givenName", "kermit" ), 
                             equal( "sn", "the frog" ) ),
                         and( 
                             equal( "givenName", "miss" ), 
                             equal( "sn", "piggy" ) ) )
                     .toString()
     
    Author:
    Apache Directory Project
    • Method Detail

      • and

        public static FilterBuilder and​(FilterBuilder... filters)
        Returns a new FilterBuilder that will & together all of the supplied filters. For example:
         and( equal( "givenName", "kermit" ), equal( "sn", "the frog" ) ).toString()
         
        would result in the string:
         (&(givenName=kermit)(sn=the frog))
         
        Which would match all entries with a given name of kermit and a surname the frog.
        Parameters:
        filters - The filters to and together
        Returns:
        A new FilterBuilder
      • approximatelyEqual

        public static FilterBuilder approximatelyEqual​(String attribute,
                                                       String value)
        Returns a new FilterBuilder for testing the approximate equality of an attribute. For example:
         approximatelyEqual( "l", "san fransico" ).toString();
         
        would result in the string:
         (l~=san fransico)
         
        Which MIGHT match results whose locality is San Francisco. The matching rule used to apply this filter is dependent on the server implementation.
        Parameters:
        attribute - The attribute
        value - The value
        Returns:
        A new FilterBuilder
      • equal

        public static FilterBuilder equal​(String attribute,
                                          String value)
        Returns a new FilterBuilder for testing equality of an attribute. For example:
         equal( "cn", "Kermit The Frog" ).toString();
         
        would result in the string:
         (cn>=Kermit The Frog)
         
        Which would match entries with the common name Kermit The Frog.
        Parameters:
        attribute - The attribute
        value - The value
        Returns:
        A new FilterBuilder
      • extensible

        public static MatchingRuleAssertionFilterBuilder extensible​(String attribute,
                                                                    String value)
        Creates an extensible match filter. This filter can be used to specify that dn attributes should be included in the match, which matcher to use, or that all attributes that support a specific matcher will be checked. For example:
         extensible( "sn", "Barney Rubble" )
             .useDnAttributes()
             .setMatchingRule( "2.4.6.8.10" )
             .toString();
         
        would result in the string:
         (sn:dn:2.4.6.8.10:=Barney Rubble)
         
        Not that the specialized filter builder that is returned IS a FilterBuilder so it can be chained with other filters. For example:
         and(
             extensible( "sn", "Rubble" )
                 .useDnAttributes()
                 .setMatchingRule( "2.4.6.8.10" ),
             equal( "givenName", "Barney" ) )
             .toString();
         
        Parameters:
        attribute - The attribute to test
        value - The value to test for
        Returns:
        A new MatchingRuleAssertionFilterBuilder
      • greaterThanOrEqual

        public static FilterBuilder greaterThanOrEqual​(String attribute,
                                                       String value)
        Returns a new FilterBuilder for testing lexicographical greater than. For example:
         greaterThanOrEqual( "sn", "n" ).toString();
         
        would result in the string:
         (sn>=n)
         
        which would match results whose surname starts with the second half of the alphabet.
        Parameters:
        attribute - The attribute
        value - The value
        Returns:
        A new FilterBuilder
      • lessThanOrEqual

        public static FilterBuilder lessThanOrEqual​(String attribute,
                                                    String value)
        Returns a new FilterBuilder for testing lexicographical less than. For example:
         lessThanOrEqual( "sn", "mzzzzzz" ).toString();
         
        would result in the string:
         (sn<=mzzzzzz)
         
        which would match results whose surname starts with the first half of the alphabet. Note, this is not perfect, but if you know anybody with a last name that starts with an m followed by six z's...
        Parameters:
        attribute - The attribute
        value - The value
        Returns:
        A new FilterBuilder
      • not

        public static FilterBuilder not​(FilterBuilder builder)
        Returns a new FilterBuilder for negating another filter. For example:
         not( present( "givenName" ) ).toString();
         
        would result in the string:
         (!(givenName=*))
         
        Parameters:
        builder - The filter to negate
        Returns:
        A new FilterBuilder
      • or

        public static FilterBuilder or​(FilterBuilder... builders)
        Returns a new FilterBuilder that will | together all of the supplied filters. For example:
         or( equal( "givenName", "kermit" ), equal( "givenName", "walter" ) ).toString()
         
        would result in the string:
         (|(givenName=kermit)(givenName=walter))
         
        Which would match any entry with the givenName of either kermit or walter.
        Parameters:
        builders - The filters to or together
        Returns:
        A new FilterBuilder
      • present

        public static FilterBuilder present​(String attribute)
        Returns a new FilterBuilder for testing the presence of an attributes. For example:
         present( "givenName" ).toString();
         
        would result in the string:
         (givenName=*)
         
        Which would match any entry that has a givenName attribute.
        Parameters:
        attribute - The attribute to test the presence of
        Returns:
        A new FilterBuilder
      • startsWith

        public static FilterBuilder startsWith​(String attribute,
                                               String... parts)
        Returns a new FilterBuilder that will construct a SubString filter, with an initial part, and zero to N any part, but no final part. For instance:
         startswith( "sn", "Th", "Soft", "Foun" )).toString()
         
        would result in the string:
         (sn=Th*Soft*Foun*)
         
        Which would match any entry with the sn starting with 'Th', and having a Soft and Foun strings in the middle, like 'The Apache Software Foundation'.
        Parameters:
        attribute - The attribute to use in the filter
        parts - The sub elements to use in the filter
        Returns:
        A new FilterBuilder
      • endsWith

        public static FilterBuilder endsWith​(String attribute,
                                             String... parts)
        Returns a new FilterBuilder that will construct a SubString filter, with an initial part, and zero to N any parts, but no final part. For instance:
         startswith( "sn", "Th", "Soft", "Foun" ).toString()
         
        would result in the string:
         (sn=Th*Soft*Foun*)
         
        Which would match any entry with the sn starting with 'Th', and having a Soft and Foun strings in the middle, like 'The Apache Software Foundation'.
        Parameters:
        attribute - The attribute to use in the filter
        parts - The sub elements to use in the filter
        Returns:
        A new FilterBuilder
      • contains

        public static FilterBuilder contains​(String attribute,
                                             String... parts)
        Returns a new FilterBuilder that will construct a SubString filter, with zero to N any parts, but no initial or final parts. For instance:
         contains( "sn", "Soft", "Foun" )).toString()
         
        would result in the string:
         (sn=*Soft*Foun*)
         
        Which would match any entry with the sn having a Soft and Foun strings in the middle, like 'The Apache Software Foundation'.
        Parameters:
        attribute - The attribute to use in the filter
        parts - The sub elements to use in the filter
        Returns:
        A new FilterBuilder
      • substring

        public static FilterBuilder substring​(String attribute,
                                              String... parts)
        Returns a new FilterBuilder that will construct a SubString filter, with a initial part, zero to N any parts, and a final part. For instance:
         substring( "sn", "The", "Soft", "Foun", "ion" )).toString()
         
        would result in the string:
         (sn=The*Soft*Foun*ion)
         
        Which would match any entry with the sn having a Soft and Foun strings in the middle, starts with The and ends with ion like 'The Apache Software Foundation'.

        Note that if we have only two strings in the parts, they will be the initial and final ones :

         substring( "sn", "The", "ion" )).toString()
         
        would result in the string:
         (sn=The*ion)
         
        Parameters:
        attribute - The attribute to use in the filter
        parts - The sub elements to use in the filter
        Returns:
        A new FilterBuilder
      • toString

        public String toString()
        Returns the string version of the filter represented by this FilterBuilder.
        Overrides:
        toString in class Object
        Returns:
        The string representation of the filter