View Javadoc

1   /*
2    * Copyright 2004-2005 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.composite.util;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.List;
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 {
32  	
33  	public static Object[] getElementsOfType(Object[] array, Class type) {
34  		Assert.notEmpty(type, "type");
35  		if (array == null) {
36  			return null;
37  		}
38  		
39  		List list = getElementsOfType(Arrays.asList(array), type);
40  		
41  		Object[] toReturn = (Object[]) ClassUtils.createArray(type, list.size());
42  		return list.toArray(toReturn);
43  	}
44  	
45  	public static List getElementsOfType(Collection collection, Class type) {
46  		Assert.notEmpty(type, "type");
47  		if (collection == null) {
48  			return null;
49  		}
50  		
51  		List elementsOfType = new ArrayList();
52  		for (Iterator i=collection.iterator(); i.hasNext(); ) {
53  			Object element = i.next();
54  			if (element != null &&
55  				type.isAssignableFrom(element.getClass())) {
56  				elementsOfType.add(element);
57  			}
58  		}
59  		return elementsOfType;
60  	}
61  
62  	public static boolean hasElementOfType(Object container, Class type) {
63  		Assert.notEmpty(container, "container");
64  		Assert.notEmpty(type, "type");
65  		
66  		Iterator iterator = getIterator(container);
67  		while (iterator.hasNext()) {
68  			Object element = iterator.next();
69  			if (element != null &&
70  				type.isAssignableFrom(element.getClass())) {
71  				return true;
72  			}
73  		}
74  		return false;
75  	}
76  
77  	/***
78  	 * Returns an iterator over the contents of the given container.
79  	 * 
80  	 * @param container
81  	 *            the container
82  	 * @return <code>null</code>, if container is <code>null</code> or <br>
83  	 *         an iterator over the contents of the container, otherwise
84  	 * @throws IllegalArgumentException
85  	 *             if an iterator over the contents of the container could not
86  	 *             be constructed
87  	 */
88  	public static Iterator getIterator(Object container) {
89  		if (container == null) {
90  			return null;
91  		}
92  		else if (container instanceof Collection) {
93  			return ((Collection) container).iterator();
94  		}
95  		else if (container.getClass().isArray()) {
96  			return new ArrayIterator(container);
97  		}
98  
99  		throw new IllegalArgumentException("Could not construct an iterator for " + ObjectUtils.getObjectDescription(container));
100 	}
101 
102 }