001package gwt.material.design.client.base.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 com.google.gwt.user.client.ui.HasValue;
024import gwt.material.design.client.base.validator.ValidationMessages.Keys;
025
026/**
027 * Validator for matching with another field.
028 *
029 * @param <T> the generic type
030 * @author Steven Jardine
031 */
032public class FieldMatchValidator<T> extends AbstractValidator<T> {
033
034    private final HasValue<T> verifyField;
035
036    /**
037     * Constructor.
038     *
039     * @param verifyField the verify field
040     */
041    public FieldMatchValidator(final HasValue<T> verifyField) {
042        this(verifyField, new Object[0]);
043    }
044
045    /**
046     * Constructor.
047     *
048     * @param verifyField the verify field
049     * @param invalidMessageArgs the invalid message args
050     */
051    public FieldMatchValidator(final HasValue<T> verifyField, final Object... invalidMessageArgs) {
052        super(Keys.FIELD_MATCH, invalidMessageArgs);
053        this.verifyField = verifyField;
054        assert this.verifyField != null;
055    }
056
057    /**
058     * Constructor.
059     *
060     * @param verifyField the field to verify matches with this one.
061     * @param invalidMessageOverride the invalid message override
062     */
063    public FieldMatchValidator(final HasValue<T> verifyField, final String invalidMessageOverride) {
064        super(invalidMessageOverride);
065        this.verifyField = verifyField;
066        assert this.verifyField != null;
067    }
068
069    @Override
070    public int getPriority() {
071        return Priority.HIGH;
072    }
073
074    @Override
075    public boolean isValid(T value) {
076        T verifyValue = verifyField.getValue();
077        return value == null ? verifyValue == null : value.equals(verifyValue);
078    }
079
080}