1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krms.api.repository; |
17 | |
|
18 | |
import java.lang.reflect.InvocationTargetException; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Collections; |
21 | |
import java.util.HashSet; |
22 | |
import java.util.List; |
23 | |
import java.util.Set; |
24 | |
|
25 | |
import org.apache.commons.collections.CollectionUtils; |
26 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
27 | |
import org.kuali.rice.core.api.mo.ModelBuilder; |
28 | |
import org.kuali.rice.core.api.mo.ModelObjectComplete; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class BuilderUtils { |
37 | |
|
38 | |
private static final String COULDNT_INVOKE_BUILDER_CREATE = "couldn't invoke Builder.create()"; |
39 | |
|
40 | |
|
41 | |
@SuppressWarnings("unchecked") |
42 | |
public static <B> List<B> convertFromBuilderList(List<? extends ModelBuilder> toConvert) { |
43 | 0 | if (CollectionUtils.isEmpty(toConvert)) { |
44 | 0 | return Collections.emptyList(); |
45 | |
} else { |
46 | 0 | List<B> results = new ArrayList<B>(toConvert.size()); |
47 | 0 | for (ModelBuilder elem : toConvert) { |
48 | 0 | results.add((B)elem.build()); |
49 | |
} |
50 | 0 | return Collections.unmodifiableList(results); |
51 | |
} |
52 | |
} |
53 | |
|
54 | |
@SuppressWarnings("unchecked") |
55 | |
public static <B> Set<B> convertFromBuilderSet(Set<? extends ModelBuilder> toConvert) { |
56 | 0 | if (CollectionUtils.isEmpty(toConvert)) { |
57 | 0 | return Collections.emptySet(); |
58 | |
} else { |
59 | 0 | Set<B> results = new HashSet<B>(toConvert.size()); |
60 | 0 | for (ModelBuilder elem : toConvert) { |
61 | 0 | results.add((B)elem.build()); |
62 | |
} |
63 | 0 | return Collections.unmodifiableSet(results); |
64 | |
} |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public static <A,B> List<B> transform(List<? extends A> toConvert, Transformer<A,B> xform) { |
78 | 0 | if (CollectionUtils.isEmpty(toConvert)) { |
79 | 0 | return new ArrayList<B>(); |
80 | |
} else { |
81 | 0 | List<B> results = new ArrayList<B>(toConvert.size()); |
82 | 0 | for (A elem : toConvert) { |
83 | 0 | results.add(xform.transform(elem)); |
84 | |
} |
85 | 0 | return results; |
86 | |
} |
87 | |
} |
88 | |
|
89 | |
public static <A,B> Set<B> transform(Set<? extends A> toConvert, Transformer<A,B> xform) { |
90 | 0 | if (CollectionUtils.isEmpty(toConvert)) { |
91 | 0 | return new HashSet<B>(); |
92 | |
} else { |
93 | 0 | Set<B> results = new HashSet<B>(toConvert.size()); |
94 | 0 | for (A elem : toConvert) { |
95 | 0 | results.add(xform.transform(elem)); |
96 | |
} |
97 | 0 | return results; |
98 | |
} |
99 | |
} |
100 | |
|
101 | 0 | public interface Transformer<A,B> { |
102 | |
public B transform(A input); |
103 | |
} |
104 | |
|
105 | |
} |