Class CredentialsMatchers


  • public class CredentialsMatchers
    extends Object
    Some standard matchers and filtering utility methods.
    Since:
    1.5
    • Method Detail

      • always

        @NonNull
        public static CredentialsMatcher always()
        Creates a matcher that always matches.
        Returns:
        a matcher that always matches.
      • never

        @NonNull
        public static CredentialsMatcher never()
        Creates a matcher that never matches.
        Returns:
        a matcher that never matches.
      • not

        @NonNull
        public static CredentialsMatcher not​(@NonNull
                                             CredentialsMatcher matcher)
        Creates a matcher that inverts the supplied matcher.
        Parameters:
        matcher - matcher to invert.
        Returns:
        a matcher that is the opposite of the supplied matcher.
      • instanceOf

        @NonNull
        public static CredentialsMatcher instanceOf​(@NonNull
                                                    Class clazz)
        Creates a matcher that matches credentials of the specified type.
        Parameters:
        clazz - the type of credential to match.
        Returns:
        a matcher that matches credentials of the specified type.
      • withProperty

        public static <T extends SerializableCredentialsMatcher withProperty​(@NonNull
                                                                               String name,
                                                                               @CheckForNull
                                                                               T expected)
        Creates a matcher that matches a named Java Bean property against the supplied expected value.
        Type Parameters:
        T - the type of expected value.
        Parameters:
        name - the name of the property to match.
        expected - the expected value of the property.
        Returns:
        a matcher that matches a named Java Bean property against the supplied expected value.
        Since:
        2.1.0
      • allOf

        @NonNull
        public static CredentialsMatcher allOf​(@NonNull
                                               CredentialsMatcher... matchers)
        Creates a matcher that matches when all of the supplied matchers match.
        Parameters:
        matchers - the matchers to match.
        Returns:
        a matcher that matches when all of the supplied matchers match.
      • anyOf

        @NonNull
        public static CredentialsMatcher anyOf​(@NonNull
                                               CredentialsMatcher... matchers)
        Creates a matcher that matches when any of the supplied matchers match.
        Parameters:
        matchers - the matchers to match.
        Returns:
        a matcher that matches when any of the supplied matchers match.
      • both

        @NonNull
        public static CredentialsMatcher both​(@NonNull
                                              CredentialsMatcher matcher1,
                                              @NonNull
                                              CredentialsMatcher matcher2)
        Creates a matcher that matches when both of the supplied matchers match.
        Parameters:
        matcher1 - the first matcher to match.
        matcher2 - the second matcher to match.
        Returns:
        a matcher that matches when both of the supplied matchers match.
      • either

        @NonNull
        public static CredentialsMatcher either​(@NonNull
                                                CredentialsMatcher matcher1,
                                                @NonNull
                                                CredentialsMatcher matcher2)
        Creates a matcher that matches when either of the supplied matchers match.
        Parameters:
        matcher1 - the first matcher to match.
        matcher2 - the second matcher to match.
        Returns:
        a matcher that matches when either of the supplied matchers match.
      • noneOf

        @NonNull
        public static CredentialsMatcher noneOf​(@NonNull
                                                CredentialsMatcher... matchers)
        Creates a matcher that matches when none of the supplied matchers match.
        Parameters:
        matchers - the matchers to match.
        Returns:
        a matcher that matches when none of the supplied matchers match.
      • filter

        @NonNull
        public static <C extends CredentialsCollection<C> filter​(@NonNull
                                                                   Collection<C> credentials,
                                                                   @NonNull
                                                                   CredentialsMatcher matcher)
        Filters credentials using the supplied matcher.
        Type Parameters:
        C - the type of credentials.
        Parameters:
        credentials - the credentials to filter.
        matcher - the matcher to match on.
        Returns:
        only those credentials that match the supplied matcher.
      • filter

        @NonNull
        public static <C extends CredentialsSet<C> filter​(@NonNull
                                                            Set<C> credentials,
                                                            @NonNull
                                                            CredentialsMatcher matcher)
        Filters credentials using the supplied matcher.
        Type Parameters:
        C - the type of credentials.
        Parameters:
        credentials - the credentials to filter.
        matcher - the matcher to match on.
        Returns:
        only those credentials that match the supplied matcher.
      • filter

        @NonNull
        public static <C extends CredentialsList<C> filter​(@NonNull
                                                             List<C> credentials,
                                                             @NonNull
                                                             CredentialsMatcher matcher)
        Filters credentials using the supplied matcher.
        Type Parameters:
        C - the type of credentials.
        Parameters:
        credentials - the credentials to filter.
        matcher - the matcher to match on.
        Returns:
        only those credentials that match the supplied matcher.
      • filter

        @NonNull
        public static <C extends CredentialsIterable<C> filter​(@NonNull
                                                                 Iterable<C> credentials,
                                                                 @NonNull
                                                                 CredentialsMatcher matcher)
        Filters credentials using the supplied matcher.
        Type Parameters:
        C - the type of credentials.
        Parameters:
        credentials - the credentials to filter.
        matcher - the matcher to match on.
        Returns:
        only those credentials that match the supplied matcher.
      • filterKeys

        @NonNull
        public static <C extends Credentials,​V> Map<C,​V> filterKeys​(@NonNull
                                                                                Map<C,​V> credentialMap,
                                                                                @NonNull
                                                                                CredentialsMatcher matcher)
        Filters a map keyed by credentials using the supplied matcher.
        Type Parameters:
        C - the type of credentials.
        V - the type of the map values.
        Parameters:
        credentialMap - the map keyed by credentials to filter.
        matcher - the matcher to match on.
        Returns:
        only those entries with keys that that match the supplied matcher.
      • filterValues

        @NonNull
        public static <C extends Credentials,​K> Map<K,​C> filterValues​(@NonNull
                                                                                  Map<K,​C> credentialMap,
                                                                                  @NonNull
                                                                                  CredentialsMatcher matcher)
        Filters a map based on credential values using the supplied matcher.
        Type Parameters:
        K - the type of the map keys.
        C - the type of credentials.
        Parameters:
        credentialMap - the map with credentials values to filter.
        matcher - the matcher to match on.
        Returns:
        only those entries with keys that that match the supplied matcher.
      • firstOrDefault

        @CheckForNull
        public static <C extends Credentials> C firstOrDefault​(@NonNull
                                                               Iterable<C> credentials,
                                                               @NonNull
                                                               CredentialsMatcher matcher,
                                                               @CheckForNull
                                                               C defaultIfNone)
        Returns the first credential from a collection that matches the supplied matcher or if none match then the specified default.
        Type Parameters:
        C - the type of credential.
        Parameters:
        credentials - the credentials to select from.
        matcher - the matcher.
        defaultIfNone - the default value if no match found.
        Returns:
        a matching credential or the supplied default.
      • firstOrNull

        @CheckForNull
        public static <C extends Credentials> C firstOrNull​(@NonNull
                                                            Iterable<C> credentials,
                                                            @NonNull
                                                            CredentialsMatcher matcher)
        Returns the first credential from a collection that matches the supplied matcher or null if none match.
        Type Parameters:
        C - the type of credential.
        Parameters:
        credentials - the credentials to select from.
        matcher - the matcher.
        Returns:
        a matching credential or the supplied default.
      • describe

        @CheckForNull
        public static String describe​(CredentialsMatcher matcher)
        Attempts to describe the supplied CredentialsMatcher in terms of a Credentials Query Language. The basic form of the query language should follow Java expression syntax assuming that there is one variable in scope, namely the credential. The Java Bean style properties will be exposed as variables in the context. Example: (instanceof com.cloudbees.plugins.credentials.common.UsernameCredentials) && !(username == "bob") will match all instances of UsernameCredentials with UsernameCredentials.getUsername() not equal to bob. See also CQLParser.
        Parameters:
        matcher - the CredentialsMatcher to describe.
        Returns:
        the CQL description or null if the CredentialsMatcher cannot be mapped to CQL.
        Since:
        2.1.0
      • parse

        @NonNull
        public static CredentialsMatcher parse​(String cql)
        Attempts to parse a Credentials Query Language expression and construct the corresponding matcher.
        Parameters:
        cql - the Credentials Query Language expression to parse.
        Returns:
        a CredentialsMatcher for this expression.
        Throws:
        CQLSyntaxException - if the expression could not be parsed.
        Since:
        2.1.0