1. /*
  2. * @(#)ParameterBlock.java 1.5 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.image.renderable;
  8. import java.awt.image.RenderedImage;
  9. import java.io.Serializable;
  10. import java.util.Vector;
  11. /**
  12. * A ParameterBlock encapsulates all the information about sources and
  13. * parameters (Objects) required by a RenderableImageOp, or other
  14. * classes that process images.
  15. *
  16. * <p> Although it is possible to place arbitrary objects in the
  17. * source Vector, users of this class may impose semantic constraints
  18. * such as requiring all sources to be RenderedImages or
  19. * RenderableImage. ParameterBlock itself is merely a container and
  20. * performs no checking on source or parameter types.
  21. *
  22. * <p> All parameters in a ParameterBlock are objects; convenience
  23. * add and set methods are available that take arguments of base type and
  24. * construct the appropriate subclass of Number (such as
  25. * Integer or Float). Corresponding get methods perform a
  26. * downward cast and have return values of base type; an exception
  27. * will be thrown if the stored values do not have the correct type.
  28. * There is no way to distinguish between the results of
  29. * "short s; add(s)" and "add(new Short(s))".
  30. *
  31. * <p> Note that the get and set methods operate on references.
  32. * Therefore, one must be careful not to share references between
  33. * ParameterBlocks when this is inappropriate. For example, to create
  34. * a new ParameterBlock that is equal to an old one except for an
  35. * added source, one might be tempted to write:
  36. *
  37. * <pre>
  38. * ParameterBlock addSource(ParameterBlock pb, RenderableImage im) {
  39. * ParameterBlock pb1 = new ParameterBlock(pb.getSources());
  40. * pb1.addSource(im);
  41. * return pb1;
  42. * }
  43. * </pre>
  44. *
  45. * <p> This code will have the side effect of altering the original
  46. * ParameterBlock, since the getSources operation returned a reference
  47. * to its source Vector. Both pb and pb1 share their source Vector,
  48. * and a change in either is visible to both.
  49. *
  50. * <p> A correct way to write the addSource function is to clone
  51. * the source Vector:
  52. *
  53. * <pre>
  54. * ParameterBlock addSource (ParameterBlock pb, RenderableImage im) {
  55. * ParameterBlock pb1 = new ParameterBlock(pb.getSources().clone());
  56. * pb1.addSource(im);
  57. * return pb1;
  58. * }
  59. * </pre>
  60. *
  61. * <p> The clone method of ParameterBlock has been defined to
  62. * perform a clone of both the source and parameter Vectors for
  63. * this reason. A standard, shallow clone is available as
  64. * shallowClone.
  65. *
  66. * <p> The addSource, setSource, add, and set methods are
  67. * defined to return 'this' after adding their argument. This allows
  68. * use of syntax like:
  69. *
  70. * <pre>
  71. * ParameterBlock pb = new ParameterBlock();
  72. * op = new RenderableImageOp("operation", pb.add(arg1).add(arg2));
  73. * </pre>
  74. * */
  75. public class ParameterBlock implements Cloneable, Serializable {
  76. /** A Vector of sources, stored as arbitrary Objects. */
  77. protected Vector sources = new Vector();
  78. /** A Vector of non-source parameters, stored as arbitrary Objects. */
  79. protected Vector parameters = new Vector();
  80. /** A dummy constructor. */
  81. public ParameterBlock() {}
  82. /** Constructs a ParameterBlock with a given Vector of sources. */
  83. public ParameterBlock(Vector sources) {
  84. setSources(sources);
  85. }
  86. /**
  87. * Constructs a ParameterBlock with a given Vector of sources and
  88. * Vector of parameters.
  89. */
  90. public ParameterBlock(Vector sources, Vector parameters) {
  91. setSources(sources);
  92. setParameters(parameters);
  93. }
  94. /**
  95. * Creates a shallow copy of a ParameterBlock. The source and
  96. * parameter Vectors are copied by reference -- additions or
  97. * changes will be visible to both versions.
  98. *
  99. * @return an Object clone of the ParameterBlock.
  100. */
  101. public Object shallowClone() {
  102. try {
  103. return super.clone();
  104. } catch (Exception e) {
  105. // We can't be here since we implement Cloneable.
  106. return null;
  107. }
  108. }
  109. /**
  110. * Creates a copy of a ParameterBlock. The source and parameter
  111. * Vectors are cloned, but the actual sources and parameters are
  112. * copied by reference. This allows modifications to the order
  113. * and number of sources and parameters in the clone to be invisible
  114. * to the original ParameterBlock. Changes to the shared sources or
  115. * parameters themselves will still be visible.
  116. *
  117. * @return an Object clone of the ParameterBlock.
  118. */
  119. public Object clone() {
  120. ParameterBlock theClone;
  121. try {
  122. theClone = (ParameterBlock) super.clone();
  123. } catch (Exception e) {
  124. // We can't be here since we implement Cloneable.
  125. return null;
  126. }
  127. if (sources != null) {
  128. theClone.setSources((Vector)sources.clone());
  129. }
  130. if (parameters != null) {
  131. theClone.setParameters((Vector)parameters.clone());
  132. }
  133. return (Object) theClone;
  134. }
  135. /**
  136. * Adds an image to end of the list of sources. The image is
  137. * stored as an object in order to allow new node types in the
  138. * future.
  139. *
  140. * @param source an image object to be stored in the source list.
  141. */
  142. public ParameterBlock addSource(Object source) {
  143. sources.addElement(source);
  144. return this;
  145. }
  146. /**
  147. * Returns a source as a general Object. The caller must cast it into
  148. * an appropriate type.
  149. *
  150. * @param index the index of the source to be returned.
  151. */
  152. public Object getSource(int index) {
  153. return sources.elementAt(index);
  154. }
  155. /**
  156. * Replaces an entry in the list of source with a new source.
  157. * If the index lies beyond the current source list,
  158. * the list is extended with nulls as needed.
  159. */
  160. public ParameterBlock setSource(Object source, int index) {
  161. int oldSize = sources.size();
  162. int newSize = index + 1;
  163. if (oldSize < newSize) {
  164. sources.setSize(newSize);
  165. }
  166. sources.setElementAt(source, index);
  167. return this;
  168. }
  169. /**
  170. * A convenience method to return a source as a RenderedImage.
  171. * An exception will be thrown if the sources is not a RenderedImage.
  172. *
  173. * @param index the index of the source to be returned.
  174. */
  175. public RenderedImage getRenderedSource(int index) {
  176. return (RenderedImage) sources.elementAt(index);
  177. }
  178. /**
  179. * A convenience method to return a source as a RenderableImage.
  180. * An exception will be thrown if the sources is not a RenderableImage.
  181. *
  182. * @param index the index of the source to be returned.
  183. */
  184. public RenderableImage getRenderableSource(int index) {
  185. return (RenderableImage) sources.elementAt(index);
  186. }
  187. /** Returns the number of source images. */
  188. public int getNumSources() {
  189. return sources.size();
  190. }
  191. /** Returns the entire Vector of sources. */
  192. public Vector getSources() {
  193. return sources;
  194. }
  195. /** Sets the entire Vector of sources to a given Vector. */
  196. public void setSources(Vector sources) {
  197. this.sources = sources;
  198. }
  199. /** Clears the list of source images. */
  200. public void removeSources() {
  201. sources = new Vector();
  202. }
  203. /** Returns the number of parameters (not including source images). */
  204. public int getNumParameters() {
  205. return parameters.size();
  206. }
  207. /** Returns the entire Vector of parameters. */
  208. public Vector getParameters() {
  209. return parameters;
  210. }
  211. /** Sets the entire Vector of parameters to a given Vector. */
  212. public void setParameters(Vector parameters) {
  213. this.parameters = parameters;
  214. }
  215. /** Clears the list of parameters. */
  216. public void removeParameters() {
  217. parameters = new Vector();
  218. }
  219. /** Adds an object to the list of parameters. */
  220. public ParameterBlock add(Object obj) {
  221. parameters.addElement(obj);
  222. return this;
  223. }
  224. /** Adds a Byte to the list of parameters. */
  225. public ParameterBlock add(byte b) {
  226. return add(new Byte(b));
  227. }
  228. /** Adds a Character to the list of parameters. */
  229. public ParameterBlock add(char c) {
  230. return add(new Character(c));
  231. }
  232. /** Adds a Short to the list of parameters. */
  233. public ParameterBlock add(short s) {
  234. return add(new Short(s));
  235. }
  236. /** Adds a Integer to the list of parameters. */
  237. public ParameterBlock add(int i) {
  238. return add(new Integer(i));
  239. }
  240. /** Adds a Long to the list of parameters. */
  241. public ParameterBlock add(long l) {
  242. return add(new Long(l));
  243. }
  244. /** Adds a Float to the list of parameters. */
  245. public ParameterBlock add(float f) {
  246. return add(new Float(f));
  247. }
  248. /** Adds a Double to the list of parameters. */
  249. public ParameterBlock add(double d) {
  250. return add(new Double(d));
  251. }
  252. /**
  253. * Replaces an Object in the list of parameters.
  254. * If the index lies beyond the current source list,
  255. * the list is extended with nulls as needed.
  256. */
  257. public ParameterBlock set(Object obj, int index) {
  258. int oldSize = parameters.size();
  259. int newSize = index + 1;
  260. if (oldSize < newSize) {
  261. parameters.setSize(newSize);
  262. }
  263. parameters.setElementAt(obj, index);
  264. return this;
  265. }
  266. /**
  267. * Replaces an Object in the list of parameters with a Byte.
  268. * If the index lies beyond the current source list,
  269. * the list is extended with nulls as needed.
  270. */
  271. public ParameterBlock set(byte b, int index) {
  272. return set(new Byte(b), index);
  273. }
  274. /**
  275. * Replaces an Object in the list of parameters with a Character.
  276. * If the index lies beyond the current source list,
  277. * the list is extended with nulls as needed.
  278. */
  279. public ParameterBlock set(char c, int index) {
  280. return set(new Character(c), index);
  281. }
  282. /**
  283. * Replaces an Object in the list of parameters with a Short.
  284. * If the index lies beyond the current source list,
  285. * the list is extended with nulls as needed.
  286. */
  287. public ParameterBlock set(short s, int index) {
  288. return set(new Short(s), index);
  289. }
  290. /**
  291. * Replaces an Object in the list of parameters with an Integer.
  292. * If the index lies beyond the current source list,
  293. * the list is extended with nulls as needed.
  294. */
  295. public ParameterBlock set(int i, int index) {
  296. return set(new Integer(i), index);
  297. }
  298. /**
  299. * Replaces an Object in the list of parameters with a Long.
  300. * If the index lies beyond the current source list,
  301. * the list is extended with nulls as needed.
  302. */
  303. public ParameterBlock set(long l, int index) {
  304. return set(new Long(l), index);
  305. }
  306. /**
  307. * Replaces an Object in the list of parameters with a Float.
  308. * If the index lies beyond the current source list,
  309. * the list is extended with nulls as needed.
  310. */
  311. public ParameterBlock set(float f, int index) {
  312. return set(new Float(f), index);
  313. }
  314. /**
  315. * Replaces an Object in the list of parameters with a Double.
  316. * If the index lies beyond the current source list,
  317. * the list is extended with nulls as needed.
  318. */
  319. public ParameterBlock set(double d, int index) {
  320. return set(new Double(d), index);
  321. }
  322. /**
  323. * Gets a parameter as an object.
  324. */
  325. public Object getObjectParameter(int index) {
  326. return parameters.elementAt(index);
  327. }
  328. /**
  329. * A convenience method to return a parameter as a byte. An
  330. * exception will be thrown if the parameter is null or not a
  331. * Byte.
  332. *
  333. * @param index the index of the parameter to be returned.
  334. */
  335. public byte getByteParameter(int index) {
  336. return ((Byte)parameters.elementAt(index)).byteValue();
  337. }
  338. /**
  339. * A convenience method to return a parameter as a char. An
  340. * exception will be thrown if the parameter is null or not a
  341. * Character.
  342. *
  343. * @param index the index of the parameter to be returned.
  344. */
  345. public char getCharParameter(int index) {
  346. return ((Character)parameters.elementAt(index)).charValue();
  347. }
  348. /**
  349. * A convenience method to return a parameter as a short. An
  350. * exception will be thrown if the parameter is null or not a
  351. * Short.
  352. *
  353. * @param index the index of the parameter to be returned.
  354. */
  355. public short getShortParameter(int index) {
  356. return ((Short)parameters.elementAt(index)).shortValue();
  357. }
  358. /**
  359. * A convenience method to return a parameter as an int. An
  360. * exception will be thrown if the parameter is null or not an
  361. * Integer.
  362. *
  363. * @param index the index of the parameter to be returned.
  364. */
  365. public int getIntParameter(int index) {
  366. return ((Integer)parameters.elementAt(index)).intValue();
  367. }
  368. /**
  369. * A convenience method to return a parameter as a long. An
  370. * exception will be thrown if the parameter is null or not a
  371. * Long.
  372. *
  373. * @param index the index of the parameter to be returned.
  374. */
  375. public long getLongParameter(int index) {
  376. return ((Long)parameters.elementAt(index)).longValue();
  377. }
  378. /**
  379. * A convenience method to return a parameter as a float. An
  380. * exception will be thrown if the parameter is null or not a
  381. * Float.
  382. *
  383. * @param index the index of the parameter to be returned.
  384. */
  385. public float getFloatParameter(int index) {
  386. return ((Float)parameters.elementAt(index)).floatValue();
  387. }
  388. /**
  389. * A convenience method to return a parameter as a double. An
  390. * exception will be thrown if the parameter is null or not a
  391. * Double.
  392. *
  393. * @param index the index of the parameter to be returned.
  394. */
  395. public double getDoubleParameter(int index) {
  396. return ((Double)parameters.elementAt(index)).doubleValue();
  397. }
  398. /**
  399. * Returns an array of Class objects describing the types
  400. * of the parameters.
  401. */
  402. public Class [] getParamClasses() {
  403. int numParams = getNumParameters();
  404. Class [] classes = new Class[numParams];
  405. int i;
  406. for (i = 0; i < numParams; i++) {
  407. Object obj = getObjectParameter(i);
  408. if (obj instanceof Byte) {
  409. classes[i] = byte.class;
  410. } else if (obj instanceof Character) {
  411. classes[i] = char.class;
  412. } else if (obj instanceof Short) {
  413. classes[i] = short.class;
  414. } else if (obj instanceof Integer) {
  415. classes[i] = int.class;
  416. } else if (obj instanceof Long) {
  417. classes[i] = long.class;
  418. } else if (obj instanceof Float) {
  419. classes[i] = float.class;
  420. } else if (obj instanceof Double) {
  421. classes[i] = double.class;
  422. } else {
  423. classes[i] = obj.getClass();
  424. }
  425. }
  426. return classes;
  427. }
  428. }