View Javadoc

1   /*
2    * Copyright 2004-2005, 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.util;
17  
18  import java.util.Arrays;
19  import java.util.Collection;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Set;
23  
24  /**
25   * Utility functions for working with container-like objects such as Collections
26   * and arrays.
27   *
28   * @author Matt Sgarlata
29   * @since Mar 11, 2005
30   */
31  public class ContainerUtils extends net.sf.composite.util.ContainerUtils {
32  
33  	/**
34  	 * Determines if the given <code>array</code> contains the given
35  	 * <code>value</code>.
36  	 *
37  	 * @param array
38  	 *            the array to test
39  	 * @param value
40  	 *            the value to test
41  	 * @return <code>true</code> if the given array contains the given value
42  	 *         or <br>
43  	 *         <code>false</code>, otherwise
44  	 */
45  	public static boolean contains(Object[] array, Object value) {
46  		if (array == null) {
47  			return false;
48  		}
49  		for (int i = 0; i < array.length; i++) {
50  			if (array[i] == value || (array[i] != null && array[i].equals(value))) {
51  				return true;
52  			}
53  		}
54  		return false;
55  	}
56  
57  	/**
58  	 * Determines if the given <code>collection</code> contains the given
59  	 * <code>value</code>.
60  	 *
61  	 * @param collection
62  	 *            the collection to test
63  	 * @param value
64  	 *            the value to test
65  	 * @return <code>true</code> if the given collection contains the given value
66  	 *         or <br>
67  	 *         <code>false</code>, otherwise
68  	 */
69  	public static boolean contains(Collection collection, Object value) {
70  		return collection != null && collection.contains(value);
71  	}
72  
73  //	/**
74  //	 * Returns the union of multiple arrays as an array.  Implementation is O(n<sup>2</sup>).
75  //	 *
76  //	 * @param arrays
77  //	 *            a List of arrays
78  //	 * @param componentType
79  //	 *            the runtime type of the returned array
80  //	 * @return the union of the arrays
81  //	 * @deprecated this is silly, just use a HashSet instead
82  //	 */
83  //	public static Object[] union(List arrays, Class componentType) {
84  //		if (componentType == null) {
85  //			throw new IllegalArgumentException("componentType must be speciifed");
86  //		}
87  //		if (arrays == null) {
88  //			return null;
89  //		}
90  //
91  //		Set unionSet = new HashSet();
92  //		for (int i=0; i<arrays.size(); i++) {
93  //			if (!ObjectUtils.isEmpty(arrays.get(i))) {
94  //				unionSet.addAll(Arrays.asList( ((Object[]) arrays.get(i)) ));
95  //			}
96  //		}
97  //
98  //		Object[] unionArray = (Object[]) ClassUtils.createArray(componentType, unionSet.size());
99  //		return unionSet.toArray(unionArray);
100 //	}
101 //
102 //	/**
103 //	 * Returns the union of multiple arrays as an array.  Implementation is O(n<sup>2</sup>).
104 //	 *
105 //	 * @param arrays
106 //	 *            a List of arrays
107 //	 * @return the union of the arrays
108 //	 */
109 //	public static Object[] union(List arrays) {
110 //		return union(arrays, Object[].class);
111 //	}
112 
113 	/**
114 	 * Returns the intersection of multiple arrays as an array.  Implementation is O(n<sup>3</sup>).
115 	 *
116 	 * @param arrays
117 	 *            a List of arrays
118 	 * @param componentType
119 	 *            the runtime type of the returned array
120 	 * @return the intersection of the arrays
121 	 */
122 	public static Object[] intersection(List arrays, Class componentType) {
123 		if (componentType == null) {
124 			throw new IllegalArgumentException("componentType must be speciifed");
125 		}
126 		if (arrays == null) {
127 			return null;
128 		}
129 
130 		Set intersectionSet = new HashSet();
131 		intersectionSet.addAll(Arrays.asList( ((Object[]) arrays.get(0)) ));
132 		for (int i = 1; i < arrays.size(); i++) {
133 			Object[] array = (Object[]) arrays.get(i);
134 			for (int j = 0; j < array.length; j++) {
135 				if (!contains(intersectionSet, array[j])) {
136 					intersectionSet.remove(array[j]);
137 				}
138 			}
139 		}
140 
141 		Object[] intersectionArray = (Object[]) ClassUtils.createArray(componentType, intersectionSet.size());
142 		return intersectionSet.toArray(intersectionArray);
143 	}
144 
145 	/**
146 	 * Returns the intersection of multiple arrays as an array.  Implementation is O(n<sup>3</sup>).
147 	 *
148 	 * @param arrays
149 	 *            a List of arrays
150 	 * @return the intersection of the arrays
151 	 */
152 	public static Object[] intersection(List arrays) {
153 		return intersection(arrays, Object[].class);
154 	}
155 
156 	/**
157 	 * Create an ordered Set implementation based the classes available in the current environment.
158 	 * @return Set
159 	 */
160 	public static Set createOrderedSet() {
161 		if (ClassUtils.isJdk14OrHigherPresent()) {
162 			try {
163 				return (Set) Class.forName("java.util.LinkedHashSet").newInstance();	
164 			}
165 			catch (Exception e) { }
166 		}
167 		
168 		if (ClassUtils.isCommonsCollections3Present()) {
169 			try {
170 				return (Set) Class.forName("org.apache.commons.collections.set.ListOrderedSet").newInstance();			
171 			}
172 			catch (Exception e) { }
173 		}
174 		
175 		return new ListOrderedSet();
176 	}
177 
178 }