| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.dozer.fieldmap; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; |
| 19 | |
import org.apache.commons.lang3.builder.ToStringStyle; |
| 20 | |
import org.dozer.util.MappingUtils; |
| 21 | |
|
| 22 | |
import java.util.ArrayList; |
| 23 | |
import java.util.List; |
| 24 | |
import java.util.StringTokenizer; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 4521 | public class HintContainer { |
| 35 | |
private String hintName; |
| 36 | |
private List<Class<?>> hints; |
| 37 | |
|
| 38 | |
public Class<?> getHint() { |
| 39 | |
Class<?> result; |
| 40 | 1440 | if (hasMoreThanOneHint()) { |
| 41 | 98 | return null; |
| 42 | |
} else { |
| 43 | 1342 | result = getHints().get(0); |
| 44 | |
} |
| 45 | 1342 | return result; |
| 46 | |
} |
| 47 | |
|
| 48 | |
public Class<?> getHint(int index) { |
| 49 | 247 | return getHints().get(index); |
| 50 | |
} |
| 51 | |
|
| 52 | |
public boolean hasMoreThanOneHint() { |
| 53 | 1681 | return getHints().size() > 1; |
| 54 | |
} |
| 55 | |
|
| 56 | |
public List<Class<?>> getHints() { |
| 57 | 4364 | if (hints == null) { |
| 58 | 438 | List<Class<?>> list = new ArrayList<Class<?>>(); |
| 59 | 438 | StringTokenizer st = new StringTokenizer(this.hintName, ","); |
| 60 | 944 | while (st.hasMoreElements()) { |
| 61 | 506 | String theHintName = st.nextToken().trim(); |
| 62 | |
|
| 63 | 506 | Class<?> clazz = MappingUtils.loadClass(theHintName); |
| 64 | 506 | list.add(clazz); |
| 65 | 506 | } |
| 66 | 438 | hints = list; |
| 67 | |
} |
| 68 | 4364 | return hints; |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
public Class<?> getHint(Class<?> clazz, List<Class<?>> clazzHints) { |
| 73 | 544 | List<Class<?>> hints = getHints(); |
| 74 | 544 | int hintsSize = hints.size(); |
| 75 | 544 | if (hintsSize == 1) { |
| 76 | 354 | return getHint(); |
| 77 | |
} |
| 78 | |
|
| 79 | 190 | if (clazzHints.size() != hintsSize) { |
| 80 | 0 | MappingUtils |
| 81 | |
.throwMappingException("When using multiple source and destination hints there must be exactly the same number of hints on the source and the destination."); |
| 82 | |
} |
| 83 | 190 | int count = 0; |
| 84 | 190 | String myClazName = MappingUtils.getRealClass(clazz).getName(); |
| 85 | 190 | int size = clazzHints.size(); |
| 86 | 268 | for (int i = 0; i < size; i++) { |
| 87 | 268 | Class<?> element = clazzHints.get(i); |
| 88 | 268 | if (element.getName().equals(myClazName)) { |
| 89 | 190 | return hints.get(count); |
| 90 | |
} |
| 91 | 78 | count++; |
| 92 | |
} |
| 93 | 0 | return clazz; |
| 94 | |
} |
| 95 | |
|
| 96 | |
public void setHintName(String hintName) { |
| 97 | 4517 | this.hintName = hintName; |
| 98 | 4517 | } |
| 99 | |
|
| 100 | |
@Override |
| 101 | |
public String toString() { |
| 102 | 0 | return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE); |
| 103 | |
} |
| 104 | |
} |