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