View Javadoc
1   /*
2    * Copyright (C) 2010-2014 Hamburg Sud and the contributors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.aludratest.config;
17  
18  /**
19   * Helper class for Preferences consumers which can be used to have getter methods which validate the existence of a configuration
20   * property when trying to retrieve it. Clients can wrap an existing Preferences object with this class and use its methods. <br>
21   * Queried child nodes are automatically wrapped and returned as ValidatingPreferencesWrapper.
22   * 
23   * @author falbrech
24   * 
25   */
26  public final class ValidatingPreferencesWrapper implements Preferences {
27  
28      private Preferences delegate;
29  
30      /**
31       * Creates a new ValidatingPreferencesWrapper object which wraps the given Preferences object.
32       * 
33       * @param delegate
34       *            Preferences node to wrap.
35       */
36      public ValidatingPreferencesWrapper(Preferences delegate) {
37          this.delegate = delegate;
38      }
39  
40      /** Returns the String representation of the given configuration key's value. If the configuration key does not exist on this
41       * Preferences node, or if its value is <code>null</code>, an {@link org.aludratest.exception.AutomationException} is thrown.
42       * 
43       * @param key Configuration key to retrieve the value of.
44       * 
45       * @return The value of the configuration key, as a String. */
46      public String getRequiredStringValue(String key) {
47          return assertNotNull(key, getStringValue(key));
48      }
49  
50      /** Returns the int representation of the given configuration key's value. If the configuration key does not exist on this
51       * Preferences node, or if its stored String value is <code>null</code>, an
52       * {@link org.aludratest.exception.AutomationException} is thrown.
53       * 
54       * @param key Configuration key to retrieve the value of.
55       * 
56       * @return The value of the configuration key, as an int. */
57      public int getRequiredIntValue(String key) {
58          return assertNotNull(key, getIntValue(key));
59      }
60  
61      /** Returns the double representation of the given configuration key's value. If the configuration key does not exist on this
62       * Preferences node, or if its stored String value is <code>null</code>, an
63       * {@link org.aludratest.exception.AutomationException} is thrown.
64       * 
65       * @param key Configuration key to retrieve the value of.
66       * 
67       * @return The value of the configuration key, as a double. */
68      public double getRequiredDoubleValue(String key) {
69          return assertNotNull(key, getDoubleValue(key));
70      }
71  
72      /** Returns the float representation of the given configuration key's value. If the configuration key does not exist on this
73       * Preferences node, or if its stored String value is <code>null</code>, an
74       * {@link org.aludratest.exception.AutomationException} is thrown.
75       * 
76       * @param key Configuration key to retrieve the value of.
77       * 
78       * @return The value of the configuration key, as a float. */
79      public float getRequiredFloatValue(String key) {
80          return assertNotNull(key, getFloatValue(key));
81      }
82  
83      /** Returns the boolean representation of the given configuration key's value. If the configuration key does not exist on this
84       * Preferences node, or if its stored String value is <code>null</code>, an
85       * {@link org.aludratest.exception.AutomationException} is thrown.
86       * 
87       * @param key Configuration key to retrieve the value of.
88       * 
89       * @return The value of the configuration key, as a boolean. */
90      public boolean getRequiredBooleanValue(String key) {
91          return assertNotNull(key, getBooleanValue(key));
92      }
93  
94      /** Returns the char representation of the given configuration key's value. If the configuration key does not exist on this
95       * Preferences node, or if its stored String value is <code>null</code>, an
96       * {@link org.aludratest.exception.AutomationException} is thrown.
97       * 
98       * @param key Configuration key to retrieve the value of.
99       * 
100      * @return The value of the configuration key, as a char. */
101     public char getRequiredCharValue(String key) {
102         return assertNotNull(key, getCharValue(key));
103     }
104 
105     @Override
106     public String getStringValue(String key) {
107         return delegate.getStringValue(key);
108     }
109 
110     @Override
111     public int getIntValue(String key) {
112         return delegate.getIntValue(key);
113     }
114 
115     @Override
116     public boolean getBooleanValue(String key) {
117         return delegate.getBooleanValue(key);
118     }
119 
120     @Override
121     public float getFloatValue(String key) {
122         return delegate.getFloatValue(key);
123     }
124 
125     @Override
126     public double getDoubleValue(String key) {
127         return delegate.getDoubleValue(key);
128     }
129 
130     @Override
131     public char getCharValue(String key) {
132         return delegate.getCharValue(key);
133     }
134 
135     @Override
136     public String getStringValue(String key, String defaultValue) {
137         return delegate.getStringValue(key, defaultValue);
138     }
139 
140     @Override
141     public int getIntValue(String key, int defaultValue) {
142         return delegate.getIntValue(key, defaultValue);
143     }
144 
145     @Override
146     public boolean getBooleanValue(String key, boolean defaultValue) {
147         return delegate.getBooleanValue(key, defaultValue);
148     }
149 
150     @Override
151     public float getFloatValue(String key, float defaultValue) {
152         return delegate.getFloatValue(key, defaultValue);
153     }
154 
155     @Override
156     public double getDoubleValue(String key, double defaultValue) {
157         return delegate.getDoubleValue(key, defaultValue);
158     }
159 
160     @Override
161     public char getCharValue(String key, char defaultValue) {
162         return delegate.getCharValue(key, defaultValue);
163     }
164 
165     @Override
166     public String[] getKeyNames() {
167         return delegate.getKeyNames();
168     }
169 
170     @Override
171     public ValidatingPreferencesWrapper getChildNode(String name) {
172         return new ValidatingPreferencesWrapper(delegate.getChildNode(name));
173     }
174 
175     @Override
176     public String[] getChildNodeNames() {
177         return delegate.getChildNodeNames();
178     }
179 
180     /** Helper method which asserts that a property value is not null and not empty.
181      * 
182      * @param key the name of the property to check
183      * @param value the property value to check
184      * @return the value specified as parameter */
185     private static <T> T assertNotNull(String key, T value) {
186         if (value == null || "".equals(value)) {
187             throw new ConfigurationException("Missing configuration property: " + key);
188         }
189         return value;
190     }
191 }