KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > EnumeratedAttribute


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
19 package org.apache.tools.ant.types;
20
21 import org.apache.tools.ant.BuildException;
22
23 /**
24  * Helper class for attributes that can only take one of a fixed list
25  * of values.
26  *
27  * <p>See {@link org.apache.tools.ant.taskdefs.FixCRLF FixCRLF} for an
28  * example.
29  *
30  */

31 public abstract class EnumeratedAttribute {
32     // CheckStyle:VisibilityModifier OFF - bc
33
/**
34      * The selected value in this enumeration.
35      */

36     protected String JavaDoc value;
37
38     // CheckStyle:VisibilityModifier ON
39

40     /**
41      * the index of the selected value in the array.
42      */

43     private int index = -1;
44
45     /**
46      * This is the only method a subclass needs to implement.
47      *
48      * @return an array holding all possible values of the enumeration.
49      * The order of elements must be fixed so that <tt>indexOfValue(String)</tt>
50      * always return the same index for the same value.
51      */

52     public abstract String JavaDoc[] getValues();
53
54     /** bean constructor */
55     protected EnumeratedAttribute() {
56     }
57
58     /**
59      * Factory method for instantiating EAs via API in a more
60      * developer friendly way.
61      * @param clazz Class, extending EA, which to instantiate
62      * @param value The value to set on that EA
63      * @return Configured EA
64      * @throws BuildException If the class could not be found or the value
65      * is not valid for the given EA-class.
66      * @see <a HREF="http://issues.apache.org/bugzilla/show_bug.cgi?id=14831">
67      * http://issues.apache.org/bugzilla/show_bug.cgi?id=14831</a>
68      */

69     public static EnumeratedAttribute getInstance(
70         Class JavaDoc/*<? extends EnumeratedAttribute>*/ clazz,
71         String JavaDoc value) throws BuildException {
72         if (!EnumeratedAttribute.class.isAssignableFrom(clazz)) {
73             throw new BuildException(
74                 "You have to provide a subclass from EnumeratedAttribut as clazz-parameter.");
75         }
76         EnumeratedAttribute ea = null;
77         try {
78             ea = (EnumeratedAttribute) clazz.newInstance();
79         } catch (Exception JavaDoc e) {
80             throw new BuildException(e);
81         }
82         ea.setValue(value);
83         return ea;
84     }
85
86     /**
87      * Invoked by {@link org.apache.tools.ant.IntrospectionHelper IntrospectionHelper}.
88      * @param value the <code>String</code> value of the attribute
89      * @throws BuildException if the value is not valid for the attribute
90      */

91     public final void setValue(String JavaDoc value) throws BuildException {
92         int idx = indexOfValue(value);
93         if (idx == -1) {
94             throw new BuildException(value + " is not a legal value for this attribute");
95         }
96         this.index = idx;
97         this.value = value;
98     }
99
100     /**
101      * Is this value included in the enumeration?
102      * @param value the <code>String</code> value to look up
103      * @return true if the value is valid
104      */

105     public final boolean containsValue(String JavaDoc value) {
106         return (indexOfValue(value) != -1);
107     }
108
109     /**
110      * get the index of a value in this enumeration.
111      * @param value the string value to look for.
112      * @return the index of the value in the array of strings
113      * or -1 if it cannot be found.
114      * @see #getValues()
115      */

116     public final int indexOfValue(String JavaDoc value) {
117         String JavaDoc[] values = getValues();
118         if (values == null || value == null) {
119             return -1;
120         }
121         for (int i = 0; i < values.length; i++) {
122             if (value.equals(values[i])) {
123                 return i;
124             }
125         }
126         return -1;
127     }
128
129     /**
130      * @return the selected value.
131      */

132     public final String JavaDoc getValue() {
133         return value;
134     }
135
136     /**
137      * @return the index of the selected value in the array.
138      * @see #getValues()
139      */

140     public final int getIndex() {
141         return index;
142     }
143
144     /**
145      * Convert the value to its string form.
146      *
147      * @return the string form of the value.
148      */

149     public String JavaDoc toString() {
150         return getValue();
151     }
152
153 }
154
Popular Tags