| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ModelObjectUtils |
|
| 3.0;3 |
| 1 | package org.kuali.rice.core.api.mo; | |
| 2 | ||
| 3 | import org.apache.commons.collections.CollectionUtils; | |
| 4 | ||
| 5 | import java.util.ArrayList; | |
| 6 | import java.util.Collections; | |
| 7 | import java.util.List; | |
| 8 | ||
| 9 | /** | |
| 10 | * A set of simple utilities to assist with common idioms in immutable model objects and their builders. | |
| 11 | * | |
| 12 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 13 | */ | |
| 14 | public class ModelObjectUtils { | |
| 15 | ||
| 16 | /** | |
| 17 | * Takes the given list of {@code ModelBuilder} objects and invokes the | |
| 18 | * {@link org.kuali.rice.core.api.mo.ModelBuilder#build()} method on each of them, adding them to a new list and | |
| 19 | * return an unmodifiable copy. If the given list is empty or null, will return an empty and unmodifiable list. | |
| 20 | * | |
| 21 | * @param builderList the list of builders to build and add to resulting list, may be empty or null | |
| 22 | * @param <T> the type of the object that is built by the builders in the list, it is up to the caller of this | |
| 23 | * method to ensure they define the proper parameterized list for the return type. | |
| 24 | * @return an unmodifiable list containing objects built from the given list of model builders | |
| 25 | */ | |
| 26 | public static <T> List<T> buildImmutableCopy(List<? extends ModelBuilder> builderList) { | |
| 27 | 0 | if (CollectionUtils.isEmpty(builderList)) { |
| 28 | 0 | return Collections.emptyList(); |
| 29 | } | |
| 30 | 0 | List<T> copy = new ArrayList<T>(); |
| 31 | 0 | for (ModelBuilder builder : builderList) { |
| 32 | // since ModelBuilder is not parameterized, this code must assume that the appropriate type of object is built | |
| 33 | @SuppressWarnings("unchecked") | |
| 34 | 0 | T built = (T)builder.build(); |
| 35 | 0 | copy.add(built); |
| 36 | 0 | } |
| 37 | 0 | return Collections.unmodifiableList(copy); |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * Takes the given list and returns an unmodifiable copy of that list containing the same elements as the original | |
| 42 | * list. This method handles a null list being passed to it by returning an unmodifiable empty list. | |
| 43 | * | |
| 44 | * @param listToCopy the list to copy | |
| 45 | * @param <T> the type of the elements in the given list | |
| 46 | * | |
| 47 | * @return an unmodifiable copy containing the same elements as the given list | |
| 48 | */ | |
| 49 | public static <T> List<T> createImmutableCopy(List<T> listToCopy) { | |
| 50 | 0 | if (CollectionUtils.isEmpty(listToCopy)) { |
| 51 | 0 | return Collections.emptyList(); |
| 52 | } | |
| 53 | 0 | return Collections.unmodifiableList(new ArrayList<T>(listToCopy)); |
| 54 | } | |
| 55 | ||
| 56 | 0 | private ModelObjectUtils() { |
| 57 | 0 | throw new UnsupportedOperationException("Do not call."); |
| 58 | } | |
| 59 | ||
| 60 | } |