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
19 /***
20 * Utility functions useful for implementing interfaces in the
21 * <code>composite</code> package.
22 *
23 * @author Matt Sgarlata
24 * @since Dec 28, 2004
25 */
26 public abstract class CompositeImplementationUtils {
27
28 private CompositeImplementationUtils() { }
29
30 // public static void testArguments(Object composite, String componentProperty) {
31 // if (composite == null) {
32 // throw new SpecializationException("You must supply a composite");
33 // }
34 // if (ObjectUtils.isEmpty(componentProperty)) {
35 // throw new SpecializationException(
36 // "You must supply the name of the property which holds the components");
37 // }
38 // }
39 //
40 // public static boolean isExposableAsType(Object candidate, Class type) throws CompositeException {
41 // if (candidate == null || type == null) {
42 // throw new CompositeException("You must supply non-null arguments to " + CompositeImplementationUtils.class.getName() + ".isExposableAsType");
43 // }
44 //
45 // if (type.isAssignableFrom(candidate.getClass())) {
46 // return true;
47 // }
48 // else if (candidate instanceof SpecializableComposite &&
49 // ((SpecializableComposite) candidate).isSpecializable(type)) {
50 // return true;
51 // }
52 //
53 // return false;
54 // }
55 //
56 // public static Object exposeAsType(Object object, Class type) throws CompositeException {
57 // if (type == null) {
58 // throw new CompositeException("You must specify the type you would like to have exposed");
59 // }
60 //
61 // if (object == null) {
62 // return null;
63 // }
64 //
65 // if (type.isAssignableFrom(object.getClass())) {
66 // return object;
67 // }
68 // else if (object instanceof SpecializableComposite) {
69 // SpecializableComposite dc = (SpecializableComposite) object;
70 // return dc.specialize(type);
71 // }
72 // else {
73 // throw new CompositeException("Cannot expose "
74 // + ObjectUtils.getObjectDescription(object) + " as type "
75 // + ObjectUtils.getObjectDescription(type));
76 // }
77 // }
78 //
79 // /***
80 // * Performs basic validity checks on a SimpleComposite, and throws a
81 // * <code>CompositeException</code> if the SimpleComposite is in an invalid
82 // * state.
83 // *
84 // * @param composite
85 // * the composite to be checked
86 // * @throws CompositeException
87 // * if the internal state of the composite is invalid
88 // */
89 // public static void checkComposite(SimpleComposite composite) throws CompositeException {
90 // if (composite == null) {
91 // throw new CompositeException(
92 // "To check the components of a composite, a composite must be specified");
93 // }
94 // Component[] components = composite.getComponents();
95 // if (ObjectUtils.isEmpty(components)) {
96 // throw new CompositeException(
97 // "You must initialize the components property before using "
98 // + ObjectUtils.getObjectDescription(composite));
99 // }
100 // for (int i = 0; i < components.length; i++) {
101 // if (components[i] == null) {
102 // throw new CompositeException(
103 // "No component in the components property can be null, but the component at index "
104 // + i + " is null");
105 // }
106 // if (composite instanceof StrictlyTypedComposite) {
107 // StrictlyTypedComposite stc = (StrictlyTypedComposite) composite;
108 // Class componentType = stc.getComponentType();
109 // if (!(componentType.isAssignableFrom(components[i].getClass()))) {
110 // throw new CompositeException(
111 // "The component at index "
112 // + i
113 // + ", "
114 // + ObjectUtils.getObjectDescription(components[i])
115 // + ", does not implement "
116 // + ObjectUtils.getObjectDescription(stc.getComponentType()));
117 // }
118 // }
119 // }
120 // }
121 //
122 // /***
123 // * Picks out the components from <code>components</code> that implement
124 // * are of the given <code>componentType</code>.
125 // *
126 // * @param components
127 // * the components to chose from
128 // * @param componentType
129 // * the type of component to search for
130 // * @return an array of runtime type <code>componentType</code> that
131 // * contains the components from <code>components</code> that
132 // * implement the given <code>componentType</code>.
133 // */
134 // public static Component[] getComponentsOfType(Component[] components,
135 // Class componentType) {
136 // List list = new ArrayList();
137 // for (int i=0; i<components.length; i++) {
138 // if (components[i] != null &&
139 // componentType.isAssignableFrom(components[i].getClass())) {
140 // list.add(components[i]);
141 // }
142 // }
143 // Object[] array = (Object[]) ClassUtils.createArray(componentType, list.size());
144 // return (Component[]) list.toArray(array);
145 // }
146
147 }