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