KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > fieldmap > Hint


1 /*
2  * Copyright 2005-2007 the original author or authors.
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 net.sf.dozer.util.mapping.fieldmap;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 import net.sf.dozer.util.mapping.MappingException;
23
24 /**
25  * @author garsombke.franz
26  * @author sullins.ben
27  * @author tierney.matt
28  *
29  */

30 public class Hint implements Cloneable JavaDoc {
31
32   private String JavaDoc hintName;
33   private List JavaDoc hints;
34
35   public Class JavaDoc getHint() {
36     if (getHints().size() > 1) {
37       return null;
38     } else {
39       return (Class JavaDoc) getHints().get(0);
40     }
41   }
42
43   public Class JavaDoc getHint(Class JavaDoc myClaz, List JavaDoc clazHints) {
44     List JavaDoc hints = getHints();
45     int hintsSize = hints.size();
46     if (hintsSize == 1) {
47       return getHint();
48     }
49     // validate sizes
50
if (clazHints.size() != hintsSize) {
51       throw new MappingException(
52           "When using multiple source and destination hints there must be exactly the same number of hints on the source and the destination.");
53     }
54     int count = 0;
55     String JavaDoc myClazName = myClaz.getName();
56     int size = clazHints.size();
57     for (int i = 0; i < size; i++) {
58       Class JavaDoc element = (Class JavaDoc) clazHints.get(i);
59       if (element.getName().equals(myClazName)) {
60         return (Class JavaDoc) hints.get(count++);
61       }
62       count++;
63     }
64     return myClaz;
65   }
66
67   public boolean hasMoreThanOneHint() {
68     return getHints().size() > 1 ? true : false;
69   }
70
71   public List JavaDoc getHints() {
72     if (hints == null) {
73       List JavaDoc list = new ArrayList JavaDoc();
74       try {
75         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(this.hintName, ",");
76         while (st.hasMoreElements()) {
77           String JavaDoc theHintName = (String JavaDoc) st.nextToken().trim();
78
79           Class JavaDoc clazz = Thread.currentThread().getContextClassLoader().loadClass(theHintName);
80           list.add(clazz);
81         }
82       } catch (ClassNotFoundException JavaDoc e) {
83         throw new MappingException(e);
84       }
85       hints = list;
86     }
87     return hints;
88   }
89
90   public String JavaDoc getHintName() {
91     return hintName;
92   }
93
94   public void setHintName(String JavaDoc sourceHintName) {
95     this.hintName = sourceHintName;
96   }
97  
98 }
99
Popular Tags