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 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 if (CollectionUtils.isEmpty(toConvert)) {
44 return Collections.emptyList();
45 } else {
46 List<B> results = new ArrayList<B>(toConvert.size());
47 for (ModelBuilder elem : toConvert) {
48 results.add((B)elem.build());
49 }
50 return Collections.unmodifiableList(results);
51 }
52 }
53
54 @SuppressWarnings("unchecked")
55 public static <B> Set<B> convertFromBuilderSet(Set<? extends ModelBuilder> toConvert) {
56 if (CollectionUtils.isEmpty(toConvert)) {
57 return Collections.emptySet();
58 } else {
59 Set<B> results = new HashSet<B>(toConvert.size());
60 for (ModelBuilder elem : toConvert) {
61 results.add((B)elem.build());
62 }
63 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 if (CollectionUtils.isEmpty(toConvert)) {
79 return new ArrayList<B>();
80 } else {
81 List<B> results = new ArrayList<B>(toConvert.size());
82 for (A elem : toConvert) {
83 results.add(xform.transform(elem));
84 }
85 return results;
86 }
87 }
88
89 public static <A,B> Set<B> transform(Set<? extends A> toConvert, Transformer<A,B> xform) {
90 if (CollectionUtils.isEmpty(toConvert)) {
91 return new HashSet<B>();
92 } else {
93 Set<B> results = new HashSet<B>(toConvert.size());
94 for (A elem : toConvert) {
95 results.add(xform.transform(elem));
96 }
97 return results;
98 }
99 }
100
101 public interface Transformer<A,B> {
102 public B transform(A input);
103 }
104
105 }