Add supported basic types to association config
[smart-dao.git] / smart-exim / smart-exim-api / src / main / java / com / smartitengineering / exim / AssociationConfig.java
blobddd88061a49cb605c1247210c12aabc06c759d18
1 /*
2 * This is a common dao with basic CRUD operations and is not limited to any
3 * persistent layer implementation
4 *
5 * Copyright (C) 2008 Imran M Yousuf (imyousuf@smartitengineering.com)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 package com.smartitengineering.exim;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
27 /**
28 * A configuration API for all association in an exim-able object. Since in Java
29 * all types are of type {@link Object} we will considering every member (a
30 * field or a property) an association and thus store a configuration for it.
31 * @author imyousuf
32 * @since 0.4
34 public interface AssociationConfig {
36 /**
37 * The accessor name for the property, if field is being used to access then
38 * it will be the name of the field else it will be the name of the method
39 * being used to read it.
40 * @return Accessor string, it will be non-blank
42 public String getAccessorName();
44 /**
45 * Defines whether the associate is to be exported as URI or not.
46 * @return True if its expected to export as URI else false
48 public boolean isItToBeExportedAsUri();
50 /**
51 * Defines whether the association is to be ignored for when the domain is
52 * exported or imported.
53 * @return True if it is to be ignored for Ex-Im.
55 public boolean isTransient();
57 /**
58 * Defines whether the object should be imported eagerly or on demand. It
59 * will only be supported if the resource is exported using properties. If
60 * any association config has it set then it will use CGLIB for proxying the
61 * resources.
62 * @return True if association is to be fetched eagerly.
63 * @see AssociationConfig#isItToBeExportedAsUri()
64 * @see AssociationConfig#isTransient()
66 public boolean isEagerSet();
68 /**
69 * Defines the name representing the association in the exported doc.
70 * @return Name to be used in export/import; if null then field/property
71 * name will be used instead.
73 public String getName();
75 /**
76 * Defines whether the association has its own string value generator
77 * implementation, (if its not exported as URI) if it does have a provider
78 * then it will be used else toString will be used.
79 * @return True if it has a string provider implemented.
80 * @see AssociationConfig#isItToBeExportedAsUri()
82 public boolean isStringProviderImplemented();
84 /**
85 * Defines the type of association this configuration represents
86 * @return Type of the associaiton
88 public AssociationType getAssociationType();
90 /**
91 * It defimes types that impacts how the data is used in order to be
92 * exported.
94 public enum AssociationType {
96 /**
97 * Represents anything thats not any form of collection
99 TYPE_OBJECT("array", Object.class),
101 * Represents List
103 TYPE_LIST("list", List.class),
105 * Represents Set
107 TYPE_SET("set", Set.class),
109 * Represents Collection
111 TYPE_COLLECTION("collection", Collection.class),
113 * Represents Array
115 TYPE_ARRAY("object", Object.class),
117 * Represents Map
119 TYPE_MAP("map", Map.class),
121 * Represents type 'double'
123 TYPE_DOUBLE("double", double.class, Double.class),
125 * Represents type 'float'
127 TYPE_FLOAT("float", float.class, Float.class),
129 * Represents type 'long'
131 TYPE_LONG("long", long.class, Long.class),
133 * Represents type 'int'
135 TYPE_INTEGER("integer", int.class, Integer.class),
137 * Represents type 'String'
139 TYPE_STRING("string", String.class),
141 * Represents type 'boolean'
143 TYPE_BOOLEAN("boolean", boolean.class, Boolean.class),
145 * Represents type 'java.util.Date'
147 TYPE_DATE("date", Date.class),;
148 private Class[] rootClasses;
149 private String simpleName;
151 AssociationType(String simpleName,
152 Class... rootClass) {
153 if (rootClass == null || rootClass.length <= 0) {
154 throw new IllegalArgumentException(
155 "There should be at least one type!");
157 this.rootClasses = rootClass;
158 this.simpleName = simpleName;
162 * The root {@link Class} of the type its representing
163 * @return The class type
165 public Class[] getRootClasses() {
166 return rootClasses;
169 public String getSimpleName() {
170 return simpleName;
173 public boolean isAssignableFrom(Class clazz) {
174 if (clazz == null) {
175 return false;
177 for (Class myClazz : getRootClasses()) {
178 if (myClazz.isAssignableFrom(clazz)) {
179 return true;
182 return false;
186 * Given any {@link Class} it will determine the {@link AssociationType}
187 * for that class.
188 * @param clazz The class whose type to determine
189 * @return Type of the clazz
190 * @throws NullPointerException If clazz is null
192 public static AssociationType getAssociationType(Class clazz)
193 throws NullPointerException {
194 if (clazz.isArray()) {
195 return TYPE_ARRAY;
197 if (TYPE_MAP.isAssignableFrom(clazz)) {
198 return TYPE_MAP;
200 if (TYPE_LIST.isAssignableFrom(clazz)) {
201 return TYPE_LIST;
203 if (TYPE_SET.isAssignableFrom(clazz)) {
204 return TYPE_SET;
206 if (TYPE_COLLECTION.isAssignableFrom(clazz)) {
207 return TYPE_COLLECTION;
209 if (TYPE_DATE.isAssignableFrom(clazz)) {
210 return TYPE_DATE;
212 if (TYPE_BOOLEAN.isAssignableFrom(clazz)) {
213 return TYPE_BOOLEAN;
215 if (TYPE_STRING.isAssignableFrom(clazz)) {
216 return TYPE_STRING;
218 if (TYPE_INTEGER.isAssignableFrom(clazz)) {
219 return TYPE_INTEGER;
221 if (TYPE_LONG.isAssignableFrom(clazz)) {
222 return TYPE_LONG;
224 if (TYPE_FLOAT.isAssignableFrom(clazz)) {
225 return TYPE_FLOAT;
227 if (TYPE_DOUBLE.isAssignableFrom(clazz)) {
228 return TYPE_DOUBLE;
230 return TYPE_OBJECT;