1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Igor V. Stolyarov
19 * @version $Revision$
20 */
21
22package java.awt.image.renderable;
23
24import java.awt.image.RenderedImage;
25import java.io.Serializable;
26import java.util.Vector;
27
28/**
29 * The class ParameterBlock groups an indexed set of parameter data with a set
30 * of renderable (source) images. The mapping between the indexed parameters and
31 * their property names is provided by a {@link ContextualRenderedImageFactory}.
32 *
33 * @since Android 1.0
34 */
35public class ParameterBlock implements Cloneable, Serializable {
36
37    /**
38     * The Constant serialVersionUID.
39     */
40    private static final long serialVersionUID = -7577115551785240750L;
41
42    /**
43     * The sources (renderable images).
44     */
45    protected Vector<Object> sources = new Vector<Object>();
46
47    /**
48     * The parameters.
49     */
50    protected Vector<Object> parameters = new Vector<Object>();
51
52    /**
53     * Instantiates a new parameter block.
54     *
55     * @param sources
56     *            the vector of source images.
57     * @param parameters
58     *            the vector of parameters.
59     */
60    public ParameterBlock(Vector<Object> sources, Vector<Object> parameters) {
61        setSources(sources);
62        setParameters(parameters);
63    }
64
65    /**
66     * Instantiates a new parameter block with no parameters.
67     *
68     * @param sources
69     *            the vector of source images.
70     */
71    public ParameterBlock(Vector<Object> sources) {
72        setSources(sources);
73    }
74
75    /**
76     * Instantiates a new parameter block with no image or parameter vectors.
77     */
78    public ParameterBlock() {
79    }
80
81    /**
82     * Sets the source image at the specified index.
83     *
84     * @param source
85     *            the source image.
86     * @param index
87     *            the index where the source will be placed.
88     * @return this parameter block.
89     */
90    public ParameterBlock setSource(Object source, int index) {
91        if (sources.size() < index + 1) {
92            sources.setSize(index + 1);
93        }
94        sources.setElementAt(source, index);
95        return this;
96    }
97
98    /**
99     * Sets the parameter value object at the specified index.
100     *
101     * @param obj
102     *            the parameter value to place at the desired index.
103     * @param index
104     *            the index where the object is to be placed in the vector of
105     *            parameters.
106     * @return this parameter block.
107     */
108    public ParameterBlock set(Object obj, int index) {
109        if (parameters.size() < index + 1) {
110            parameters.setSize(index + 1);
111        }
112        parameters.setElementAt(obj, index);
113        return this;
114    }
115
116    /**
117     * Adds a source to the vector of sources.
118     *
119     * @param source
120     *            the source to add.
121     * @return this parameter block.
122     */
123    public ParameterBlock addSource(Object source) {
124        sources.addElement(source);
125        return this;
126    }
127
128    /**
129     * Adds the object to the vector of parameter values
130     *
131     * @param obj
132     *            the obj to add.
133     * @return this parameter block.
134     */
135    public ParameterBlock add(Object obj) {
136        parameters.addElement(obj);
137        return this;
138    }
139
140    /**
141     * Sets the vector of sources, replacing the existing vector of sources, if
142     * any.
143     *
144     * @param sources
145     *            the new sources.
146     */
147    public void setSources(Vector<Object> sources) {
148        this.sources = sources;
149    }
150
151    /**
152     * Sets the vector of parameters, replacing the existing vector of
153     * parameters, if any.
154     *
155     * @param parameters
156     *            the new parameters.
157     */
158    public void setParameters(Vector<Object> parameters) {
159        this.parameters = parameters;
160    }
161
162    /**
163     * Gets the vector of sources.
164     *
165     * @return the sources.
166     */
167    public Vector<Object> getSources() {
168        return sources;
169    }
170
171    /**
172     * Gets the vector of parameters.
173     *
174     * @return the parameters.
175     */
176    public Vector<Object> getParameters() {
177        return parameters;
178    }
179
180    /**
181     * Gets the source at the specified index.
182     *
183     * @param index
184     *            the index.
185     * @return the source object found at the specified index.
186     */
187    public Object getSource(int index) {
188        return sources.elementAt(index);
189    }
190
191    /**
192     * Gets the object parameter found at the specified index.
193     *
194     * @param index
195     *            the index.
196     * @return the parameter object found at the specified index.
197     */
198    public Object getObjectParameter(int index) {
199        return parameters.elementAt(index);
200    }
201
202    /**
203     * Shallow clone (clones using the superclass clone method).
204     *
205     * @return the clone of this object.
206     */
207    public Object shallowClone() {
208        try {
209            return super.clone();
210        } catch (Exception e) {
211            return null;
212        }
213    }
214
215    /**
216     * Returns a copy of this ParameterBlock instance.
217     *
218     * @return the identical copy of this instance.
219     */
220    @SuppressWarnings("unchecked")
221    @Override
222    public Object clone() {
223        ParameterBlock replica;
224        try {
225            replica = (ParameterBlock)super.clone();
226        } catch (Exception e) {
227            return null;
228        }
229        if (sources != null) {
230            replica.setSources((Vector<Object>)(sources.clone()));
231        }
232        if (parameters != null) {
233            replica.setParameters((Vector<Object>)(parameters.clone()));
234        }
235        return replica;
236    }
237
238    /**
239     * Gets an array of classes corresponding to all of the parameter values
240     * found in the array of parameters, in order.
241     *
242     * @return the parameter classes.
243     */
244    public Class[] getParamClasses() {
245        int count = parameters.size();
246        Class paramClasses[] = new Class[count];
247
248        for (int i = 0; i < count; i++) {
249            paramClasses[i] = parameters.elementAt(i).getClass();
250        }
251        return paramClasses;
252    }
253
254    /**
255     * Gets the renderable source image found at the specified index in the
256     * source array.
257     *
258     * @param index
259     *            the index.
260     * @return the renderable source image.
261     */
262    public RenderableImage getRenderableSource(int index) {
263        return (RenderableImage)sources.elementAt(index);
264    }
265
266    /**
267     * Wraps the short value in a Short and places it in the parameter block at
268     * the specified index.
269     *
270     * @param s
271     *            the short value of the parameter.
272     * @param index
273     *            the index.
274     * @return this parameter block.
275     */
276    public ParameterBlock set(short s, int index) {
277        return set(new Short(s), index);
278    }
279
280    /**
281     * Wraps the short value in a Short and adds it to the parameter block.
282     *
283     * @param s
284     *            the short value of the parameter.
285     * @return this parameter block.
286     */
287    public ParameterBlock add(short s) {
288        return add(new Short(s));
289    }
290
291    /**
292     * Wraps the long value in a Long and places it in the parameter block at
293     * the specified index.
294     *
295     * @param l
296     *            the long value of the parameter.
297     * @param index
298     *            the index.
299     * @return this parameter block.
300     */
301    public ParameterBlock set(long l, int index) {
302        return set(new Long(l), index);
303    }
304
305    /**
306     * Wraps the long value in a Long and adds it to the parameter block.
307     *
308     * @param l
309     *            the long value of the parameter.
310     * @return this parameter block.
311     */
312    public ParameterBlock add(long l) {
313        return add(new Long(l));
314    }
315
316    /**
317     * Wraps the integer value in an Integer and places it in the parameter
318     * block at the specified index.
319     *
320     * @param i
321     *            the integer value of the parameter.
322     * @param index
323     *            the index.
324     * @return this parameter block.
325     */
326    public ParameterBlock set(int i, int index) {
327        return set(new Integer(i), index);
328    }
329
330    /**
331     * Wraps the integer value in an Integer and adds it to the parameter block.
332     *
333     * @param i
334     *            the integer value of the parameter.
335     * @return this parameter block.
336     */
337    public ParameterBlock add(int i) {
338        return add(new Integer(i));
339    }
340
341    /**
342     * Wraps the float value in a Float and places it in the parameter block at
343     * the specified index.
344     *
345     * @param f
346     *            the float value of the parameter.
347     * @param index
348     *            the index.
349     * @return this parameter block.
350     */
351    public ParameterBlock set(float f, int index) {
352        return set(new Float(f), index);
353    }
354
355    /**
356     * Wraps the float value in a Float and adds it to the parameter block.
357     *
358     * @param f
359     *            the float value of the parameter.
360     * @return this parameter block.
361     */
362    public ParameterBlock add(float f) {
363        return add(new Float(f));
364    }
365
366    /**
367     * Wraps the double value in a Double and places it in the parameter block
368     * at the specified index.
369     *
370     * @param d
371     *            the double value of the parameter.
372     * @param index
373     *            the index.
374     * @return this parameter block.
375     */
376    public ParameterBlock set(double d, int index) {
377        return set(new Double(d), index);
378    }
379
380    /**
381     * Wraps the double value in a Double and adds it to the parameter block.
382     *
383     * @param d
384     *            the double value of the parameter.
385     * @return this parameter block.
386     */
387    public ParameterBlock add(double d) {
388        return add(new Double(d));
389    }
390
391    /**
392     * Wraps the char value in a Character and places it in the parameter block
393     * at the specified index.
394     *
395     * @param c
396     *            the char value of the parameter.
397     * @param index
398     *            the index.
399     * @return this parameter block.
400     */
401    public ParameterBlock set(char c, int index) {
402        return set(new Character(c), index);
403    }
404
405    /**
406     * Wraps the char value in a Character and adds it to the parameter block.
407     *
408     * @param c
409     *            the char value of the parameter.
410     * @return this parameter block.
411     */
412    public ParameterBlock add(char c) {
413        return add(new Character(c));
414    }
415
416    /**
417     * Wraps the byte value in a Byte and places it in the parameter block at
418     * the specified index.
419     *
420     * @param b
421     *            the byte value of the parameter.
422     * @param index
423     *            the index.
424     * @return this parameter block.
425     */
426    public ParameterBlock set(byte b, int index) {
427        return set(new Byte(b), index);
428    }
429
430    /**
431     * Wraps the byte value in a Byte and adds it to the parameter block.
432     *
433     * @param b
434     *            the byte value of the parameter.
435     * @return the parameter block.
436     */
437    public ParameterBlock add(byte b) {
438        return add(new Byte(b));
439    }
440
441    /**
442     * Gets the RenderedImage at the specified index from the vector of source
443     * images.
444     *
445     * @param index
446     *            the index.
447     * @return the rendered image.
448     */
449    public RenderedImage getRenderedSource(int index) {
450        return (RenderedImage)sources.elementAt(index);
451    }
452
453    /**
454     * Gets the short-valued parameter found at the desired index in the vector
455     * of parameter values.
456     *
457     * @param index
458     *            the index.
459     * @return the short parameter.
460     */
461    public short getShortParameter(int index) {
462        return ((Short)parameters.elementAt(index)).shortValue();
463    }
464
465    /**
466     * Gets the long-valued parameter found at the desired index in the vector
467     * of parameter values.
468     *
469     * @param index
470     *            the index.
471     * @return the long parameter.
472     */
473    public long getLongParameter(int index) {
474        return ((Long)parameters.elementAt(index)).longValue();
475    }
476
477    /**
478     * Gets the integer-valued parameter found at the desired index in the
479     * vector of parameter values.
480     *
481     * @param index
482     *            the index.
483     * @return the integer parameter.
484     */
485    public int getIntParameter(int index) {
486        return ((Integer)parameters.elementAt(index)).intValue();
487    }
488
489    /**
490     * Gets the float-valued parameter found at the desired index in the vector
491     * of parameter values.
492     *
493     * @param index
494     *            the index.
495     * @return the float parameter.
496     */
497    public float getFloatParameter(int index) {
498        return ((Float)parameters.elementAt(index)).floatValue();
499    }
500
501    /**
502     * Gets the double-valued parameter found at the desired index in the vector
503     * of parameter values.
504     *
505     * @param index
506     *            the index.
507     * @return the double parameter.
508     */
509    public double getDoubleParameter(int index) {
510        return ((Double)parameters.elementAt(index)).doubleValue();
511    }
512
513    /**
514     * Gets the char-valued parameter found at the desired index in the vector
515     * of parameter values.
516     *
517     * @param index
518     *            the index.
519     * @return the char parameter.
520     */
521    public char getCharParameter(int index) {
522        return ((Character)parameters.elementAt(index)).charValue();
523    }
524
525    /**
526     * Gets the byte-valued parameter found at the desired index in the vector
527     * of parameter values.
528     *
529     * @param index
530     *            the index.
531     * @return the byte parameter.
532     */
533    public byte getByteParameter(int index) {
534        return ((Byte)parameters.elementAt(index)).byteValue();
535    }
536
537    /**
538     * Clears the vector of sources.
539     */
540    public void removeSources() {
541        sources.removeAllElements();
542    }
543
544    /**
545     * Clears the vector of parameters.
546     */
547    public void removeParameters() {
548        parameters.removeAllElements();
549    }
550
551    /**
552     * Gets the number of elements in the vector of sources.
553     *
554     * @return the number of elements in the vector of sources.
555     */
556    public int getNumSources() {
557        return sources.size();
558    }
559
560    /**
561     * Gets the number of elements in the vector of parameters.
562     *
563     * @return the number of elements in the vector of parameters.
564     */
565    public int getNumParameters() {
566        return parameters.size();
567    }
568}
569