/*
* Copyright 2000-2013 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.validator;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import com.vaadin.data.Validator;
/**
* The
* The value is valid, if:
* CompositeValidator
allows you to chain (compose) many
* validators to validate one field. The contained validators may be required to
* all validate the value to validate or it may be enough that one contained
* validator validates the value. This behaviour is controlled by the modes
* AND
and OR
.
*
* @author Vaadin Ltd.
* @since 3.0
*/
@SuppressWarnings("serial")
public class CompositeValidator implements Validator {
public enum CombinationMode {
/**
* The validators are combined with AND
clause: validity of
* the composite implies validity of the all validators it is composed
* of must be valid.
*/
AND,
/**
* The validators are combined with OR
clause: validity of
* the composite implies that some of validators it is composed of must
* be valid.
*/
OR;
}
/**
* @deprecated As of 7.0, use {@link CombinationMode#AND} instead
*/
@Deprecated
public static final CombinationMode MODE_AND = CombinationMode.AND;
/**
* @deprecated As of 7.0, use {@link CombinationMode#OR} instead
*/
@Deprecated
public static final CombinationMode MODE_OR = CombinationMode.OR;
private String errorMessage;
/**
* Operation mode.
*/
private CombinationMode mode = CombinationMode.AND;
/**
* List of contained validators.
*/
private final ListAND
mode without error
* message.
*/
public CompositeValidator() {
this(CombinationMode.AND, "");
}
/**
* Constructs a composite validator in given mode.
*
* @param mode
* @param errorMessage
*/
public CompositeValidator(CombinationMode mode, String errorMessage) {
setErrorMessage(errorMessage);
setMode(mode);
}
/**
* Validates the given value.
*
*
*
* If the value is invalid, validation error is thrown. If the error message
* is set (non-null), it is used. If the error message has not been set, the
* first error occurred is thrown.
* MODE_AND
: All of the sub-validators are valid
* MODE_OR
: Any of the sub-validators are valid
*
* If the component contains directly or recursively (it contains another
* composite containing the validator) validators compatible with given type
* they are returned. This only applies to AND
mode composite
* validators.
*
* If the validator is in OR
mode or does not contain any
* validators of given type null is returned.
*