KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > PropUtil


1 package org.sapia.ubik.rmi;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 /**
8  * This class provides utility methods over a list of <code>Properties</code> object that are sequentially
9  * looked up for given values.
10  *
11  * @author Yanick Duchesne
12  *
13  * <dl>
14  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class PropUtil {
20   
21   private List JavaDoc _props = new ArrayList JavaDoc();
22   
23   /**
24    * @param props some <code>Properties</code> to look up.
25    * @return this instance.
26    */

27   public PropUtil addProperties(Properties JavaDoc props){
28     _props.add(props);
29     return this;
30   }
31   
32   /**
33    * @param key the key of the desired property.
34    * @return the property value.
35    * @throws NumberFormatException if the value could not be converted to an integer.
36    * @throws IllegalArgumentException if no property exists for the given key.
37    */

38   public int getIntProperty(String JavaDoc key) throws NumberFormatException JavaDoc, IllegalArgumentException JavaDoc{
39     String JavaDoc val = lookup(key, true);
40     return Integer.parseInt(val);
41   }
42   
43   /**
44    * @param key the key of the desired property.
45    * @param defaultValue the default value that must be returned if not property was found
46    * for the given key.
47    * @return the property value.
48    * @throws NumberFormatException if the value could not be converted to an integer.
49      */

50   public int getIntProperty(String JavaDoc key, int defaultValue) throws NumberFormatException JavaDoc{
51     String JavaDoc val = lookup(key, false);
52     if(val == null){
53       return defaultValue;
54     }
55     return Integer.parseInt(val);
56   }
57   
58   /**
59    * @param key the key of the desired property.
60    * @return the property value.
61    * @throws NumberFormatException if the value could not be converted to a long.
62    * @throws IllegalArgumentException if no property exists for the given key.
63    */

64   public long getLongProperty(String JavaDoc key) throws NumberFormatException JavaDoc, IllegalArgumentException JavaDoc{
65     String JavaDoc val = lookup(key, true);
66     return Long.parseLong(val);
67   }
68   
69   /**
70    * @param key the key of the desired property.
71    * @param defaultValue the default value that must be returned if not property was found
72    * for the given key.
73    * @return the property value.
74    * @throws NumberFormatException if the value could not be converted to a long.
75      */

76   public long getLongProperty(String JavaDoc key, long defaultValue) throws NumberFormatException JavaDoc{
77     String JavaDoc val = lookup(key, false);
78     if(val == null){
79       return defaultValue;
80     }
81     return Long.parseLong(val);
82   }
83   
84   
85   /**
86    * @param key the key of the desired property
87    * @return <code>true</code> if the value corresponding to the given key equals <code>true</code>, <code>on</code>, or
88    * <code>yes</code>, <code>false</code> otherwise, or if no value could be found for the given key.
89    */

90   public boolean getBooleanProperty(String JavaDoc key){
91     String JavaDoc val = lookup(key, false);
92     if(val == null){
93       return false;
94     }
95     val = val.toLowerCase();
96     return val.equals("true") || val.equals("yes") || val.equals("yes");
97   }
98   
99   /**
100    * @param key the key of the desired property
101    * @return <code>true</code> if the value corresponding to the given key equals <code>true</code>, <code>on</code>, or
102    * <code>yes</code>, <code>false</code> otherwise, or the passed in default if no value could be found for the given key.
103    */

104   public boolean getBooleanProperty(String JavaDoc key, boolean defaultValue){
105     String JavaDoc val = lookup(key, false);
106     if(val == null){
107       return defaultValue;
108     }
109     val = val.toLowerCase();
110     return val.equals("true") || val.equals("yes") || val.equals("yes");
111   }
112   
113   /**
114    * @param key the key of the desired property.
115    * @return the value corresponding to the given key, or <code>null</code> if no
116    * such value exists.
117    */

118   public String JavaDoc getProperty(String JavaDoc key){
119     return lookup(key, false);
120   }
121   
122   /**
123    * @param key the key of the desired property.
124    * @return the value corresponding to the given key, or the given
125    * default value of the desired property was not found.
126    */

127   public String JavaDoc getProperty(String JavaDoc key, String JavaDoc defaultValue){
128     String JavaDoc val = lookup(key, false);
129     if(val == null){
130       return defaultValue;
131     }
132     return val;
133   }
134   
135   private String JavaDoc lookup(String JavaDoc key, boolean throwExcIfNotFound){
136     Properties JavaDoc current;
137     String JavaDoc val;
138     for(int i = 0; i < _props.size(); i++){
139       current = (Properties JavaDoc)_props.get(i);
140       val = current.getProperty(key);
141       if(val != null){
142         return val;
143       }
144     }
145     if(throwExcIfNotFound){
146       throw new IllegalArgumentException JavaDoc("No value found for property: " + key);
147     }
148     return null;
149   }
150
151 }
152
Popular Tags