/* * Copyright 2000-2016 Vaadin Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.vaadin.data; import java.io.Serializable; import java.util.Objects; import java.util.Optional; import com.vaadin.data.Binder.Binding; import com.vaadin.data.Binder.BindingBuilder; /** * Represents the status of field validation. Status can be {@code Status.OK}, * {@code Status.ERROR} or {@code Status.RESET}. Status OK and ERROR are always * associated with a ValidationResult {@link #getResult}. *
* Use
* {@link BindingBuilder#withValidationStatusHandler(ValidationStatusHandler)}
* to register a handler for field level validation status changes.
*
* @author Vaadin Ltd
*
* @param
* The status is the part of {@link ValidationStatus} which indicates
* whether the validation failed or not, or whether it is in unresolved
* state (e.g. after clear or reset).
*/
public enum Status {
/** Validation passed. */
OK,
/** Validation failed. */
ERROR,
/**
* Unresolved status, e.g field has not yet been validated because value
* was cleared.
*
* In practice this status means that the value might be invalid, but
* validation errors should be hidden.
*/
UNRESOLVED;
}
private final Status status;
private final ValidationResult result;
private final Binding, TARGET> binding;
/**
* Convenience method for creating a {@link Status#UNRESOLVED} validation
* status for the given binding.
*
* @param source
* the source binding
* @return unresolved validation status
* @param
* The {@code message} must be {@code null} if the {@code status} is
* {@link Status#OK}.
*
* @param source
* field whose status has changed, not {@code null}
* @param status
* updated status value, not {@code null}
* @param result
* the related result, may be {@code null}
*/
public ValidationStatus(Binding, TARGET> source, Status status,
ValidationResult result) {
Objects.requireNonNull(source, "Event source may not be null");
Objects.requireNonNull(status, "Status may not be null");
if (Objects.equals(status, Status.OK) && result.isError()
|| Objects.equals(status, Status.ERROR) && !result.isError()
|| Objects.equals(status, Status.UNRESOLVED)
&& result != null) {
throw new IllegalStateException(
"Invalid validation status " + status + " for given result "
+ (result == null ? "null" : result.toString()));
}
binding = source;
this.status = status;
this.result = result;
}
/**
* Gets status of the validation.
*
* @return status
*/
public Status getStatus() {
return status;
}
/**
* Gets whether the validation failed or not.
*
* @return {@code true} if validation failed, {@code false} if validation
* passed
*/
public boolean isError() {
return status == Status.ERROR;
}
/**
* Gets error validation message if status is {@link Status#ERROR}.
*
* @return an optional validation error status or an empty optional if
* status is not an error
*/
public Optional