KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > parameters > IsA


1 /**
2  * $Id: IsA.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2003-2004 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.parameters;
30
31 import com.idaremedia.antx.helpers.Tk;
32
33 /**
34  * Enumeration of a flex value's possible interpretation: as a literal, as a property,
35  * as a reference, or as a variable (exported property).
36  *
37  * @since JWare/AntX 0.2
38  * @author ssmc, &copy;2003-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
39  * @version 0.5
40  * @.safety single
41  * @.group impl,helper
42  **/

43
44 public final class IsA extends EnumSkeleton
45 {
46     /** Index of {@linkplain #LITERAL LITERAL}. **/
47     public static final int LITERAL_INDEX= 0;
48     /** Index of {@linkplain #PROPERTY PROPERTY}. **/
49     public static final int PROPERTY_INDEX= LITERAL_INDEX+1;
50     /** Index of {@linkplain #VARIABLE VARIABLE}. **/
51     public static final int VARIABLE_INDEX= PROPERTY_INDEX+1;
52     /** Index of {@linkplain #REFERENCE REFERENCE}. **/
53     public static final int REFERENCE_INDEX= VARIABLE_INDEX+1;
54
55     /** Singleton "literal" Is-A. **/
56     public static final IsA LITERAL = new IsA("literal",LITERAL_INDEX);
57     /** Singleton "property" Is-A. **/
58     public static final IsA PROPERTY = new IsA("property",PROPERTY_INDEX);
59     /** Singleton "variable" Is-A. **/
60     public static final IsA VARIABLE = new IsA("variable",VARIABLE_INDEX);
61     /** Singleton "reference" Is-A. **/
62     public static final IsA REFERENCE = new IsA("reference",REFERENCE_INDEX);
63
64
65     /**
66      * Required bean void constructor for Ant's introspector.
67      **/

68     public IsA()
69     {
70         super();
71     }
72
73
74     /**
75      * Use to create public singletons. Ensure it's initialized
76      * as with default Ant Introspector helper thingy.
77      **/

78     private IsA(String JavaDoc v, int i)
79     {
80         super(v);
81     }
82
83
84     /**
85      * Returns copy of all possible modification values as an ordered
86      * string array. Note: ordering should be same as singletons indice.
87      **/

88     public String JavaDoc[] getValues()
89     {
90         return new String JavaDoc[] {"literal","property","variable","reference"};
91     };
92
93
94
95     /**
96      * Helper that converts a scalar to a known IsA. Returns <i>null</i>
97      * if value does not match any of expected modifications.
98      * @param i the index to be matched
99      **/

100     public static IsA from(int i)
101     {
102         if (i==PROPERTY.index) { return PROPERTY; }
103         if (i==VARIABLE.index) { return VARIABLE; }
104         if (i==REFERENCE.index) { return REFERENCE; }
105         if (i==LITERAL.index) { return LITERAL; }
106         return null;
107     }
108
109
110     /**
111      * Same as {@linkplain #from(int) from(int)} but with a
112      * default value if value does not match any known IsA's
113      * index.
114      * @param i the index to be matched
115      * @param dflt the default IsA if necessary
116      **/

117     public static IsA from(int i, IsA dflt)
118     {
119         IsA isa= from(i);
120         return (isa==null) ? dflt : isa;
121     }
122
123
124     /**
125      * Helper that converts a string to a known IsA singleton.
126      * Returns <i>null</i> if string unrecognized. String can be
127      * either IsA's symbolic name or its index.
128      **/

129     public static IsA from(String JavaDoc s)
130     {
131         if (s!=null && s.length()>1) {
132             s = Tk.lowercaseFrom(s);
133             if (Character.isDigit(s.charAt(0))) {
134                 try { return from(Integer.parseInt(s)); }
135                 catch(Exception JavaDoc nfx) {/*burp*/}
136             } else {
137                 if ("prop".equals(s)) { return PROPERTY; }
138                 if (PROPERTY.value.equals(s)) { return PROPERTY; }
139                 if ("var".equals(s)) { return VARIABLE; }
140                 if (VARIABLE.value.equals(s)) { return VARIABLE; }
141                 if ("ref".equals(s)) { return REFERENCE;}
142                 if (REFERENCE.value.equals(s)) { return REFERENCE;}
143                 if ("value".equals(s)) { return LITERAL; }
144                 if (LITERAL.value.equals(s)) { return LITERAL; }
145
146                 if ("variables".equals(s)) { return VARIABLE; }
147                 if ("references".equals(s)) { return REFERENCE;}
148                 if ("properties".equals(s)) { return PROPERTY; }
149                 if ("default".equals(s)) { return PROPERTY; }
150             }
151         }
152         return null;
153     }
154
155
156     /**
157      * Same as {@linkplain #from(java.lang.String) from(String)} but
158      * with a default value if value does not match any known
159      * IsA's name.
160      * @param s the symbolic name to be matched
161      * @param dflt the default IsA if necessary
162      **/

163     public static IsA from(String JavaDoc s, IsA dflt)
164     {
165         IsA isa= from(s);
166         return (isa==null) ? dflt : isa;
167     }
168 }
169
170 /* end-of-IsA.java */
171
Popular Tags