001/*
002 *  Copyright 2011 Chris Pheby
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.jadira.bindings.core.binder;
017
018import java.lang.annotation.Annotation;
019
020import org.jadira.bindings.core.annotation.DefaultBinding;
021
022/**
023 * A {@link ConverterKey} is used to look up a registered converter
024 */
025public class ConverterKey<I,O> {
026        
027        private final Class<I> input;
028        private final Class<O> output;
029        private final Class<? extends Annotation> qualifier;
030
031        /**
032         * Creates a new ConverterKey instance
033         * @param input The Input class
034         * @param output The output class
035         * @param qualifier The qualifier for the annotation
036         */
037        public ConverterKey(Class<I> input, Class<O> output, Class<? extends Annotation> qualifier) {
038                this.input = input;
039                this.output = output;
040                this.qualifier = qualifier == null ? DefaultBinding.class : qualifier;
041        }
042        
043        /**
044         * Gets the configured input class
045         * @return The input class
046         */
047        public Class<I> getInputClass() {
048                return input;
049        }
050
051        /**
052         * Gets the configured output class
053         * @return The output class
054         */
055        public Class<O> getOutputClass() {
056                return output;
057        }
058        
059        /**
060         * Gets the configured qualifier
061         * @return The qualifier
062         */
063        public Class<? extends Annotation> getQualifierAnnotation() {
064                return qualifier;
065        }
066        
067        /**
068         * {@inheritDoc}
069         */
070        @Override
071        public boolean equals(Object obj) {
072        if (obj == null) {
073                return false;
074        }
075        if (!this.getClass().equals(obj.getClass())) {
076                return false;
077        }
078       
079        final ConverterKey<?,?> obj2 = (ConverterKey<?,?>)obj;
080        if (this.getInputClass().equals(obj2.getInputClass()) 
081                        && this.getOutputClass().equals(obj2.getOutputClass())
082                        && this.getQualifierAnnotation().equals(obj2.getQualifierAnnotation())) {
083            return true;
084        }
085        
086        return false;
087        }
088
089        /**
090         * {@inheritDoc}
091         */
092        @Override
093        public int hashCode() {
094                return getInputClass().hashCode() * 3 + getOutputClass().hashCode() * 5 + getQualifierAnnotation().hashCode() * 7;
095        }
096        
097        /**
098         * {@inheritDoc}
099         */
100        @Override
101        public String toString() {
102                return "input: {" + getInputClass().getName() + "}, output: {" + getOutputClass().getName() + "}, qualifier: {" + getQualifierAnnotation().getName() + "}";
103        }
104        
105        public ConverterKey<O,I> invert() {
106                return new ConverterKey<O,I>(output, input, qualifier);
107        }
108}