All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
pl.ais.commons.bean.validation.constraint.SimpleConstraint Maven / Gradle / Ivy
package pl.ais.commons.bean.validation.constraint;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.Objects;
import java.util.function.Predicate;
/**
* Simple Constraint.
*
* @param the type of the values handled by the constraint
* @author Warlock, AIS.PL
* @since 1.2.1
*/
@Immutable
public final class SimpleConstraint extends AbstractConstraint, T> {
private final Predicate determinant;
private SimpleConstraint(@Nonnull final String name, @Nonnull final Predicate determinant, final boolean active,
@Nonnull final Object[] messageParameters, @Nullable final String message) {
super(name, active, messageParameters, message);
// Verify constructor requirements, ...
Objects.requireNonNull(name, "SimpleConstraint name is required.");
Objects.requireNonNull(determinant, "SimpleConstraint determinant is required.");
Objects.requireNonNull(messageParameters, "Invalid message parameters provided");
// ... and initialize this instance fields.
this.determinant = determinant;
}
/**
* Constructs new instance.
*
* @param name name of the constraint
* @param determinant predicate being determinant of the constraint
*/
public SimpleConstraint(@Nonnull final String name, @Nonnull final Predicate determinant) {
this(name, determinant, true, new Object[0], null);
}
/**
* Indicates whether some other object is "equal to" this one.
*/
@Override
public boolean equals(final Object object) {
boolean result = (this == object);
if (!result && (object instanceof SimpleConstraint)) {
final SimpleConstraint other = (SimpleConstraint) object;
result = Objects.equals(name, other.name) && Objects.equals(determinant, other.determinant);
}
return result;
}
/**
* @return a hash code value for this constraint
*/
@Override
public int hashCode() {
return Objects.hash(name, determinant);
}
/**
* {@inheritDoc}
*/
@Override
public boolean test(final T candidate) {
return !active || determinant.test(candidate);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return new StringBuilder().append("Constraint '")
.append(determinant)
.append('\'')
.toString();
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("hiding")
@Nonnull
public SimpleConstraint when(final boolean active) {
return new SimpleConstraint<>(name, determinant, active, messageParameters, message);
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("hiding")
@Nonnull
public SimpleConstraint withDescription(final String message, final Object... messageParameters) {
return new SimpleConstraint<>(name, determinant, active, messageParameters, message);
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("hiding")
@Nonnull
public SimpleConstraint withMessageParameters(final Object... messageParameters) {
return new SimpleConstraint<>(name, determinant, active, messageParameters, message);
}
}