KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: MergeBehavior.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 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 (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any 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 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 GNU 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 that represents the various ways an iteration setting or a collection of
35  * settings can be merged with a pre-existing set. The values are based on the standard
36  * Ant "<span class="src">build.sysclasspath</span>" merge pattern. The following list
37  * explains what the various values mean:<ul>
38  * <li><span class="src">only</span>: The newer setting is used as if the existing
39  * value never existed.</li>
40  * <li><span class="src">ignore</span>: The newer setting is ignored as if it
41  * never existed.</li>
42  * <li><span class="src">first</span>: The newer setting should be combined with
43  * the existing setting while being given preference in script queries.</li>
44  * <li><span class="src">last</span>: The newer setting should be combined with
45  * the existing setting but preference will be given the existing settings
46  * in script queries.</li>
47  * </ul>
48
49  *
50  * @since JWare/AntX 0.5
51  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
52  * @version 0.5
53  * @.safety multiple
54  * @.group api,helper
55  **/

56
57 public final class MergeBehavior extends EnumSkeleton
58 {
59     /** Index of {@linkplain #ONLY ONLY}. **/
60     public static final int ONLY_INDEX = 0;
61     /** Index of {@linkplain #IGNORE IGNORE}. **/
62     public static final int IGNORE_INDEX = ONLY_INDEX+1;
63     /** Index of {@linkplain #FIRST FIRST}. **/
64     public static final int FIRST_INDEX = IGNORE_INDEX+1;
65     /** Index of {@linkplain #LAST LAST}. **/
66     public static final int LAST_INDEX = FIRST_INDEX+1;
67
68
69     /** Singleton "<span class="src">only</span>" choice. **/
70     public static final MergeBehavior ONLY=
71         new MergeBehavior("only",ONLY_INDEX);
72
73     /** Singleton "<span class="src">ignore</span>" choice. **/
74     public static final MergeBehavior IGNORE=
75         new MergeBehavior("ignore",IGNORE_INDEX);
76
77     /** Singleton "<span class="src">first</span>" choice. **/
78     public static final MergeBehavior FIRST=
79         new MergeBehavior("first",FIRST_INDEX);
80
81     /** Singleton "<span class="src">last</span>" choice. **/
82     public static final MergeBehavior LAST=
83         new MergeBehavior("last",LAST_INDEX);
84     
85
86
87     /**
88      * Required bean void constructor for Ant's introspector.
89      **/

90     public MergeBehavior()
91     {
92         super();
93     }
94
95
96     /**
97      * Use to create public singletons. Ensures this enum is
98      * initialized as if with the default Ant Introspector
99      * helper thingy.
100      **/

101     private MergeBehavior(String JavaDoc v, int i)
102     {
103         super(v);
104     }
105
106
107     /**
108      * Returns copy of all possible source values as an ordered
109      * string array. Note: ordering should be same as our
110      * singleton indices.
111      **/

112     public String JavaDoc[] getValues()
113     {
114         return new String JavaDoc[] {"only", "ignore", "first", "last"};
115     };
116
117
118
119     /**
120      * Helper that converts a scalar to a known MergeBehavior.
121      * Returns <i>null</i> if value does not match any of expected
122      * source.
123      * @param i the index to be matched
124      **/

125     public static MergeBehavior from(int i)
126     {
127         if (i==FIRST.index) { return FIRST; }
128         if (i==ONLY.index) { return ONLY; }
129         if (i==LAST.index) { return LAST; }
130         if (i==IGNORE.index) { return IGNORE; }
131         return null;
132     }
133
134
135     /**
136      * Same as {@linkplain #from(int) from(int)} but with a
137      * default value if value does not match any known
138      * MergeBehavior's index.
139      * @param i the index to be matched
140      * @param dflt the default MergeBehavior if necessary
141      **/

142     public static MergeBehavior from(int i, MergeBehavior dflt)
143     {
144         MergeBehavior choice= from(i);
145         return (choice==null) ? dflt : choice;
146     }
147
148
149     /**
150      * Helper that converts a string to a known MergeBehavior
151      * singleton. Returns <i>null</i> if string unrecognized. String
152      * can be either MergeBehavior's symbolic name or its index.
153      **/

154     public static MergeBehavior from(String JavaDoc s)
155     {
156         if (s!=null && s.length()>1) {
157             s = Tk.lowercaseFrom(s);
158             if (Character.isDigit(s.charAt(0))) {
159                 try { return from(Integer.parseInt(s)); }
160                 catch(Exception JavaDoc nfx) {/*burp*/}
161             } else {
162                 if (FIRST.value.equals(s)) { return FIRST; }
163                 if (ONLY.value.equals(s)) { return ONLY; }
164                 if (LAST.value.equals(s)) { return LAST; }
165                 if (IGNORE.value.equals(s)) { return IGNORE; }
166             }
167         }
168         return null;
169     }
170
171
172     /**
173      * Same as {@linkplain #from(String) from(String)} but with a
174      * default value if supplied value does not match any known
175      * MergeBehavior's name.
176      * @param s the symbolic name to be matched
177      * @param dflt the default MergeBehavior if necessary
178      **/

179     public static MergeBehavior from(String JavaDoc s, MergeBehavior dflt)
180     {
181         MergeBehavior choice= from(s);
182         return (choice==null) ? dflt : choice;
183     }
184 }
185
186 /* end-of-MergeBehavior.java */
Popular Tags