1 package com.trendmicro.grid.acl.ds;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6
7 /**
8 * Utilities to work with the data source api.
9 *
10 * @author juergen_kellerer, 2010-06-01
11 * @version 1.0
12 */
13 public class DSUtil {
14
15 /**
16 * Shortens casting the given source collection to a collection of the specified class.
17 *
18 * @param source the source to cast.
19 * @param cls the class of the target collections elements.
20 * @param <E> the class of the target collections elements.
21 * @return the given collection instance without any modification.
22 */
23 @SuppressWarnings("unchecked")
24 public static <E> Collection<E> cast(Collection<? extends E> source, Class<E> cls) {
25 return (Collection<E>) source;
26 }
27
28 /**
29 * Shortens casting the given source collection to a collection of the specified class.
30 *
31 * @param source the source to cast.
32 * @param cls the class of the target collections elements.
33 * @param <E> the class of the target collections elements.
34 * @return the given collection instance without any modification.
35 */
36 @SuppressWarnings("unchecked")
37 public static <E> List<E> cast(List<? extends E> source, Class<E> cls) {
38 return (List<E>) source;
39 }
40
41 /**
42 * Replaces all 'null' entries in the given collection with the specified value.
43 *
44 * @param source the source list.
45 * @param with the replacement of 'null'.
46 * @param <E> the type of the list elements.
47 * @return a collection that does not contain 'null' values.
48 */
49 public static <E> Collection<E> replaceNulls(Collection<E> source, E with) {
50 Collection<E> replacedList = source;
51
52 if (source != null && source.contains(null)) {
53 replacedList = new ArrayList<E>(source.size());
54 for (E e : source)
55 replacedList.add(e == null ? with : e);
56 }
57
58 return replacedList;
59 }
60
61 private DSUtil() {
62 }
63 }