KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > helpers > Setting


1 /**
2  * $Id: Setting.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-2003 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.helpers;
30
31 import org.apache.tools.ant.types.EnumeratedAttribute;
32
33 /**
34  * Enumeration of settings: on, off, default, inherited, and undefined.
35  *
36  * @since JWare/AntX 0.1
37  * @author ssmc, &copy;2002-2003 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
38  * @version 0.5
39  * @.safety single
40  * @.group impl,helper
41  **/

42
43 public final class Setting extends EnumeratedAttribute
44 {
45     /** Index of {@linkplain #OFF OFF}. **/
46     public static final int OFF_INDEX= 0;
47     /** Index of {@linkplain #ON ON}. **/
48     public static final int ON_INDEX= 1;
49     /** Index of {@linkplain #DEFAULT DEFAULT}. **/
50     public static final int DEFAULT_INDEX= 2;
51     /** Index of {@linkplain #INHERITED INHERITED}. **/
52     public static final int INHERITED_INDEX= 3;
53     /** Index of {@linkplain #UNDEFINED UNDEFINED}. **/
54     public static final int UNDEFINED_INDEX= 4;
55
56     /** Singleton "off" setting. **/
57     public static final Setting OFF = new Setting("off",OFF_INDEX);
58     /** Singleton "on" setting. **/
59     public static final Setting ON = new Setting("on",ON_INDEX);
60     /** Singleton "default" setting. **/
61     public static final Setting DEFAULT = new Setting("default",DEFAULT_INDEX);
62     /** Singleton "inherited" setting. **/
63     public static final Setting INHERITED = new Setting("inherited",INHERITED_INDEX);
64     /** Singleton "undefined" setting. **/
65     public static final Setting UNDEFINED = new Setting("undefined",UNDEFINED_INDEX);
66
67
68
69     /**
70      * Required bean void constructor for Ant's introspector.
71      **/

72     public Setting()
73     {
74         super();
75     }
76
77
78     /**
79      * Use to create public singletons. Ensure it's initialized
80      * as with default Ant Introspector helper thingy.
81      **/

82     private Setting(String JavaDoc v, int i)
83     {
84         super();
85         Tk.initEnum(this,v);
86         this.index = getIndex();//speeds up 'from'
87
}
88
89
90     /**
91      * Returns copy of all possible settings as an ordered string
92      * array. Implementation: must be in order specified in public
93      * INDEX_xxx constants.
94      **/

95     public String JavaDoc[] getValues()
96     {
97         return new String JavaDoc[] {"off","on","default","inherited","undefined"};
98     };
99
100
101
102     /**
103      * Helper that converts a scalar to a known setting.
104      * @param i the index to be matched
105      **/

106     public static Setting from(int i)
107     {
108         if (i==ON.index) { return ON; }
109         if (i==OFF.index) { return OFF; }
110         if (i==DEFAULT.index) { return DEFAULT; }
111         if (i==INHERITED.index) { return INHERITED; }
112         if (i==UNDEFINED.index) { return UNDEFINED; }
113         return null;
114     }
115
116
117     /**
118      * Same as {@linkplain #from(int) from(int)} but with a
119      * default value if value does not match any known setting
120      * index.
121      * @param i the index to be matched
122      * @param dflt the default Setting if necessary
123      **/

124     public static Setting from(int i, Setting dflt)
125     {
126         Setting st= from(i);
127         return (st==null) ? dflt : st;
128     }
129
130
131     /**
132      * Helper that converts a scalar to a known setting. Some synonyms
133      * for on (true,yes) and off (false,no) as accepted. Returns <i>null</i>
134      * if string unrecognized. String can be either setting's symbolic
135      * name or its index.
136      * @param s the string to be interpreted
137      **/

138     public static Setting from(String JavaDoc s)
139     {
140         if (s!=null && s.length()>1) {
141             s = s.toLowerCase();
142             if (Character.isDigit(s.charAt(0))) {
143                 try { return from(Integer.parseInt(s)); }
144                 catch(Exception JavaDoc nfx) {/*burp*/}
145             } else {
146                 if ("yes".equals(s)) { return ON; }
147                 if ("true".equals(s)) { return ON; }
148                 if (ON.value.equals(s)) { return ON; }
149
150                 if ("no".equals(s)) { return OFF; }
151                 if ("false".equals(s)) { return OFF; }
152                 if (OFF.value.equals(s)) { return OFF; }
153
154                 if (Strings.INHERIT.equals(s)) { return INHERITED; }
155                 if (INHERITED.value.equals(s)) { return INHERITED; }
156
157                 if (DEFAULT.value.equals(s)) { return DEFAULT; }
158                 if (UNDEFINED.value.equals(s)) { return UNDEFINED; }
159             }
160         }
161         return null;
162     }
163
164
165     /**
166      * Same as {@linkplain #from(java.lang.String) from(String)} but
167      * with a default value if value does not match any known
168      * setting's name.
169      * @param s the symbolic name to be matched
170      * @param dflt the default Setting if necessary
171      **/

172     public static Setting from(String JavaDoc s, Setting dflt)
173     {
174         Setting st= from(s);
175         return (st==null) ? dflt : st;
176     }
177
178
179     /**
180      * Returns <i>true</i> if this setting equals incoming
181      * object (also a setting).
182      **/

183     public boolean equals(Object JavaDoc o)
184     {
185         if (o==this) { return true; }
186         if (o==null) { return false; }
187         if (o.getClass()==getClass()) {
188             return ((Setting)o).getIndex()==this.getIndex();
189         }
190         return false;
191     }
192
193
194
195     /**
196      * Returns this setting's hash value; fixed when
197      * constructed.
198      **/

199     public int hashCode()
200     {
201         return this.value.hashCode();
202     }
203
204     private int index;
205 }
206
207 /* end-of-Setting.java */
208
Popular Tags