KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > solo > Modification


1 /**
2  * $Id: Modification.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-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.solo;
30
31 import com.idaremedia.antx.helpers.Tk;
32 import com.idaremedia.antx.parameters.EnumSkeleton;
33
34 /**
35  * Enumeration of variable modifications. Exposed as standalone class since reused by
36  * multiple AntX tasks and conditions. The following list explains what the various
37  * settings mean:<ul>
38  * <li><span class="src">+s</span>: The value (a string) is concatenated to the
39  * current value of the variable (also a string).</li>
40  * <li><span class="src">len</span> or <span class="src">=s</span>: The variable is set
41  * to the source's (a string)length.</li>
42  * </ul>
43  *
44  * @since JWare/AntX 0.1
45  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
46  * @version 0.5
47  * @.safety single
48  * @.group impl,helper
49  **/

50
51 public class Modification extends EnumSkeleton
52 {
53     //@.impl ORDERING of static declarations is important! ------
54

55     //1) Indices (in order)
56

57     /** Index of {@linkplain #SET SET}. **/
58     public static final int SET_INDEX= 0;
59     /** Index of {@linkplain #INC INC}. **/
60     public static final int INC_INDEX= SET_INDEX+1;
61     /** Index of {@linkplain #DEC DEC}. **/
62     public static final int DEC_INDEX= INC_INDEX+1;
63     /** Index of {@linkplain #INC_BY1 INC_BY1}. **/
64     public static final int INC_BY1_INDEX= DEC_INDEX+1;
65     /** Index of {@linkplain #DEC_BY1 DEC_BY1}. **/
66     public static final int DEC_BY1_INDEX= INC_BY1_INDEX+1;
67     /** Index of {@linkplain #NOW NOW}. **/
68     public static final int NOW_INDEX= DEC_BY1_INDEX+1;
69     /** Index of {@linkplain #MINUS_NOW MINUS_NOW}. **/
70     public static final int MINUS_NOW_INDEX= NOW_INDEX+1;
71
72     /** Index of {@linkplain #STRCAT STRCAT}.
73      * @since JWare/AntX 0.5 **/

74      public static final int STRCAT_INDEX= MINUS_NOW_INDEX+1;
75      
76     /** Index of {@linkplain #STRLEN STRLEN}.
77      * @since JWare/AntX 0.5 **/

78      public static final int STRLEN_INDEX= STRCAT_INDEX+1;
79
80
81
82     /** The number of base modifications. Subclasses need.
83      * @since JWare/AntX 0.4
84      **/

85     protected static final int BASE_VALUE_COUNT= STRLEN_INDEX+1;
86
87
88     //2) Values (in order)
89

90     /** Values in same order as public indices. **/
91     private static final String JavaDoc[] VALUES_= new String JavaDoc[] {
92         "=", "+", "-", "++", "--", "now", "-now", "+s", "len"
93     };
94
95
96     //3) Singletons (depend on Indices and Values already existing!)
97

98     /** Singleton "set" operation. **/
99     public static final Modification SET =
100         new Modification(VALUES_[SET_INDEX],SET_INDEX);
101     /** Singleton "increment" operation. **/
102     public static final Modification INC =
103         new Modification(VALUES_[INC_INDEX],INC_INDEX);
104     /** Singleton "decrement" operation. **/
105     public static final Modification DEC =
106         new Modification(VALUES_[DEC_INDEX],DEC_INDEX);
107     /** Singleton "increment-by-1" operation. **/
108     public static final Modification INC_BY1 =
109         new Modification(VALUES_[INC_BY1_INDEX],INC_BY1_INDEX);
110     /** Singleton "decrement-by-1" operation. **/
111     public static final Modification DEC_BY1 =
112         new Modification(VALUES_[DEC_BY1_INDEX],DEC_BY1_INDEX);
113     /** Singleton "now" operation. **/
114     public static final Modification NOW =
115         new Modification(VALUES_[NOW_INDEX],NOW_INDEX);
116     /** Singleton "minus-now" operation. **/
117     public static final Modification MINUS_NOW =
118         new Modification(VALUES_[MINUS_NOW_INDEX],MINUS_NOW_INDEX);
119
120     /** Singleton "strcat" operation.
121      * @since JWare/AntX 0.5 **/

122     public static final Modification STRCAT =
123         new Modification(VALUES_[STRCAT_INDEX],STRCAT_INDEX);
124
125     /** Singleton "strlen" operation.
126      * @since JWare/AntX 0.5 **/

127     public static final Modification STRLEN =
128         new Modification(VALUES_[STRLEN_INDEX],STRLEN_INDEX);
129
130
131     /**
132      * Required bean void constructor for Ant's introspector.
133      **/

134     public Modification()
135     {
136         super();
137     }
138
139
140     /**
141      * Use to create public singletons. Ensure it's initialized
142      * as with default Ant Introspector helper thingy.
143      **/

144     private Modification(String JavaDoc v, int i)
145     {
146         super(v);
147     }
148
149
150
151     /**
152      * Returns a <em>copy</em> of the standard modification
153      * operators in order. Subclasses can use this method to
154      * pre-fill their value arrays with the inherited list.
155      * @param fillin [optional] array of strings to update with values.
156      **/

157     public static String JavaDoc[] copyOfDefaultValues(String JavaDoc[] fillin)
158     {
159         if (fillin==null) {
160             fillin = new String JavaDoc[VALUES_.length];
161         }
162         System.arraycopy(VALUES_,0,fillin,0,VALUES_.length);
163         return fillin;
164     }
165
166
167
168     /**
169      * Returns copy of all possible modification values as an ordered
170      * string array. Note: ordering should be same as singletons indice.
171      **/

172     public String JavaDoc[] getValues()
173     {
174         return Modification.copyOfDefaultValues(null);
175     };
176
177
178
179     /**
180      * Helper that converts a scalar to a known modification.
181      * Returns <i>null</i> if value does not match any of five
182      * expected modifications.
183      * @param i the index to be matched
184      **/

185     public static Modification from(int i)
186     {
187         switch(i) {
188             case SET_INDEX: return SET;
189             case INC_INDEX: return INC;
190             case DEC_INDEX: return DEC;
191             case INC_BY1_INDEX: return INC_BY1;
192             case DEC_BY1_INDEX: return DEC_BY1;
193             case NOW_INDEX: return NOW;
194             case MINUS_NOW_INDEX: return MINUS_NOW;
195             case STRCAT_INDEX: return STRCAT;
196             case STRLEN_INDEX: return STRLEN;
197         }
198         return null;
199     }
200
201
202     /**
203      * Same as {@linkplain #from(int) from(int)} but with a
204      * default value if value does not match any known modification
205      * index.
206      * @param i the index to be matched
207      * @param dfltMod the default modification if necessary
208      **/

209     public static Modification from(int i, Modification dfltMod)
210     {
211         Modification mod= from(i);
212         return (mod==null) ? dfltMod : mod;
213     }
214
215
216     /**
217      * Helper that converts a string to a known modification
218      * singleton. Returns <i>null</i> if string unrecognized.
219      * String can be either modification's symbolic name or
220      * its index.
221      **/

222     public static Modification from(String JavaDoc s)
223     {
224         if (s!=null && s.length()>1) {
225             s = Tk.lowercaseFrom(s);
226             if (Character.isDigit(s.charAt(0))) {
227                 try { return from(Integer.parseInt(s)); }
228                 catch(Exception JavaDoc nfx) {/*burp*/}
229             } else {
230                 if ("default".equals(s)) { return getAntXDefault(); }
231                 if (SET.value.equals(s)) { return SET; }
232                 if (INC.value.equals(s)) { return INC; }
233                 if (DEC.value.equals(s)) { return DEC; }
234                 if (INC_BY1.value.equals(s)) { return INC_BY1; }
235                 if (DEC_BY1.value.equals(s)) { return DEC_BY1; }
236                 if (NOW.value.equals(s)) { return NOW; }
237                 if (MINUS_NOW.value.equals(s)) { return MINUS_NOW; }
238                 if (STRCAT.value.equals(s)) { return STRCAT; }
239                 if ("strcat".equals(s)) { return STRCAT; }
240                 if (STRLEN.value.equals(s)) { return STRLEN; }
241                 if ("=s".equals(s)) { return STRLEN; }
242                 if ("strlen".equals(s)) { return STRLEN; }
243             }
244         }
245         return null;
246     }
247
248
249     /**
250      * Same as {@linkplain #from(java.lang.String) from(String)} but
251      * with a default value if value does not match any known
252      * modification name.
253      * @param s the symbolic name to be matched
254      * @param dfltMod the default modification if necessary
255      **/

256     public static Modification from(String JavaDoc s, Modification dfltMod)
257     {
258         Modification mod= from(s);
259         return (mod==null) ? dfltMod : mod;
260     }
261
262
263     /**
264      * Returns the <i>AntX</i> default modification (SET).
265      **/

266     public static final Modification getAntXDefault()
267     {
268         return Modification.SET;
269     }
270 }
271
272 /* end-of-Modification.java */
273
Popular Tags