source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 3.2 KB
Line 
1/*
2 * Copyright 2000-2002,2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.types;
19
20import org.apache.tools.ant.BuildException;
21
22/**
23 * Helper class for attributes that can only take one of a fixed list
24 * of values.
25 *
26 * <p>See {@link org.apache.tools.ant.taskdefs.FixCRLF FixCRLF} for an
27 * example.
28 *
29 */
30public abstract class EnumeratedAttribute {
31
32 /**
33 * The selected value in this enumeration.
34 */
35 protected String value;
36
37 /**
38 * the index of the selected value in the array.
39 */
40 private int index = -1;
41
42 /**
43 * This is the only method a subclass needs to implement.
44 *
45 * @return an array holding all possible values of the enumeration.
46 * The order of elements must be fixed so that <tt>indexOfValue(String)</tt>
47 * always return the same index for the same value.
48 */
49 public abstract String[] getValues();
50
51 /** bean constructor */
52 protected EnumeratedAttribute() {
53 }
54
55 /**
56 * Invoked by {@link org.apache.tools.ant.IntrospectionHelper IntrospectionHelper}.
57 */
58 public final void setValue(String value) throws BuildException {
59 int index = indexOfValue(value);
60 if (index == -1) {
61 throw new BuildException(value + " is not a legal value for this attribute");
62 }
63 this.index = index;
64 this.value = value;
65 }
66
67 /**
68 * Is this value included in the enumeration?
69 */
70 public final boolean containsValue(String value) {
71 return (indexOfValue(value) != -1);
72 }
73
74 /**
75 * get the index of a value in this enumeration.
76 * @param value the string value to look for.
77 * @return the index of the value in the array of strings
78 * or -1 if it cannot be found.
79 * @see #getValues()
80 */
81 public final int indexOfValue(String value) {
82 String[] values = getValues();
83 if (values == null || value == null) {
84 return -1;
85 }
86 for (int i = 0; i < values.length; i++) {
87 if (value.equals(values[i])) {
88 return i;
89 }
90 }
91 return -1;
92 }
93
94 /**
95 * @return the selected value.
96 */
97 public final String getValue() {
98 return value;
99 }
100
101 /**
102 * @return the index of the selected value in the array.
103 * @see #getValues()
104 */
105 public final int getIndex() {
106 return index;
107 }
108
109
110 /**
111 * Convert the value to its string form.
112 *
113 * @return the string form of the value.
114 */
115 public String toString() {
116 return getValue();
117 }
118
119}
Note: See TracBrowser for help on using the repository browser.