View Javadoc
1   /*
2    *  Copyright 2010, 2011 Christopher Pheby
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.jadira.bindings.core.loader;
17  
18  import java.lang.annotation.Annotation;
19  import java.lang.reflect.Constructor;
20  import java.lang.reflect.Method;
21  
22  import org.jadira.bindings.core.annotation.DefaultBinding;
23  
24  /**
25   * Represents a BindingConfigurationEntry - a specification of a particular
26   * binding by configuration 
27   * 
28   * The configuration entry either consists of a) a
29   * binding class implementing either {@link org.jadira.bindings.core.api.Binding}, 
30   * {@link org.jadira.bindings.core.api.ToMarshaller},
31   * {@link org.jadira.bindings.core.api.FromUnmarshaller}, 
32   * or {@link org.jadira.bindings.core.api.Converter} together with an optional
33   * qualifier, b) an arbitrary class to be inspected for annotations or other
34   * configuration meaningful to a Provider, 
35   * or c) an explicit definitionof a method for marshalling or unmarshalling 
36   * and/or constructor (either from method or from constructor can be defined but 
37   * not both).
38   * 
39   * @author Chris
40   */
41  public class BindingConfigurationEntry {
42  
43  	private final Class<?> bindingClass;
44  	private final Class<?> sourceClass;
45  	private final Class<?> targetClass;
46  	private final Class<? extends Annotation> qualifier;
47  	private final Method toMethod;
48  	private final Method fromMethod;
49  	private final Constructor<?> fromConstructor;
50  
51  	/**
52  	 * Create a new entry for the given binding class
53  	 * @param bindingClass The binding class
54  	 */
55  	public BindingConfigurationEntry(Class<?> bindingClass) {
56  		this(bindingClass, null);
57  	}
58  
59  	/**
60  	 * Create a new entry for the given binding class and qualifier
61  	 * @param bindingClass The binding class
62  	 * @param qualifier The qualifier
63  	 */
64  	public BindingConfigurationEntry(Class<?> bindingClass, Class<? extends Annotation> qualifier) {
65  		
66  		this.bindingClass = bindingClass;
67  		this.qualifier = qualifier == null ? DefaultBinding.class : qualifier;
68  		
69  		this.sourceClass = null;
70  		this.targetClass = null;
71  		this.toMethod = null;
72  		this.fromMethod = null;
73  		this.fromConstructor = null;
74  	}
75  	
76  	/**
77  	 * Create a new entry for the given options
78  	 * @param sourceClass The source class to be bound
79  	 * @param targetClass The foreign side of the relationship
80  	 * @param qualifier The qualifier
81  	 * @param toMethod The to method to be bound
82  	 * @param fromMethod The from method to be bound
83  	 */
84  	public BindingConfigurationEntry(Class<?> sourceClass, Class<?> targetClass,
85  			Class<? extends Annotation> qualifier, Method toMethod,
86  			Method fromMethod) {
87  		
88  		this.sourceClass = sourceClass;
89  		this.targetClass = targetClass;
90  		this.qualifier = qualifier == null ? DefaultBinding.class : qualifier;
91  		this.toMethod = toMethod;
92  		this.fromMethod = fromMethod;
93  		this.fromConstructor = null;
94  		
95  		this.bindingClass = null;
96  	}
97  	
98  	/**
99  	 * 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 }