1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.flatfile.morph; |
18 | |
|
19 | |
import java.util.Locale; |
20 | |
|
21 | |
import org.apache.commons.flatfile.IndexedEntityCollection; |
22 | |
|
23 | |
import net.sf.morph.reflect.Reflector; |
24 | |
import net.sf.morph.reflect.SizableReflector; |
25 | |
import net.sf.morph.reflect.reflectors.ArrayReflector; |
26 | |
import net.sf.morph.reflect.reflectors.CollectionReflector; |
27 | |
import net.sf.morph.reflect.reflectors.EnumerationReflector; |
28 | |
import net.sf.morph.reflect.reflectors.IteratorReflector; |
29 | |
import net.sf.morph.reflect.reflectors.ListReflector; |
30 | |
import net.sf.morph.reflect.reflectors.ObjectReflector; |
31 | |
import net.sf.morph.reflect.reflectors.SetReflector; |
32 | |
import net.sf.morph.reflect.reflectors.SimpleDelegatingReflector; |
33 | |
import net.sf.morph.reflect.reflectors.SortedSetReflector; |
34 | |
import net.sf.morph.transform.copiers.ContainerCopier; |
35 | |
import net.sf.morph.transform.TransformationException; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class ContainerToIndexedEntityCollectionCopier extends ContainerCopier { |
43 | 6 | private static final Class<?>[] DEST_CLASSES = new Class[] { IndexedEntityCollection.class }; |
44 | |
|
45 | 24 | private static class SizeOneReflector extends ObjectReflector implements SizableReflector { |
46 | |
|
47 | |
|
48 | |
|
49 | |
@Override |
50 | |
protected int getSizeImpl(Object container) throws Exception { |
51 | 0 | return 1; |
52 | |
} |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 12 | public ContainerToIndexedEntityCollectionCopier() { |
59 | 16 | setReflector(new SimpleDelegatingReflector(new Reflector[] { |
60 | 4 | new EntityCollectionReflector(), new ListReflector(), new SortedSetReflector(), |
61 | 4 | new SetReflector(), new EnumerationReflector(), new IteratorReflector(), |
62 | 4 | new ArrayReflector(), new CollectionReflector(), new SizeOneReflector() })); |
63 | 12 | } |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
@Override |
69 | |
public synchronized void setSourceClasses(@SuppressWarnings("rawtypes") Class[] sourceClasses) { |
70 | 0 | super.setSourceClasses(sourceClasses); |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
@Override |
77 | |
protected Class<?>[] getSourceClassesImpl() throws Exception { |
78 | 12 | Class<?>[] c = getBeanReflector().getReflectableClasses(); |
79 | 12 | Class<?>[] result = new Class[c.length + 1]; |
80 | 12 | System.arraycopy(c, 0, result, 0, c.length); |
81 | 12 | return result; |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
@Override |
88 | |
public synchronized void setDestinationClasses(@SuppressWarnings("rawtypes") Class[] destinationClasses) { |
89 | 0 | super.setDestinationClasses(destinationClasses); |
90 | 0 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
@Override |
96 | |
protected Class<?>[] getDestinationClassesImpl() throws Exception { |
97 | 12 | return DEST_CLASSES; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
@Override |
104 | |
protected void copyImpl(Object destination, Object source, Locale locale, |
105 | |
Integer preferredTransformationType) throws TransformationException { |
106 | 18 | int size = source == null ? 0 : getBeanReflector().getSize(source); |
107 | 18 | ((IndexedEntityCollection) destination).setSize(size); |
108 | 18 | if (size > 0) { |
109 | 6 | super.copyImpl(destination, source, locale, preferredTransformationType); |
110 | |
} |
111 | 18 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
@Override |
117 | |
protected boolean isAutomaticallyHandlingNulls() { |
118 | 6 | return false; |
119 | |
} |
120 | |
} |