001: /*
002: * $RCSfile: EnumeratedParameter.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:08 $
010: * $State: Exp $
011: */
012: package javax.media.jai;
013:
014: import java.io.Serializable;
015:
016: /**
017: * This class provides a mechanism by which enumerated parameters may be
018: * added to subclasses of <code>OperationDescriptorImpl</code> while
019: * retaining the ability to perform introspection on the allowable range of
020: * values of the enumeration. An example of an enumerated parameter is the
021: * <i>type</i> parameter of the "Transpose" operation which is defined in
022: * <code>TransposeDescriptor</code> to accept only the values defined by the
023: * <code>FLIP_*</code> and <code>ROTATE_*</code> fields of the descriptor.
024: *
025: * <p> This class may be used to create enumerated parameters in an
026: * <code>OperationDescriptor</code> as follows:
027: *
028: * <ul>
029: *
030: * <li> For each constrained-value parameter create a final class extending
031: * <code>EnumeratedParameter</code>. This class should consist of only a
032: * package private constructor with a single <code>String</code> parameter
033: * called <i>name</i> and a single statement invoking the superclass
034: * constructor with <i>name</i> as the parameter.</li>
035: *
036: * <li> Define the class of the parameter in question to be the
037: * subclass of <code>EnumeratedParameter</code> which was created for it in
038: * in the previous step.</li>
039: *
040: * <li> For each possible value of the parameter, define in the
041: * <code>OperationDescriptor</code> of the operator a public static final
042: * field of type equal to the class defined in the first step. Each field
043: * should be assigned an instance of the subclass defined in the first step.
044: * The <i>name</i> and <code>value</code> used for each of these
045: * <code>EnumeratedParameter</code> subclass instances should be distinct
046: * for each distinct field or an error will occur.</li>
047: *
048: * </ul>
049: *
050: * With respect to <code>TransposeDescriptor</code>, the three steps above
051: * would be to 1) create a final class <code>TransposeType</code> in the
052: * <code>javax.media.jai.operator</code> package; 2) define the
053: * type of the "type" parameter as <code>TransposeType.class</code>; and
054: * 3) define a static final field of class <code>TransposeType</code> for
055: * each of the enumerated values with each field being initialized to an
056: * instance of <code>TransposeType</code> with <i>name</i> equal to the name
057: * of the field and <i>value</i> to its integral (enumerated) value.
058: *
059: * @see OperationDescriptorImpl
060: * @see javax.media.jai.operator.TransposeDescriptor
061: *
062: * @since JAI 1.1
063: */
064: public class EnumeratedParameter implements Serializable {
065: private String name;
066: private int value;
067:
068: /**
069: * Constructs an <code>EnumeratedParameter</code> with the indicated name
070: * and value.
071: */
072: public EnumeratedParameter(String name, int value) {
073: this .name = name;
074: this .value = value;
075: }
076:
077: /**
078: * Returns the name assigned to this <code>EnumeratedParameter</code>
079: * when it was constructed.
080: */
081: public String getName() {
082: return name;
083: }
084:
085: /**
086: * Returns the value assigned to this <code>EnumeratedParameter</code>
087: * when it was constructed.
088: */
089: public int getValue() {
090: return value;
091: }
092:
093: /**
094: * Returns a hash code value for the object.
095: */
096: public int hashCode() {
097: return (getClass().getName() + (new Integer(value))).hashCode();
098: }
099:
100: /**
101: * Returns <code>true</code> if and only if the parameter is an instance
102: * of the class on which this method is invoked and has either the same
103: * name or the same value.
104: */
105: public boolean equals(Object o) {
106: return o != null
107: && this .getClass().equals(o.getClass())
108: && (name.equals(((EnumeratedParameter) o).getName()) || value == ((EnumeratedParameter) o)
109: .getValue());
110: }
111:
112: /**
113: * Returns a <code>String</code> representation of this
114: * <code>EnumeratedParameter</code> as a concatentation of the form
115: * <pre>
116: * [class name]:[parameter name]=[parameter value]
117: * </pre>
118: * For example, for an instance of a subclass
119: * <code>org.foobar.jai.EnumParam</code> with name "SomeValue" and
120: * value "2" the returned <code>String</code> would be
121: * <pre>
122: * org.foobar.jai.EnumParam:SomeValue=2
123: * </pre>
124: */
125: public String toString() {
126: return getClass().getName() + ":" + name + "="
127: + String.valueOf(value);
128: }
129: }
|