001package org.gwtbootstrap3.client.ui.form.validator;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2015 GwtBootstrap3
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.gwtbootstrap3.client.ui.form.validator.ValidationMessages.Keys;
024
025import com.google.gwt.user.client.ui.HasValue;
026
027/**
028 * Validator for matching with another field.
029 *
030 * @param <T> the generic type
031 * @author Steven Jardine
032 */
033public class FieldMatchValidator<T> extends AbstractValidator<T> {
034
035    private final HasValue<T> verifyField;
036
037    /**
038     * Constructor.
039     *
040     * @param verifyField the verify field
041     */
042    public FieldMatchValidator(final HasValue<T> verifyField) {
043        this(verifyField, new Object[0]);
044    }
045
046    /**
047     * Constructor.
048     *
049     * @param verifyField the verify field
050     * @param invalidMessageArgs the invalid message args
051     */
052    public FieldMatchValidator(final HasValue<T> verifyField, final Object... invalidMessageArgs) {
053        super(Keys.FIELD_MATCH, invalidMessageArgs);
054        this.verifyField = verifyField;
055        assert this.verifyField != null;
056    }
057
058    /**
059     * Constructor.
060     *
061     * @param verifyField the field to verify matches with this one.
062     * @param invalidMessageOverride the invalid message override
063     */
064    public FieldMatchValidator(final HasValue<T> verifyField, final String invalidMessageOverride) {
065        super(invalidMessageOverride);
066        this.verifyField = verifyField;
067        assert this.verifyField != null;
068    }
069
070    /** {@inheritDoc} */
071    @Override
072    public int getPriority() {
073        return Priority.HIGH;
074    }
075
076    /** {@inheritDoc} */
077    @Override
078    public boolean isValid(T value) {
079        T verifyValue = verifyField.getValue();
080        return value == null ? verifyValue == null : value.equals(verifyValue);
081    }
082
083}