View Javadoc
1   package org.esigate.test;
2   
3   import java.util.Properties;
4   
5   /**
6    * This is a builder for esigate properties.
7    * 
8    * <p>
9    * Used mainly for testing purposes.
10   * 
11   * @author Nicolas Richeton
12   * 
13   */
14  public class PropertiesBuilder {
15  
16      private Properties prop = new Properties();
17      private Properties target = null;
18  
19      /**
20       * Constructor
21       */
22      public PropertiesBuilder() {
23      }
24  
25      /**
26       * Set a property.
27       * 
28       * <p>
29       * Perform automatic conversion for
30       * <ul>
31       * <li>Class object : uses the full name (package + classname)</li>
32       * <li>Multiple objects : appends all values, comma separated.</li>
33       * <li>Other objects : uses the toString() value.</li>
34       * </ul>
35       * 
36       * @param key
37       *            property name
38       * @param value
39       *            one or multiple objects
40       * @return the builder (fluent style)
41       */
42      @SuppressWarnings("rawtypes")
43      public PropertiesBuilder set(Object key, Object... value) {
44  
45          String objectValue = "";
46          boolean first = true;
47  
48          for (int i = 0; i < value.length; i++) {
49  
50              // Handle list
51              if (first)
52                  first = false;
53              else
54                  objectValue = objectValue + ",";
55  
56              // Add object
57              Object o = value[i];
58  
59              if (o instanceof Class) {
60                  objectValue = objectValue + ((Class) o).getName();
61              }
62              // Other conversions could be added here...
63              else {
64                  // Default value
65                  objectValue = o.toString();
66              }
67          }
68  
69          return this.set(key.toString(), objectValue);
70      }
71  
72      /**
73       * Set a property.
74       * 
75       * @param key
76       *            property name
77       * 
78       * @param value
79       *            property value
80       * @return the builder (fluent style)
81       */
82      public PropertiesBuilder set(String key, String value) {
83          prop.setProperty(key, value);
84          return this;
85      }
86  
87      /**
88       * The builder can update an existing Properties object.
89       * 
90       * @param p
91       *            target properties object
92       * @return the builder (fluent style)
93       */
94      public PropertiesBuilder on(Properties p) {
95          target = p;
96          return this;
97      }
98  
99      /**
100      * Build the Properties object, based on previous parameters.
101      * 
102      * @return the properties
103      */
104     public Properties build() {
105 
106         if (target != null) {
107             // Merge properties
108             target.putAll(prop);
109         } else {
110             target = prop;
111         }
112 
113         return target;
114     }
115 }