Coverage Report - org.kuali.rice.krms.api.repository.BuilderUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
BuilderUtils
38%
10/26
37%
6/16
3.4
BuilderUtils$Transformer
N/A
N/A
3.4
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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  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  1
                 if (CollectionUtils.isEmpty(toConvert)) {
 44  0
                         return Collections.emptyList();
 45  
                 } else {
 46  1
                         List<B> results = new ArrayList<B>(toConvert.size());
 47  1
                         for (ModelBuilder elem : toConvert) {
 48  1
                                 results.add((B)elem.build());
 49  
                         }
 50  1
                         return Collections.unmodifiableList(results);
 51  
                 }
 52  
         } 
 53  
         
 54  
         @SuppressWarnings("unchecked")
 55  
         public static <B> Set<B> convertFromBuilderSet(Set<? extends ModelBuilder> toConvert) {
 56  1
                 if (CollectionUtils.isEmpty(toConvert)) {
 57  0
                         return Collections.emptySet();
 58  
                 } else {
 59  1
                         Set<B> results = new HashSet<B>(toConvert.size());
 60  1
                         for (ModelBuilder elem : toConvert) {
 61  1
                                 results.add((B)elem.build());
 62  
                         }
 63  1
                         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  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  
 }