KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > zeus > transform > ValueEnumeration


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  */

19 package org.enhydra.zeus.transform;
20
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * <p>
26  * <code>ValueEnumeration</code> represents a set of allowed values for a
27  * class or other construct to be allowed to take on. Even though the values
28  * may ultimately be represented by Java <code>int</code>s,
29  * <code>Date</code>s, or other non-character-based types, all data in this
30  * class is represented by Java <code>String</code>s, and later converted
31  * to appropriate types.
32  * </p>
33  *
34  * @author Brett McLaughlin
35  */

36 public class ValueEnumeration {
37     
38     /** The name of this enumeration */
39     private String JavaDoc name;
40     
41     /** The list of allowed values */
42     private List JavaDoc allowedValues;
43
44     /**
45      * <p>
46      * Default constructor.
47      * </p>
48      *
49      * @param name the name of this enumeration.
50      */

51     public ValueEnumeration(String JavaDoc name) {
52         this(name, new LinkedList JavaDoc());
53     }
54     
55     /**
56      * <p>
57      * This constructor creates a new <code>ValueEnumeration</code> using
58      * the supplied <code>List</code> to seed the allowed values.
59      * </p>
60      *
61      * @param name the name of this enumeration.
62      * @param allowedValue initial values to allow for this enumeration.
63      */

64     public ValueEnumeration(String JavaDoc name, List JavaDoc allowedValues) {
65         // Error Checking
66
if (name == null) {
67             throw new IllegalArgumentException JavaDoc("A ValueEnumeration cannot " +
68                 "have a null name.");
69         }
70         
71         this.name = name;
72         this.allowedValues = allowedValues;
73     }
74     
75     /**
76      * <p>
77      * This will set the name of this <code>ValueEnumeration</code>.
78      * </p>
79      *
80      * @param name the name for this enumeration
81      */

82     public void setName(String JavaDoc name) {
83         if (name == null) {
84             throw new IllegalArgumentException JavaDoc("A ValueEnumeration cannot " +
85                 "have a null name.");
86         }
87         this.name = name;
88     }
89     
90     /**
91      * <p>
92      * This will return the current name of this <code>ValueEnumeration</code>.
93      * </p>
94      *
95      * @return <code>String</code> - the name of this enumeration.
96      */

97     public String JavaDoc getName() {
98         return name;
99     }
100     
101     /**
102      * <p>
103      * This sets the list of allowed values to the supplied <code>List</code>.
104      * Any existing allowed values are replaced. If any value is allowed,
105      * the <code>null</code> value should be supplied here.
106      * </p>
107      *
108      * @param allowedValues the values to allow for this enumeration.
109      */

110     public void setAllowedValues(List JavaDoc allowedValues) {
111         this.allowedValues = allowedValues;
112     }
113     
114     /**
115      * <p>
116      * This returns the current list of allowed values.
117      * </p>
118      *
119      * @return <code>List</code> - the list of allowed values.
120      */

121     public List JavaDoc getAllowedValues() {
122         return allowedValues;
123     }
124     
125     /**
126      * <p>
127      * This will add a new allowed value to the current list.
128      * </p>
129      *
130      * @param allowedValue the new value to allow
131      */

132     public void addAllowedValue(String JavaDoc allowedValue) {
133         if (allowedValue == null) {
134             throw new IllegalArgumentException JavaDoc("A ValueEnumeration cannot " +
135                 "have the null value in its set of allowed values. To allow " +
136                 "a variable to have the null value, use the String \"null\".");
137         }
138         allowedValues.add(allowedValue);
139     }
140
141     /**
142      * <p>
143      * This will remove the specified value from the current list.
144      * </p>
145      *
146      * @param removedValue the value to remove from the enumeration.
147      */

148     public void removeAllowedValue(String JavaDoc removedValue) {
149         allowedValues.remove(removedValue);
150     }
151     
152     /**
153      * <p>
154      * This will indicate if the supplied value is allowed for this
155      * <code>ValueEnumeration</code>.
156      * </p><p>
157      * Note that this operation is <i>case-sensitive</i>; while this is not
158      * relevant if the data is numerical or non-character-based, it will
159      * affect this operation on character-based values.
160      * </p>
161      *
162      * @param value the value to check for legality in this enumeration.
163      * @return <code>boolean</code> - whether the supplied value is allowed.
164      */

165     public boolean isAllowedValue(String JavaDoc value) {
166         return allowedValues.contains(value);
167     }
168 }
169
Popular Tags