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
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    @Override
055    public int compareTo(ValidatorWrapper<T> other) {
056        if (this == other || getName().equals(other.getName())) { return 0; }
057        int result = getPriority().compareTo(other.getPriority());
058        if (result == 0) {
059            result = getInsertionOrder().compareTo(other.getInsertionOrder());
060        }
061        return result;
062    }
063
064    @Override
065    public boolean equals(Object obj) {
066        if (this == obj) return true;
067        if (obj == null) return false;
068        if (getClass() != obj.getClass()) return false;
069        ValidatorWrapper<?> other = (ValidatorWrapper<?>) obj;
070        if (name == null) {
071            if (other.name != null) return false;
072        } else if (!name.equals(other.name)) return false;
073        return true;
074    }
075
076    /**
077     * @return the insertionOrder
078     */
079    public Integer getInsertionOrder() {
080        return insertionOrder;
081    }
082
083    /**
084     * @return the name
085     */
086    public String getName() {
087        return name;
088    }
089
090    /**
091     * @return the priority
092     */
093    public Integer getPriority() {
094        return priority;
095    }
096
097    /**
098     * @return the validator
099     */
100    public Validator<T> getValidator() {
101        return validator;
102    }
103
104    @Override
105    public int hashCode() {
106        final int prime = 31;
107        int result = 1;
108        result = prime * result + ((name == null) ? 0 : name.hashCode());
109        return result;
110    }
111}