View Javadoc

1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Utilities for implementing {@link ModelBuilder}s more easily.
32   * 
33   * @author Kuali Rice Team (rice.collab@kuali.org)
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  	 * This method is useful for converting a List&lt;? extends BlahContract&gt; to a 
69  	 * List&lt;Blah.Builder&gt;.  You'll just need to implement Transformer to use it.
70  	 * 
71  	 * @param <A>
72  	 * @param <B>
73  	 * @param toConvert
74  	 * @param xform
75  	 * @return
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 }