KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > StringFlags


1 package jodd.util;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5
6 import jodd.bean.BeanUtil;
7
8 /**
9  * Parses string and sets bean boolean parameters (flags) depending on
10  * exisiting characters. Class may be used when methods has many boolean
11  * parameters. In this cases, a single string can replace all those
12  * parameters, where chars represent if some boolean property should be
13  * set to <code>true</code> or <code>false</code>.
14  * <p>
15  *
16  * Usually, this class is instanced and configured in static block of a
17  * class that uses it:<br>
18  * <code>
19  * private static StringFlags flags = new StringFlags();
20  * static {
21  * flags.addFlag('p', "fooP", true);
22  * flags.addFlag('d', "fooD", false);
23  * }
24  * </code>
25  *
26  * Flags boolean values are those that properties will be set to if
27  * coresponding character exist in option string. This means that when such
28  * character doesn't exist in options string, its parameter will be set to
29  * inverse value. In above example, if option string contains char 'p' then
30  * setFooP(true) will be performed, otherwise setFooP(false) will be
31  * performed. For the 'd' flag is opposite: if it is present in the option
32  * string, setFooD(false) will be executed, otherwise setFooD(true) will be
33  * executed.
34  */

35 public class StringFlags {
36
37     private HashMap map = new HashMap();
38
39     class Flag {
40         String bpname;
41         boolean val;
42     }
43
44     /**
45      * Add new flag to the buffer.
46      *
47      * @param c character flag
48      * @param bpname name of bean property (without set or get)
49      * @param val value that will be set if character exist in the string.
50      */

51     public void addFlag(char c, String bpname, boolean val) {
52         Flag f = new Flag();
53         f.bpname = bpname;
54         f.val = val;
55         map.put(new Character(c), f);
56     }
57
58     /**
59      * Clears complete flags buffer.
60      */

61     public void clearFlags() {
62         map.clear();
63     }
64
65
66     /**
67      * Parses given string and sets the boolean properties of a bean.
68      *
69      * @param params option string, contains char flags
70      * @param o object to populate
71      */

72     public void parse(String params, Object o) {
73         if (params == null) {
74             params = "";
75         }
76         char[] ca = params.toCharArray();
77         Iterator i = map.keySet().iterator();
78         while (i.hasNext()) {
79             Character c = (Character) i.next();
80             Flag f = (Flag) map.get(c);
81             if (f == null) {
82                 continue;
83             }
84             boolean b = f.val;
85             if (params.indexOf(c.charValue()) == -1) {
86                 b = !b;
87             }
88             BeanUtil.setProperty(o, f.bpname, new Boolean(b));
89         }
90     }
91
92 }
93
94
Popular Tags