001 /** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krms.api.repository; 017 018 import java.lang.reflect.InvocationTargetException; 019 import java.util.ArrayList; 020 import java.util.Collections; 021 import java.util.HashSet; 022 import java.util.List; 023 import java.util.Set; 024 025 import org.apache.commons.collections.CollectionUtils; 026 import org.kuali.rice.core.api.exception.RiceRuntimeException; 027 import org.kuali.rice.core.api.mo.ModelBuilder; 028 import org.kuali.rice.core.api.mo.ModelObjectComplete; 029 030 /** 031 * Utilities for implementing {@link ModelBuilder}s more easily. 032 * 033 * @author Kuali Rice Team (rice.collab@kuali.org) 034 * 035 */ 036 public class BuilderUtils { 037 038 private static final String COULDNT_INVOKE_BUILDER_CREATE = "couldn't invoke Builder.create()"; 039 040 041 @SuppressWarnings("unchecked") 042 public static <B> List<B> convertFromBuilderList(List<? extends ModelBuilder> toConvert) { 043 if (CollectionUtils.isEmpty(toConvert)) { 044 return Collections.emptyList(); 045 } else { 046 List<B> results = new ArrayList<B>(toConvert.size()); 047 for (ModelBuilder elem : toConvert) { 048 results.add((B)elem.build()); 049 } 050 return Collections.unmodifiableList(results); 051 } 052 } 053 054 @SuppressWarnings("unchecked") 055 public static <B> Set<B> convertFromBuilderSet(Set<? extends ModelBuilder> toConvert) { 056 if (CollectionUtils.isEmpty(toConvert)) { 057 return Collections.emptySet(); 058 } else { 059 Set<B> results = new HashSet<B>(toConvert.size()); 060 for (ModelBuilder elem : toConvert) { 061 results.add((B)elem.build()); 062 } 063 return Collections.unmodifiableSet(results); 064 } 065 } 066 067 /** 068 * This method is useful for converting a List<? extends BlahContract> to a 069 * List<Blah.Builder>. You'll just need to implement Transformer to use it. 070 * 071 * @param <A> 072 * @param <B> 073 * @param toConvert 074 * @param xform 075 * @return 076 */ 077 public static <A,B> List<B> transform(List<? extends A> toConvert, Transformer<A,B> xform) { 078 if (CollectionUtils.isEmpty(toConvert)) { 079 return new ArrayList<B>(); 080 } else { 081 List<B> results = new ArrayList<B>(toConvert.size()); 082 for (A elem : toConvert) { 083 results.add(xform.transform(elem)); 084 } 085 return results; 086 } 087 } 088 089 public static <A,B> Set<B> transform(Set<? extends A> toConvert, Transformer<A,B> xform) { 090 if (CollectionUtils.isEmpty(toConvert)) { 091 return new HashSet<B>(); 092 } else { 093 Set<B> results = new HashSet<B>(toConvert.size()); 094 for (A elem : toConvert) { 095 results.add(xform.transform(elem)); 096 } 097 return results; 098 } 099 } 100 101 public interface Transformer<A,B> { 102 public B transform(A input); 103 } 104 105 }