001    package org.gwtbootstrap3.client.ui.form.validator;
002    
003    /*
004     * #%L
005     * GwtBootstrap3
006     * %%
007     * Copyright (C) 2013 - 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     */
031    public 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        public ValidatorWrapper(Validator<T> validator, int insertionOrder) {
042            this.validator = validator;
043            this.insertionOrder = insertionOrder;
044            this.name = validator.getClass().getName();
045            this.priority = validator.getPriority();
046        }
047    
048        @Override
049        public int compareTo(ValidatorWrapper<T> other) {
050            if (getName().equals(other.getName())) { return 0; }
051            int result = getPriority().compareTo(other.getPriority());
052            if (result == 0) {
053                result = getInsertionOrder().compareTo(other.getInsertionOrder());
054            }
055            return result;
056        }
057    
058        /** {@inheritDoc} */
059        @Override
060        public boolean equals(Object obj) {
061            if (this == obj) return true;
062            if (obj == null) return false;
063            if (getClass() != obj.getClass()) return false;
064            ValidatorWrapper<?> other = (ValidatorWrapper<?>) obj;
065            if (name == null) {
066                if (other.name != null) return false;
067            } else if (!name.equals(other.name)) return false;
068            return true;
069        }
070    
071        /**
072         * @return the insertionOrder
073         */
074        public Integer getInsertionOrder() {
075            return insertionOrder;
076        }
077    
078        /**
079         * @return the name
080         */
081        public String getName() {
082            return name;
083        }
084    
085        /**
086         * @return the priority
087         */
088        public Integer getPriority() {
089            return priority;
090        }
091    
092        /**
093         * @return the validator
094         */
095        public Validator<T> getValidator() {
096            return validator;
097        }
098    
099        /** {@inheritDoc} */
100        @Override
101        public int hashCode() {
102            final int prime = 31;
103            int result = 1;
104            result = prime * result + ((name == null) ? 0 : name.hashCode());
105            return result;
106        }
107    
108    }