001: /*
002: * $RCSfile: PropertyGeneratorImpl.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:57:01 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.util;
013:
014: import javax.media.jai.PropertyGenerator;
015: import javax.media.jai.RenderableOp;
016: import javax.media.jai.RenderedOp;
017:
018: /**
019: * This utility class provides an implementation of the
020: * <code>PropertyGenerator</code> interface that is suitable for
021: * extending. In addition to providing a no-arg constructor which
022: * passes the appropriate arrays, subclasses need only override the
023: * method <code>getProperty(String,Object)</code> in which the method
024: * <code>validate()</code> should be invoked as the first statement.
025: *
026: * @see PropertyGenerator
027: */
028: public abstract class PropertyGeneratorImpl implements
029: PropertyGenerator {
030:
031: private String[] propertyNames;
032:
033: private Class[] propertyClasses;
034:
035: private Class[] supportedOpClasses;
036:
037: /**
038: * Constructs a <code>PropertyGeneratorImpl</code>. All parameters are
039: * saved by reference.
040: *
041: * @param propertyNames Names of emitted properties.
042: * @param propertyClasses Classes of emitted properties.
043: * @param supportedOpClasses Classes of supported op nodes.
044: * @exception IllegalArgumentException if any of the array parameters
045: * is <code>null</code>.
046: * @exception IllegalArgumentException if any of the array parameters
047: * has length zero.
048: * @exception IllegalArgumentException if the lengths of the arrays
049: * <code>propertyNames</code> and <code>propertyClasses</code>
050: * are unequal.
051: */
052: protected PropertyGeneratorImpl(String[] propertyNames,
053: Class[] propertyClasses, Class[] supportedOpClasses) {
054: if (propertyNames == null || propertyClasses == null
055: || supportedOpClasses == null) {
056: throw new IllegalArgumentException(JaiI18N
057: .getString("PropertyGeneratorImpl0"));
058: } else if (propertyNames.length == 0
059: || propertyClasses.length == 0
060: || supportedOpClasses.length == 0) {
061: throw new IllegalArgumentException(JaiI18N
062: .getString("PropertyGeneratorImpl1"));
063: } else if (propertyNames.length != propertyClasses.length) {
064: throw new IllegalArgumentException(JaiI18N
065: .getString("PropertyGeneratorImpl2"));
066: }
067:
068: for (int i = 0; i < propertyClasses.length; i++) {
069: if (propertyClasses[i].isPrimitive()) {
070: throw new IllegalArgumentException(JaiI18N
071: .getString("PropertyGeneratorImpl4"));
072: }
073: }
074:
075: this .propertyNames = propertyNames;
076: this .propertyClasses = propertyClasses;
077: this .supportedOpClasses = supportedOpClasses;
078: }
079:
080: /**
081: * Returns an array of <code>String</code>s naming properties emitted
082: * by this property generator.
083: *
084: * @return an array of <code>String</code>s that may be passed as parameter
085: * names to the <code>getProperty()</code> method.
086: */
087: public String[] getPropertyNames() {
088: return propertyNames;
089: }
090:
091: /**
092: * Returns the class expected to be returned by a request for
093: * the property with the specified name. If this information
094: * is unavailable, <code>null</code> will be returned indicating
095: * that <code>getProperty(propertyName).getClass()</code> should
096: * be executed instead. A <code>null</code> value might
097: * be returned for example to prevent generating the value of
098: * a deferred property solely to obtain its class.
099: *
100: * @return The <code>Class</code> expected to be return by a
101: * request for the value of this property or <code>null</code>.
102: * @exception IllegalArgumentException if <code>propertyName</code>
103: * is <code>null</code>.
104: */
105: public Class getClass(String propertyName) {
106: if (propertyName == null) {
107: throw new IllegalArgumentException(JaiI18N
108: .getString("PropertyGeneratorImpl0"));
109: }
110:
111: // Linear search as there are likely few properties.
112: int numProperties = propertyNames.length;
113: for (int i = 0; i < numProperties; i++) {
114: if (propertyName.equalsIgnoreCase(propertyNames[i])) {
115: return propertyClasses[i];
116: }
117: }
118:
119: // XXX Should an IllegalArgumentException be thrown if this property
120: // is not emitted by this PropertyGenerator?
121: return null;
122: }
123:
124: /**
125: * Determines whether the specified <code>Object</code> will
126: * be recognized by <code>getProperty(String,Object)</code>.
127: *
128: * @exception IllegalArgumentException if <code>opNode</code>
129: * is <code>null</code>.
130: */
131: public boolean canGenerateProperties(Object opNode) {
132: if (opNode == null) {
133: throw new IllegalArgumentException(JaiI18N
134: .getString("PropertyGeneratorImpl0"));
135: }
136:
137: int numClasses = supportedOpClasses.length;
138: if (numClasses == 1) {
139: return supportedOpClasses[0].isInstance(opNode);
140: } else {
141: // Linear search as there are likely few supported classes.
142: for (int i = 0; i < numClasses; i++) {
143: if (supportedOpClasses[i].isInstance(opNode)) {
144: return true;
145: }
146: }
147: }
148:
149: return false;
150: }
151:
152: /**
153: * Computes the value of a property relative to an environment
154: * of pre-existing properties. The case of the supplied
155: * <code>String</code> is ignored.
156: *
157: * <p> In the case of an <code>OperationNode</code> in a chain of
158: * operations these properties may be emitted by the sources of the
159: * node in a chain or the parameters of that operation. The information
160: * requisite to compute the requested property must be available via the
161: * supplied <code>OperationNode</code>. It is legal to call
162: * <code>getProperty()</code> on the operation's sources.
163: *
164: * @param name the name of the property, as a <code>String</code>.
165: * @param op the <code>Object</code> from which properties will
166: * be generated.
167: * @return the value of the property, as an <code>Object</code> or the
168: * value <code>java.awt.Image.UndefinedProperty</code>.
169: * @exception IllegalArgumentException if <code>name</code> or
170: * <code>opNode</code> is <code>null</code>.
171: * @exception IllegalArgumentException if <code>opNode</code> is
172: * not an instance of a supported class for this method, i.e.,
173: * <code>canGenerateProperties(opNode)</code> returns
174: * <code>false</code>.
175: */
176: public abstract Object getProperty(String name, Object opNode);
177:
178: /**
179: * Computes the value of a property relative to an environment
180: * of pre-existing properties emitted by the sources of
181: * a <code>RenderedOp</code>, and the parameters of that operation.
182: *
183: * <p> The operation name, sources, and <code>ParameterBlock</code>
184: * of the <code>RenderedOp</code> being processed may be obtained by
185: * means of the <code>op.getOperationName</code>,
186: * <code>op.getSources()</code>, and <code>op.getParameterBlock()</code>
187: * methods. It is legal to call <code>getProperty()</code> on the
188: * operation's sources.
189: *
190: * @param name the name of the property, as a <code>String</code>.
191: * @param op the <code>RenderedOp</code> representing the operation.
192: * @return the value of the property, as an <code>Object</code> or the
193: * value <code>java.awt.Image.UndefinedProperty</code>.
194: *
195: * @deprecated as of Java(tm) Advanced Imaging 1.1. Use
196: * <code>getProperty(String,Object)</code> instead.
197: * @exception IllegalArgumentException if <code>name</code> or
198: * <code>op</code> is <code>null</code>.
199: */
200: public Object getProperty(String name, RenderedOp op) {
201: return getProperty(name, (Object) op);
202: }
203:
204: /**
205: * Computes the value of a property relative to an environment
206: * of pre-existing properties emitted by the sources of
207: * a <code>RenderableOp</code>, and the parameters of that operation.
208: *
209: * <p> The operation sources and <code>ParameterBlock</code> of the
210: * <code>RenderableOp</code> being processed may be obtained by
211: * means of the <code>op.getSources()</code> and
212: * <code>op.getParameterBlock()</code> methods. It is legal to call
213: * <code>getProperty()</code> on the operation's sources.
214: *
215: * @param name the name of the property, as a <code>String</code>.
216: * @param op the <code>RenderableOp</code> representing the operation.
217: * @return the value of the property, as an <code>Object</code> or the
218: * value <code>java.awt.Image.UndefinedProperty</code>.
219: * @exception IllegalArgumentException if <code>name</code> or
220: * <code>op</code> is <code>null</code>.
221: *
222: * @deprecated as of Java(tm) Advanced Imaging 1.1. Use
223: * <code>getProperty(String,Object)</code> instead.
224: */
225: public Object getProperty(String name, RenderableOp op) {
226: return getProperty(name, (Object) op);
227: }
228:
229: /**
230: * Throws an exception if the arguments are illegal for
231: * <code>getProperty(String,Object)</code>.
232: *
233: * @param name the name of the property, as a <code>String</code>.
234: * @param op the <code>Object</code> from which properties will
235: * be generated.
236: * @exception IllegalArgumentException if <code>name</code> or
237: * <code>opNode</code> is <code>null</code>.
238: * @exception IllegalArgumentException if <code>opNode</code> is
239: * not an instance of a supported class for this method, i.e.,
240: * <code>canGenerateProperties(opNode)</code> returns
241: * <code>false</code>.
242: */
243: protected void validate(String name, Object opNode) {
244: if (name == null) {
245: throw new IllegalArgumentException(JaiI18N
246: .getString("PropertyGeneratorImpl0"));
247: } else if (!canGenerateProperties(opNode)) {
248: throw new IllegalArgumentException(JaiI18N
249: .getString("PropertyGeneratorImpl3"));
250: }
251: }
252: }
|