View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.util;
17  
18  import java.util.Properties;
19  
20  /**
21   * A parameter with a T value.
22   * 
23   * @param <T>
24   *            type
25   */
26  public abstract class Parameter<T> {
27      private final String name;
28      private final T defaultValue;
29  
30      @Override
31      public boolean equals(Object obj) {
32          boolean equals = false;
33          if (obj != null) {
34              if (obj instanceof Parameter) {
35                  equals = this.name.equals(((Parameter<?>) obj).getName());
36              } else if (obj instanceof String) {
37                  equals = this.name.equals(obj);
38              }
39          }
40          return equals;
41      }
42  
43      @Override
44      public String toString() {
45          return this.name;
46      }
47  
48      @Override
49      public int hashCode() {
50          return this.name.hashCode();
51      }
52  
53      public Parameter(String name) {
54          this(name, null);
55      }
56  
57      Parameter(String name, T defaultValue) {
58          this.name = name;
59          this.defaultValue = defaultValue;
60  
61      }
62  
63      public String getName() {
64          return name;
65      }
66  
67      public T getDefaultValue() {
68          return defaultValue;
69      }
70  
71      public T getValue(Properties properties) {
72          T value = (T) properties.getProperty(this.name);
73  
74          if (value == null) {
75              value = defaultValue;
76          }
77          return value;
78      }
79  
80  }