Package com.alibaba.citrus.service.form.impl.validation

Source Code of com.alibaba.citrus.service.form.impl.validation.NumberCompareValidator$DefinitionParser

/*
* Copyright 2010 Alibaba Group Holding Limited.
* All rights reserved.
*
* 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.alibaba.citrus.service.form.impl.validation;

import static com.alibaba.citrus.service.form.support.CompareOperator.*;
import static com.alibaba.citrus.util.Assert.*;
import static com.alibaba.citrus.util.CollectionUtil.*;
import static com.alibaba.citrus.util.StringUtil.*;

import com.alibaba.citrus.service.form.Field;
import com.alibaba.citrus.service.form.configuration.FieldConfig;
import com.alibaba.citrus.service.form.support.AbstractNumberValidator;
import com.alibaba.citrus.service.form.support.AbstractValidatorDefinitionParser;
import com.alibaba.citrus.service.form.support.CompareOperator;
import com.alibaba.citrus.service.form.support.NumberSupport;

/**
* ����һ��field�Ƚ����ֵ�validator��
*
* @author Michael Zhou
*/
public class NumberCompareValidator extends AbstractNumberValidator {
    private String fieldName;
    private CompareOperator op;

    /**
     * ȡ��Ҫ�Ƚϵ�field���ơ�
     */
    public String getFieldName() {
        return fieldName;
    }

    /**
     * ȡ�ñȽϲ��������͡�
     */
    public CompareOperator getOp() {
        return op;
    }

    /**
     * ���õ��ڲ�����
     */
    public void setEqualTo(String fieldName) {
        setFieldName(equalTo, fieldName);
    }

    /**
     * ���ò����ڲ�����
     */
    public void setNotEqualTo(String fieldName) {
        setFieldName(notEqualTo, fieldName);
    }

    /**
     * ����С�ڲ�����
     */
    public void setLessThan(String fieldName) {
        setFieldName(lessThan, fieldName);
    }

    /**
     * ���ô��ڲ�����
     */
    public void setGreaterThan(String fieldName) {
        setFieldName(greaterThan, fieldName);
    }

    /**
     * ����С�ڵ��ڲ�����
     */
    public void setLessThanOrEqualTo(String fieldName) {
        setFieldName(lessThanOrEqualTo, fieldName);
    }

    /**
     * ���ô��ڵ��ڲ�����
     */
    public void setGreaterThanOrEqualTo(String fieldName) {
        setFieldName(greaterThanOrEqualTo, fieldName);
    }

    private void setFieldName(CompareOperator op, String fieldName) {
        this.op = op;
        this.fieldName = trimToNull(fieldName);
    }

    /**
     * ��֤��������ʼ����
     * <p>
     * �˳�ʼ����������ȡ��ͬ���е�����fieldConfig��
     * </p>
     */
    @Override
    public void init(FieldConfig fieldConfig) throws Exception {
        super.init(fieldConfig);

        if (fieldName == null || op == null) {
            throw new IllegalArgumentException("One of the following attributes should be set: "
                    + asList(CompareOperator.values()));
        }

        assertNotNull(fieldConfig.getGroupConfig().getFieldConfig(fieldName), "Field %s not exists", fieldName);
    }

    /**
     * ��֤һ���ֶΡ�
     */
    @Override
    protected boolean validate(Context context, String value) {
        Field fieldToCompare = assertNotNull(context.getField(fieldName), "field not found");
        NumberSupport thisValue = new NumberSupport(getNumberType(), value);
        NumberSupport otherValue = new NumberSupport(getNumberType(), fieldToCompare.getStringValue());

        try {
            return getOp().accept(thisValue.compareTo(otherValue));
        } catch (IllegalArgumentException e) {
            return false; // ����NumberFormatException�����getValueʧ�ܣ���֤ʧ�ܡ�
        }
    }

    public static class DefinitionParser extends AbstractValidatorDefinitionParser<NumberCompareValidator> {
    }
}
TOP

Related Classes of com.alibaba.citrus.service.form.impl.validation.NumberCompareValidator$DefinitionParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.