1. /*
  2. * @(#)ParameterBlock.java 1.14 04/05/05
  3. *
  4. * Copyright 2004 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 <code>ParameterBlock</code> 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. <code>ParameterBlock</code> itself is merely a container and
  20. * performs no checking on source or parameter types.
  21. *
  22. * <p> All parameters in a <code>ParameterBlock</code> 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. * <code>ParameterBlock</code>s when this is inappropriate. For example, to create
  34. * a new <code>ParameterBlock</code> 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. * <code>ParameterBlock</code>, 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 <code>ParameterBlock</code> 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<Object> sources = new Vector<Object>();
  78. /** A Vector of non-source parameters, stored as arbitrary Objects. */
  79. protected Vector<Object> parameters = new Vector<Object>();
  80. /** A dummy constructor. */
  81. public ParameterBlock() {}
  82. /**
  83. * Constructs a <code>ParameterBlock</code> with a given Vector
  84. * of sources.
  85. * @param sources a <code>Vector</code> of source images
  86. */
  87. public ParameterBlock(Vector<Object> sources) {
  88. setSources(sources);
  89. }
  90. /**
  91. * Constructs a <code>ParameterBlock</code> with a given Vector of sources and
  92. * Vector of parameters.
  93. * @param sources a <code>Vector</code> of source images
  94. * @param parameters a <code>Vector</code> of parameters to be used in the
  95. * rendering operation
  96. */
  97. public ParameterBlock(Vector<Object> sources,
  98. Vector<Object> parameters)
  99. {
  100. setSources(sources);
  101. setParameters(parameters);
  102. }
  103. /**
  104. * Creates a shallow copy of a <code>ParameterBlock</code>. The source and
  105. * parameter Vectors are copied by reference -- additions or
  106. * changes will be visible to both versions.
  107. *
  108. * @return an Object clone of the <code>ParameterBlock</code>.
  109. */
  110. public Object shallowClone() {
  111. try {
  112. return super.clone();
  113. } catch (Exception e) {
  114. // We can't be here since we implement Cloneable.
  115. return null;
  116. }
  117. }
  118. /**
  119. * Creates a copy of a <code>ParameterBlock</code>. The source and parameter
  120. * Vectors are cloned, but the actual sources and parameters are
  121. * copied by reference. This allows modifications to the order
  122. * and number of sources and parameters in the clone to be invisible
  123. * to the original <code>ParameterBlock</code>. Changes to the shared sources or
  124. * parameters themselves will still be visible.
  125. *
  126. * @return an Object clone of the <code>ParameterBlock</code>.
  127. */
  128. public Object clone() {
  129. ParameterBlock theClone;
  130. try {
  131. theClone = (ParameterBlock) super.clone();
  132. } catch (Exception e) {
  133. // We can't be here since we implement Cloneable.
  134. return null;
  135. }
  136. if (sources != null) {
  137. theClone.setSources((Vector)sources.clone());
  138. }
  139. if (parameters != null) {
  140. theClone.setParameters((Vector)parameters.clone());
  141. }
  142. return (Object) theClone;
  143. }
  144. /**
  145. * Adds an image to end of the list of sources. The image is
  146. * stored as an object in order to allow new node types in the
  147. * future.
  148. *
  149. * @param source an image object to be stored in the source list.
  150. * @return a new <code>ParameterBlock</code> containing the specified
  151. * <code>source</code>.
  152. */
  153. public ParameterBlock addSource(Object source) {
  154. sources.addElement(source);
  155. return this;
  156. }
  157. /**
  158. * Returns a source as a general Object. The caller must cast it into
  159. * an appropriate type.
  160. *
  161. * @param index the index of the source to be returned.
  162. * @return an <code>Object</code> that represents the source located
  163. * at the specified index in the <code>sources</code>
  164. * <code>Vector</code>.
  165. * @see #setSource(Object, int)
  166. */
  167. public Object getSource(int index) {
  168. return sources.elementAt(index);
  169. }
  170. /**
  171. * Replaces an entry in the list of source with a new source.
  172. * If the index lies beyond the current source list,
  173. * the list is extended with nulls as needed.
  174. * @param source the specified source image
  175. * @param index the index into the <code>sources</code>
  176. * <code>Vector</code> at which to
  177. * insert the specified <code>source</code>
  178. * @return a new <code>ParameterBlock</code> that contains the
  179. * specified <code>source</code> at the specified
  180. * <code>index</code>.
  181. * @see #getSource(int)
  182. */
  183. public ParameterBlock setSource(Object source, int index) {
  184. int oldSize = sources.size();
  185. int newSize = index + 1;
  186. if (oldSize < newSize) {
  187. sources.setSize(newSize);
  188. }
  189. sources.setElementAt(source, index);
  190. return this;
  191. }
  192. /**
  193. * Returns a source as a <code>RenderedImage</code>. This method is
  194. * a convenience method.
  195. * An exception will be thrown if the source is not a RenderedImage.
  196. *
  197. * @param index the index of the source to be returned
  198. * @return a <code>RenderedImage</code> that represents the source
  199. * image that is at the specified index in the
  200. * <code>sources</code> <code>Vector</code>.
  201. */
  202. public RenderedImage getRenderedSource(int index) {
  203. return (RenderedImage) sources.elementAt(index);
  204. }
  205. /**
  206. * Returns a source as a RenderableImage. This method is a
  207. * convenience method.
  208. * An exception will be thrown if the sources is not a RenderableImage.
  209. *
  210. * @param index the index of the source to be returned
  211. * @return a <code>RenderableImage</code> that represents the source
  212. * image that is at the specified index in the
  213. * <code>sources</code> <code>Vector</code>.
  214. */
  215. public RenderableImage getRenderableSource(int index) {
  216. return (RenderableImage) sources.elementAt(index);
  217. }
  218. /**
  219. * Returns the number of source images.
  220. * @return the number of source images in the <code>sources</code>
  221. * <code>Vector</code>.
  222. */
  223. public int getNumSources() {
  224. return sources.size();
  225. }
  226. /**
  227. * Returns the entire Vector of sources.
  228. * @return the <code>sources</code> <code>Vector</code>.
  229. * @see #setSources(Vector)
  230. */
  231. public Vector<Object> getSources() {
  232. return sources;
  233. }
  234. /**
  235. * Sets the entire Vector of sources to a given Vector.
  236. * @param sources the <code>Vector</code> of source images
  237. * @see #getSources
  238. */
  239. public void setSources(Vector<Object> sources) {
  240. this.sources = sources;
  241. }
  242. /** Clears the list of source images. */
  243. public void removeSources() {
  244. sources = new Vector();
  245. }
  246. /**
  247. * Returns the number of parameters (not including source images).
  248. * @return the number of parameters in the <code>parameters</code>
  249. * <code>Vector</code>.
  250. */
  251. public int getNumParameters() {
  252. return parameters.size();
  253. }
  254. /**
  255. * Returns the entire Vector of parameters.
  256. * @return the <code>parameters</code> <code>Vector</code>.
  257. * @see #setParameters(Vector)
  258. */
  259. public Vector<Object> getParameters() {
  260. return parameters;
  261. }
  262. /**
  263. * Sets the entire Vector of parameters to a given Vector.
  264. * @param parameters the specified <code>Vector</code> of
  265. * parameters
  266. * @see #getParameters
  267. */
  268. public void setParameters(Vector<Object> parameters) {
  269. this.parameters = parameters;
  270. }
  271. /** Clears the list of parameters. */
  272. public void removeParameters() {
  273. parameters = new Vector();
  274. }
  275. /**
  276. * Adds an object to the list of parameters.
  277. * @param obj the <code>Object</code> to add to the
  278. * <code>parameters</code> <code>Vector</code>
  279. * @return a new <code>ParameterBlock</code> containing
  280. * the specified parameter.
  281. */
  282. public ParameterBlock add(Object obj) {
  283. parameters.addElement(obj);
  284. return this;
  285. }
  286. /**
  287. * Adds a Byte to the list of parameters.
  288. * @param b the byte to add to the
  289. * <code>parameters</code> <code>Vector</code>
  290. * @return a new <code>ParameterBlock</code> containing
  291. * the specified parameter.
  292. */
  293. public ParameterBlock add(byte b) {
  294. return add(new Byte(b));
  295. }
  296. /**
  297. * Adds a Character to the list of parameters.
  298. * @param c the char to add to the
  299. * <code>parameters</code> <code>Vector</code>
  300. * @return a new <code>ParameterBlock</code> containing
  301. * the specified parameter.
  302. */
  303. public ParameterBlock add(char c) {
  304. return add(new Character(c));
  305. }
  306. /**
  307. * Adds a Short to the list of parameters.
  308. * @param s the short to add to the
  309. * <code>parameters</code> <code>Vector</code>
  310. * @return a new <code>ParameterBlock</code> containing
  311. * the specified parameter.
  312. */
  313. public ParameterBlock add(short s) {
  314. return add(new Short(s));
  315. }
  316. /**
  317. * Adds a Integer to the list of parameters.
  318. * @param i the int to add to the
  319. * <code>parameters</code> <code>Vector</code>
  320. * @return a new <code>ParameterBlock</code> containing
  321. * the specified parameter.
  322. */
  323. public ParameterBlock add(int i) {
  324. return add(new Integer(i));
  325. }
  326. /**
  327. * Adds a Long to the list of parameters.
  328. * @param l the long to add to the
  329. * <code>parameters</code> <code>Vector</code>
  330. * @return a new <code>ParameterBlock</code> containing
  331. * the specified parameter.
  332. */
  333. public ParameterBlock add(long l) {
  334. return add(new Long(l));
  335. }
  336. /**
  337. * Adds a Float to the list of parameters.
  338. * @param f the float to add to the
  339. * <code>parameters</code> <code>Vector</code>
  340. * @return a new <code>ParameterBlock</code> containing
  341. * the specified parameter.
  342. */
  343. public ParameterBlock add(float f) {
  344. return add(new Float(f));
  345. }
  346. /**
  347. * Adds a Double to the list of parameters.
  348. * @param d the double to add to the
  349. * <code>parameters</code> <code>Vector</code>
  350. * @return a new <code>ParameterBlock</code> containing
  351. * the specified parameter.
  352. */
  353. public ParameterBlock add(double d) {
  354. return add(new Double(d));
  355. }
  356. /**
  357. * Replaces an Object in the list of parameters.
  358. * If the index lies beyond the current source list,
  359. * the list is extended with nulls as needed.
  360. * @param obj the parameter that replaces the
  361. * parameter at the specified index in the
  362. * <code>parameters</code> <code>Vector</code>
  363. * @param index the index of the parameter to be
  364. * replaced with the specified parameter
  365. * @return a new <code>ParameterBlock</code> containing
  366. * the specified parameter.
  367. */
  368. public ParameterBlock set(Object obj, int index) {
  369. int oldSize = parameters.size();
  370. int newSize = index + 1;
  371. if (oldSize < newSize) {
  372. parameters.setSize(newSize);
  373. }
  374. parameters.setElementAt(obj, index);
  375. return this;
  376. }
  377. /**
  378. * Replaces an Object in the list of parameters with a Byte.
  379. * If the index lies beyond the current source list,
  380. * the list is extended with nulls as needed.
  381. * @param b the parameter that replaces the
  382. * parameter at the specified index in the
  383. * <code>parameters</code> <code>Vector</code>
  384. * @param index the index of the parameter to be
  385. * replaced with the specified parameter
  386. * @return a new <code>ParameterBlock</code> containing
  387. * the specified parameter.
  388. */
  389. public ParameterBlock set(byte b, int index) {
  390. return set(new Byte(b), index);
  391. }
  392. /**
  393. * Replaces an Object in the list of parameters with a Character.
  394. * If the index lies beyond the current source list,
  395. * the list is extended with nulls as needed.
  396. * @param c the parameter that replaces the
  397. * parameter at the specified index in the
  398. * <code>parameters</code> <code>Vector</code>
  399. * @param index the index of the parameter to be
  400. * replaced with the specified parameter
  401. * @return a new <code>ParameterBlock</code> containing
  402. * the specified parameter.
  403. */
  404. public ParameterBlock set(char c, int index) {
  405. return set(new Character(c), index);
  406. }
  407. /**
  408. * Replaces an Object in the list of parameters with a Short.
  409. * If the index lies beyond the current source list,
  410. * the list is extended with nulls as needed.
  411. * @param s the parameter that replaces the
  412. * parameter at the specified index in the
  413. * <code>parameters</code> <code>Vector</code>
  414. * @param index the index of the parameter to be
  415. * replaced with the specified parameter
  416. * @return a new <code>ParameterBlock</code> containing
  417. * the specified parameter.
  418. */
  419. public ParameterBlock set(short s, int index) {
  420. return set(new Short(s), index);
  421. }
  422. /**
  423. * Replaces an Object in the list of parameters with an Integer.
  424. * If the index lies beyond the current source list,
  425. * the list is extended with nulls as needed.
  426. * @param i the parameter that replaces the
  427. * parameter at the specified index in the
  428. * <code>parameters</code> <code>Vector</code>
  429. * @param index the index of the parameter to be
  430. * replaced with the specified parameter
  431. * @return a new <code>ParameterBlock</code> containing
  432. * the specified parameter.
  433. */
  434. public ParameterBlock set(int i, int index) {
  435. return set(new Integer(i), index);
  436. }
  437. /**
  438. * Replaces an Object in the list of parameters with a Long.
  439. * If the index lies beyond the current source list,
  440. * the list is extended with nulls as needed.
  441. * @param l the parameter that replaces the
  442. * parameter at the specified index in the
  443. * <code>parameters</code> <code>Vector</code>
  444. * @param index the index of the parameter to be
  445. * replaced with the specified parameter
  446. * @return a new <code>ParameterBlock</code> containing
  447. * the specified parameter.
  448. */
  449. public ParameterBlock set(long l, int index) {
  450. return set(new Long(l), index);
  451. }
  452. /**
  453. * Replaces an Object in the list of parameters with a Float.
  454. * If the index lies beyond the current source list,
  455. * the list is extended with nulls as needed.
  456. * @param f the parameter that replaces the
  457. * parameter at the specified index in the
  458. * <code>parameters</code> <code>Vector</code>
  459. * @param index the index of the parameter to be
  460. * replaced with the specified parameter
  461. * @return a new <code>ParameterBlock</code> containing
  462. * the specified parameter.
  463. */
  464. public ParameterBlock set(float f, int index) {
  465. return set(new Float(f), index);
  466. }
  467. /**
  468. * Replaces an Object in the list of parameters with a Double.
  469. * If the index lies beyond the current source list,
  470. * the list is extended with nulls as needed.
  471. * @param d the parameter that replaces the
  472. * parameter at the specified index in the
  473. * <code>parameters</code> <code>Vector</code>
  474. * @param index the index of the parameter to be
  475. * replaced with the specified parameter
  476. * @return a new <code>ParameterBlock</code> containing
  477. * the specified parameter.
  478. */
  479. public ParameterBlock set(double d, int index) {
  480. return set(new Double(d), index);
  481. }
  482. /**
  483. * Gets a parameter as an object.
  484. * @param index the index of the parameter to get
  485. * @return an <code>Object</code> representing the
  486. * the parameter at the specified index
  487. * into the <code>parameters</code>
  488. * <code>Vector</code>.
  489. */
  490. public Object getObjectParameter(int index) {
  491. return parameters.elementAt(index);
  492. }
  493. /**
  494. * A convenience method to return a parameter as a byte. An
  495. * exception is thrown if the parameter is
  496. * <code>null</code> or not a <code>Byte</code>.
  497. *
  498. * @param index the index of the parameter to be returned.
  499. * @return the parameter at the specified index
  500. * as a <code>byte</code> value.
  501. * @throws ClassCastException if the parameter at the
  502. * specified index is not a <code>Byte</code>
  503. * @throws NullPointerException if the parameter at the specified
  504. * index is <code>null</code>
  505. * @throws ArrayIndexOutOfBoundsException if <code>index</code>
  506. * is negative or not less than the current size of this
  507. * <code>ParameterBlock</code> object
  508. */
  509. public byte getByteParameter(int index) {
  510. return ((Byte)parameters.elementAt(index)).byteValue();
  511. }
  512. /**
  513. * A convenience method to return a parameter as a char. An
  514. * exception is thrown if the parameter is
  515. * <code>null</code> or not a <code>Character</code>.
  516. *
  517. * @param index the index of the parameter to be returned.
  518. * @return the parameter at the specified index
  519. * as a <code>char</code> value.
  520. * @throws ClassCastException if the parameter at the
  521. * specified index is not a <code>Character</code>
  522. * @throws NullPointerException if the parameter at the specified
  523. * index is <code>null</code>
  524. * @throws ArrayIndexOutOfBoundsException if <code>index</code>
  525. * is negative or not less than the current size of this
  526. * <code>ParameterBlock</code> object
  527. */
  528. public char getCharParameter(int index) {
  529. return ((Character)parameters.elementAt(index)).charValue();
  530. }
  531. /**
  532. * A convenience method to return a parameter as a short. An
  533. * exception is thrown if the parameter is
  534. * <code>null</code> or not a <code>Short</code>.
  535. *
  536. * @param index the index of the parameter to be returned.
  537. * @return the parameter at the specified index
  538. * as a <code>short</code> value.
  539. * @throws ClassCastException if the parameter at the
  540. * specified index is not a <code>Short</code>
  541. * @throws NullPointerException if the parameter at the specified
  542. * index is <code>null</code>
  543. * @throws ArrayIndexOutOfBoundsException if <code>index</code>
  544. * is negative or not less than the current size of this
  545. * <code>ParameterBlock</code> object
  546. */
  547. public short getShortParameter(int index) {
  548. return ((Short)parameters.elementAt(index)).shortValue();
  549. }
  550. /**
  551. * A convenience method to return a parameter as an int. An
  552. * exception is thrown if the parameter is
  553. * <code>null</code> or not an <code>Integer</code>.
  554. *
  555. * @param index the index of the parameter to be returned.
  556. * @return the parameter at the specified index
  557. * as a <code>int</code> value.
  558. * @throws ClassCastException if the parameter at the
  559. * specified index is not a <code>Integer</code>
  560. * @throws NullPointerException if the parameter at the specified
  561. * index is <code>null</code>
  562. * @throws ArrayIndexOutOfBoundsException if <code>index</code>
  563. * is negative or not less than the current size of this
  564. * <code>ParameterBlock</code> object
  565. */
  566. public int getIntParameter(int index) {
  567. return ((Integer)parameters.elementAt(index)).intValue();
  568. }
  569. /**
  570. * A convenience method to return a parameter as a long. An
  571. * exception is thrown if the parameter is
  572. * <code>null</code> or not a <code>Long</code>.
  573. *
  574. * @param index the index of the parameter to be returned.
  575. * @return the parameter at the specified index
  576. * as a <code>long</code> value.
  577. * @throws ClassCastException if the parameter at the
  578. * specified index is not a <code>Long</code>
  579. * @throws NullPointerException if the parameter at the specified
  580. * index is <code>null</code>
  581. * @throws ArrayIndexOutOfBoundsException if <code>index</code>
  582. * is negative or not less than the current size of this
  583. * <code>ParameterBlock</code> object
  584. */
  585. public long getLongParameter(int index) {
  586. return ((Long)parameters.elementAt(index)).longValue();
  587. }
  588. /**
  589. * A convenience method to return a parameter as a float. An
  590. * exception is thrown if the parameter is
  591. * <code>null</code> or not a <code>Float</code>.
  592. *
  593. * @param index the index of the parameter to be returned.
  594. * @return the parameter at the specified index
  595. * as a <code>float</code> value.
  596. * @throws ClassCastException if the parameter at the
  597. * specified index is not a <code>Float</code>
  598. * @throws NullPointerException if the parameter at the specified
  599. * index is <code>null</code>
  600. * @throws ArrayIndexOutOfBoundsException if <code>index</code>
  601. * is negative or not less than the current size of this
  602. * <code>ParameterBlock</code> object
  603. */
  604. public float getFloatParameter(int index) {
  605. return ((Float)parameters.elementAt(index)).floatValue();
  606. }
  607. /**
  608. * A convenience method to return a parameter as a double. An
  609. * exception is thrown if the parameter is
  610. * <code>null</code> or not a <code>Double</code>.
  611. *
  612. * @param index the index of the parameter to be returned.
  613. * @return the parameter at the specified index
  614. * as a <code>double</code> value.
  615. * @throws ClassCastException if the parameter at the
  616. * specified index is not a <code>Double</code>
  617. * @throws NullPointerException if the parameter at the specified
  618. * index is <code>null</code>
  619. * @throws ArrayIndexOutOfBoundsException if <code>index</code>
  620. * is negative or not less than the current size of this
  621. * <code>ParameterBlock</code> object
  622. */
  623. public double getDoubleParameter(int index) {
  624. return ((Double)parameters.elementAt(index)).doubleValue();
  625. }
  626. /**
  627. * Returns an array of Class objects describing the types
  628. * of the parameters.
  629. * @return an array of <code>Class</code> objects.
  630. */
  631. public Class [] getParamClasses() {
  632. int numParams = getNumParameters();
  633. Class [] classes = new Class[numParams];
  634. int i;
  635. for (i = 0; i < numParams; i++) {
  636. Object obj = getObjectParameter(i);
  637. if (obj instanceof Byte) {
  638. classes[i] = byte.class;
  639. } else if (obj instanceof Character) {
  640. classes[i] = char.class;
  641. } else if (obj instanceof Short) {
  642. classes[i] = short.class;
  643. } else if (obj instanceof Integer) {
  644. classes[i] = int.class;
  645. } else if (obj instanceof Long) {
  646. classes[i] = long.class;
  647. } else if (obj instanceof Float) {
  648. classes[i] = float.class;
  649. } else if (obj instanceof Double) {
  650. classes[i] = double.class;
  651. } else {
  652. classes[i] = obj.getClass();
  653. }
  654. }
  655. return classes;
  656. }
  657. }