View Javadoc

1   package org.ocltf.utils;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Iterator;
6   
7   /***
8    * Contains utilities for dealing with model "facade" objects.
9    * 
10   * @see org.ocltf.model.ModelFacade
11   * 
12   * @author Chad Brandon
13   */
14  public class FacadeUtils {
15  
16  	/***
17  	 * Checks to see if the element is the specified type and if so
18  	 * casts it to the object and returns it, otherwise it returns null.
19  	 * 
20  	 * @param element the element to check.
21  	 * @param type the Class type.
22  	 * @return java.lang.Object
23  	 */
24  	public static Object getElementAsType(Object element, Class type) {
25  		Object elementAsType = null;
26  		if (element != null && type != null) {
27  			Class elementClass = element.getClass();
28  			if (type.isAssignableFrom(elementClass)) {
29  				elementAsType = element;
30  			}
31  		}
32  		return elementAsType;
33  	}
34  
35  	/***
36  	 * Filters the elements for the elements collection 
37  	 * by the specified type.
38  	 * @param elements the elements to filter.
39  	 * @param type the model element type which to filter by
40  	 * @return java.util.Collection
41  	 */
42  	public static Collection filterElementsByType(Collection elements, Class type) {
43  		String methodName = "filterElementsByType";
44  		ExceptionUtils.checkNull(methodName, "type", type);
45  		ExceptionUtils.checkNull(methodName, "elements", elements);
46  		Collection filtered = new ArrayList();
47  		Collection modelElements = elements;
48  		Iterator modelElementIt = modelElements.iterator();
49  		while (modelElementIt.hasNext()) {
50  			Object element = (Object) modelElementIt.next();
51  			//copies the class to the OCL model class
52  			if (type.isAssignableFrom(element.getClass())) {
53  				filtered.add(element);
54  			}
55  		}
56  		return filtered;
57  	}
58  }