Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@
import java.io.Serializable;

/**
* Something that matches credentials. Best practice is to
* Something that matches credentials. Best practice is to use Java records,
* which automatically provide
* {@code equals()}, {@code hashCode()}, and {@code toString()} implementations.
* When using records, ensure
* the canonical constructor maintains binary compatibility with existing code
* that may instantiate matchers directly.
* <p>
* Implementations should define a {@code serialVersionUID} field to ensure consistent serialization.
* <p>
* For legacy implementations using classes instead of records:
* <ul>
* <li>Implement {@link Object#toString()}</li>
* <li>Implement {@link Object#equals(Object)} and {@link Object#hashCode()}</li>
* <li>Define a {@code serialVersionUID} field to ensure consistent serialization</li>
* <li>Implement {@link Object#equals(Object)} and
* {@link Object#hashCode()}</li>
* </ul>
*
* @since 1.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsMatcher;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* Matches all of the supplied matchers.
*
* @since 1.5
*/
public class AllOfMatcher implements CredentialsMatcher {
public record AllOfMatcher(@NonNull List<CredentialsMatcher> matchers) implements CredentialsMatcher {
/**
* Standardize serialization.
*
Expand All @@ -45,59 +45,18 @@ public class AllOfMatcher implements CredentialsMatcher {
private static final long serialVersionUID = 2161005681083022432L;

/**
* The matchers to match.
* Compact constructor that defensively copies and makes the list unmodifiable.
*/
@NonNull
private final List<CredentialsMatcher> matchers;

/**
* Creates a new instance.
*
* @param matchers the matchers to match.
*/
public AllOfMatcher(@CheckForNull List<CredentialsMatcher> matchers) {
this.matchers = new ArrayList<>(matchers == null ? Collections.emptyList() : matchers);
public AllOfMatcher {
Objects.requireNonNull(matchers);
matchers = Collections.unmodifiableList(new ArrayList<>(matchers));
}

/**
* {@inheritDoc}
*/
@Override
public boolean matches(@NonNull Credentials item) {
return matchers.stream().allMatch(matcher -> matcher.matches(item));
}

/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return matchers.hashCode();
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

AllOfMatcher that = (AllOfMatcher) o;

return matchers.equals(that.matchers);

}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "AllMatcher{" + "matchers=" + matchers +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,79 +25,38 @@

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsMatcher;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* Matches any of the supplied matchers.
*
* @since 1.5
*/
public class AnyOfMatcher implements CredentialsMatcher {
public record AnyOfMatcher(@NonNull List<CredentialsMatcher> matchers) implements CredentialsMatcher {
/**
* Standardize serialization.
*
* @since 2.1.0
*/
private static final long serialVersionUID = 8214348092732916263L;
/**
* The matchers to match.
*/
@NonNull
private final List<CredentialsMatcher> matchers;

/**
* Creates a new instance.
*
* @param matchers the matchers to match.
* Compact constructor that defensively copies and makes the list unmodifiable.
*/
public AnyOfMatcher(@CheckForNull List<CredentialsMatcher> matchers) {
this.matchers = new ArrayList<>(
matchers == null ? Collections.emptyList() : matchers);
public AnyOfMatcher {
Objects.requireNonNull(matchers);
matchers = Collections.unmodifiableList(new ArrayList<>(matchers));
}

/**
* {@inheritDoc}
*/
@Override
public boolean matches(@NonNull Credentials item) {
return matchers.stream().anyMatch(matcher -> matcher.matches(item));
}

/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return matchers.hashCode();
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

AnyOfMatcher that = (AnyOfMatcher) o;

return matchers.equals(that.matchers);

}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "AnyMatcher{" + "matchers=" + matchers +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,66 +32,19 @@
*
* @since 1.5
*/
public class ConstantMatcher implements CredentialsMatcher {
public record ConstantMatcher(boolean match) implements CredentialsMatcher {
/**
* Standardize serialization.
*
* @since 2.1.0
*/
private static final long serialVersionUID = 8270819649776908382L;
/**
* Whether to match.
*/
private final boolean match;

/**
* Constructs a new instance.
*
* @param match whether to match or not.
*/
public ConstantMatcher(boolean match) {
this.match = match;
}

/**
* {@inheritDoc}
*/
@Override
public boolean matches(@NonNull Credentials item) {
return match;
}

/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return (match ? 1 : 0);
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

ConstantMatcher that = (ConstantMatcher) o;

return match == that.match;

}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "ConstantMatcher{" + "match=" + match +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,72 +31,33 @@
import java.util.Objects;

/**
* Matches credentials that are {@link IdCredentials} and have the specified {@link IdCredentials#getId()}.
* Matches credentials that are {@link IdCredentials} and have the specified
* {@link IdCredentials#getId()}.
*
* @since 1.5
*/
public class IdMatcher implements CredentialsMatcher {
public record IdMatcher(@NonNull String id) implements CredentialsMatcher {
/**
* Standardize serialization.
*
* @since 2.1.0
*/
private static final long serialVersionUID = -2400504567993839126L;
/**
* The id to match.
*/
@NonNull
private final String id;

/**
* Constructs a new instance.
*
* @param id the id to match.
*/
public IdMatcher(@NonNull String id) {
public IdMatcher {
Objects.requireNonNull(id);
this.id = id;
}

/**
* {@inheritDoc}
*/
public boolean matches(@NonNull Credentials item) {
return item instanceof IdCredentials && id.equals(((IdCredentials) item).getId());
}

/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return id.hashCode();
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

IdMatcher idMatcher = (IdMatcher) o;

return id.equals(idMatcher.id);

}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "IdMatcher{" + "id='" + id + '\'' +
'}';
public boolean matches(@NonNull Credentials item) {
return item instanceof IdCredentials idc && id.equals(idc.getId());
}
}
Loading
Loading