1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.api.mo;
17
18 import org.apache.commons.collections.CollectionUtils;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29
30
31
32
33
34 public class ModelObjectUtils {
35
36
37
38
39
40
41
42
43
44
45
46 public static <T> List<T> buildImmutableCopy(List<? extends ModelBuilder> builderList) {
47 if (CollectionUtils.isEmpty(builderList)) {
48 return Collections.emptyList();
49 }
50 List<T> copy = new ArrayList<T>();
51 for (ModelBuilder builder : builderList) {
52
53 @SuppressWarnings("unchecked")
54 T built = (T)builder.build();
55 copy.add(built);
56 }
57 return Collections.unmodifiableList(copy);
58 }
59
60 @SuppressWarnings("unchecked")
61 public static <B> Set<B> buildImmutableCopy(Set<? extends ModelBuilder> toConvert) {
62 if (CollectionUtils.isEmpty(toConvert)) {
63 return Collections.emptySet();
64 } else {
65 Set<B> results = new HashSet<B>(toConvert.size());
66 for (ModelBuilder elem : toConvert) {
67 results.add((B)elem.build());
68 }
69 return Collections.unmodifiableSet(results);
70 }
71 }
72
73 @SuppressWarnings("unchecked")
74 public static <T> T buildImmutable(ModelBuilder builder) {
75 if (builder == null) {
76 return null;
77 }
78 return (T)builder.build();
79 }
80
81
82
83
84
85
86
87
88
89
90 public static <T> List<T> createImmutableCopy(List<T> listToCopy) {
91 if (CollectionUtils.isEmpty(listToCopy)) {
92 return Collections.emptyList();
93 }
94 return Collections.unmodifiableList(new ArrayList<T>(listToCopy));
95 }
96
97
98
99
100
101
102
103
104
105
106 public static <T> Set<T> createImmutableCopy(Set<T> setToCopy) {
107 if (CollectionUtils.isEmpty(setToCopy)) {
108 return Collections.emptySet();
109 }
110 return Collections.unmodifiableSet(new HashSet<T>(setToCopy));
111 }
112
113
114
115
116
117
118
119
120
121
122 public static <K, V> Map<K, V> createImmutableCopy(Map<K, V> mapToCopy) {
123 if (mapToCopy == null || mapToCopy.isEmpty()) {
124 return Collections.emptyMap();
125 }
126 return Collections.unmodifiableMap(new HashMap<K, V>(mapToCopy));
127 }
128
129
130
131
132
133
134
135
136
137
138
139 public static <A,B> List<B> transform(Collection<? extends A> toConvert, Transformer<A,B> xform) {
140 if (CollectionUtils.isEmpty(toConvert)) {
141 return new ArrayList<B>();
142 } else {
143 List<B> results = new ArrayList<B>(toConvert.size());
144 for (A elem : toConvert) {
145 results.add(xform.transform(elem));
146 }
147 return results;
148 }
149 }
150
151
152
153
154
155
156
157
158
159
160
161 public static <A,B> Set<B> transformSet(Collection<? extends A> toConvert, Transformer<A,B> xform) {
162 if (CollectionUtils.isEmpty(toConvert)) {
163 return new HashSet<B>();
164 } else {
165 Set<B> results = new HashSet<B>(toConvert.size());
166 for (A elem : toConvert) {
167 results.add(xform.transform(elem));
168 }
169 return results;
170 }
171 }
172
173 public interface Transformer<A,B> {
174 public B transform(A input);
175 }
176
177 private ModelObjectUtils() {
178 throw new UnsupportedOperationException("Do not call.");
179 }
180
181 }