001/*
002 *  Copyright 2010, 2011 Christopher 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.loader;
017
018import java.lang.annotation.Annotation;
019import java.lang.reflect.Constructor;
020import java.lang.reflect.Method;
021
022import org.jadira.bindings.core.annotation.DefaultBinding;
023
024/**
025 * Represents a BindingConfigurationEntry - a specification of a particular
026 * binding by configuration 
027 * 
028 * The configuration entry either consists of a) a
029 * binding class implementing either {@link org.jadira.bindings.core.api.Binding}, 
030 * {@link org.jadira.bindings.core.api.ToMarshaller},
031 * {@link org.jadira.bindings.core.api.FromUnmarshaller}, 
032 * or {@link org.jadira.bindings.core.api.Converter} together with an optional
033 * qualifier, b) an arbitrary class to be inspected for annotations or other
034 * configuration meaningful to a Provider, 
035 * or c) an explicit definitionof a method for marshalling or unmarshalling 
036 * and/or constructor (either from method or from constructor can be defined but 
037 * not both).
038 * 
039 * @author Chris
040 */
041public class BindingConfigurationEntry {
042
043        private final Class<?> bindingClass;
044        private final Class<?> sourceClass;
045        private final Class<?> targetClass;
046        private final Class<? extends Annotation> qualifier;
047        private final Method toMethod;
048        private final Method fromMethod;
049        private final Constructor<?> fromConstructor;
050
051        /**
052         * Create a new entry for the given binding class
053         * @param bindingClass The binding class
054         */
055        public BindingConfigurationEntry(Class<?> bindingClass) {
056                this(bindingClass, null);
057        }
058
059        /**
060         * Create a new entry for the given binding class and qualifier
061         * @param bindingClass The binding class
062         * @param qualifier The qualifier
063         */
064        public BindingConfigurationEntry(Class<?> bindingClass, Class<? extends Annotation> qualifier) {
065                
066                this.bindingClass = bindingClass;
067                this.qualifier = qualifier == null ? DefaultBinding.class : qualifier;
068                
069                this.sourceClass = null;
070                this.targetClass = null;
071                this.toMethod = null;
072                this.fromMethod = null;
073                this.fromConstructor = null;
074        }
075        
076        /**
077         * Create a new entry for the given options
078         * @param sourceClass The source class to be bound
079         * @param targetClass The foreign side of the relationship
080         * @param qualifier The qualifier
081         * @param toMethod The to method to be bound
082         * @param fromMethod The from method to be bound
083         */
084        public BindingConfigurationEntry(Class<?> sourceClass, Class<?> targetClass,
085                        Class<? extends Annotation> qualifier, Method toMethod,
086                        Method fromMethod) {
087                
088                this.sourceClass = sourceClass;
089                this.targetClass = targetClass;
090                this.qualifier = qualifier == null ? DefaultBinding.class : qualifier;
091                this.toMethod = toMethod;
092                this.fromMethod = fromMethod;
093                this.fromConstructor = null;
094                
095                this.bindingClass = null;
096        }
097        
098        /**
099         * Create a new entry for the given options
100         * @param sourceClass The source class to be bound
101         * @param targetClass The foreign side of the relationship
102         * @param qualifier The qualifier
103         * @param toMethod The to method to be bound
104         * @param fromConstructor The from constructor to be bound
105         */
106        public BindingConfigurationEntry(Class<?> sourceClass, Class<?> targetClass,
107                        Class<? extends Annotation> qualifier, Method toMethod,
108                        Constructor<?> fromConstructor) {
109                
110                this.sourceClass = sourceClass;
111                this.targetClass = targetClass;
112                this.qualifier = qualifier == null ? DefaultBinding.class : qualifier;
113                this.toMethod = toMethod;
114                this.fromConstructor = fromConstructor;
115                this.fromMethod = null;
116                
117                this.bindingClass = null;
118        }
119
120        /**
121         * @return The Binding Class, if any
122         */
123        public Class<?> getBindingClass() {
124                return bindingClass;
125        }
126
127        /**
128         * @return The Source Class, if any
129         */
130        public Class<?> getSourceClass() {
131                return sourceClass;
132        }
133
134        /**
135         * @return The Target Class, if any
136         */
137        public Class<?> getTargetClass() {
138                return targetClass;
139        }
140
141        /**
142         * @return The Qualifier Annotation, if any
143         */
144        public Class<? extends Annotation> getQualifier() {
145                return qualifier;
146        }
147
148        /** 
149         * @return The To Method, if any
150         */
151        public Method getToMethod() {
152                return toMethod;
153        }
154
155        /** 
156         * @return The From Method, if any
157         */
158        public Method getFromMethod() {
159                return fromMethod;
160        }
161
162        /** 
163         * @return The From Constructor, if any
164         */
165        public Constructor<?> getFromConstructor() {
166                return fromConstructor;
167        }
168}