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
023/**
024 * Wraps a validator in order to provide sorting capability.
025 * 
026 * We sort based on priority first, then insertion order. The hashCode and equals function should prevent a
027 * set from containing 2 validators of the same type.
028 * 
029 * @author Steven Jardine
030 */
031public class ValidatorWrapper<T> implements Comparable<ValidatorWrapper<T>> {
032
033    private final Integer insertionOrder;
034
035    private final String name;
036
037    private final Integer priority;
038
039    private final Validator<T> validator;
040
041    /**
042     * Constructor.
043     *
044     * @param validator the validator
045     * @param insertionOrder the insertion order
046     */
047    public ValidatorWrapper(Validator<T> validator, int insertionOrder) {
048        this.validator = validator;
049        this.insertionOrder = insertionOrder;
050        this.name = validator.getClass().getName();
051        this.priority = validator.getPriority();
052    }
053
054    /** {@inheritDoc} */
055    @Override
056    public int compareTo(ValidatorWrapper<T> other) {
057        if (this == other || getName().equals(other.getName())) { return 0; }
058        int result = getPriority().compareTo(other.getPriority());
059        if (result == 0) {
060            result = getInsertionOrder().compareTo(other.getInsertionOrder());
061        }
062        return result;
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public boolean equals(Object obj) {
068        if (this == obj) return true;
069        if (obj == null) return false;
070        if (getClass() != obj.getClass()) return false;
071        ValidatorWrapper<?> other = (ValidatorWrapper<?>) obj;
072        if (name == null) {
073            if (other.name != null) return false;
074        } else if (!name.equals(other.name)) return false;
075        return true;
076    }
077
078    /**
079     * @return the insertionOrder
080     */
081    public Integer getInsertionOrder() {
082        return insertionOrder;
083    }
084
085    /**
086     * @return the name
087     */
088    public String getName() {
089        return name;
090    }
091
092    /**
093     * @return the priority
094     */
095    public Integer getPriority() {
096        return priority;
097    }
098
099    /**
100     * @return the validator
101     */
102    public Validator<T> getValidator() {
103        return validator;
104    }
105
106    /** {@inheritDoc} */
107    @Override
108    public int hashCode() {
109        final int prime = 31;
110        int result = 1;
111        result = prime * result + ((name == null) ? 0 : name.hashCode());
112        return result;
113    }
114
115}