package net.sf.oval.constraint;

import net.sf.oval.Validator;
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
import net.sf.oval.context.OValContext;
import net.sf.oval.exception.OValException;

/**
 * Проверка контрактной спецификации, описывающей допустимые длины строки
 */
public class PossibleLengthCheck extends
		AbstractAnnotationCheck<PossibleLength> {
	private static final long serialVersionUID = 8624265267273032665L;

	private int[] lengths;

	@Override
	public void configure(final PossibleLength constraintAnnotation) {
		super.configure(constraintAnnotation);
		lengths = constraintAnnotation.lengths();
		message = constraintAnnotation.message();
	}

	public boolean isSatisfied(final Object validatedObject,
			final Object value, final OValContext context,
			final Validator validator) throws OValException {
		if (value == null)
			return true;
		for (final int length : lengths)
			if (length == value.toString().length())
				return true;
		return false;
	}
}
