Project

General

Profile

1
package eu.dnetlib.pace.config;
2

    
3
import java.util.List;
4

    
5
/**
6
 * The Class OptionalConfig.
7
 */
8
public class OptionalConfig {
9

    
10
	/** The config. */
11
	private com.typesafe.config.Config config;
12

    
13
	/**
14
	 * Instantiates a new optional config.
15
	 * 
16
	 * @param config
17
	 *            the config
18
	 */
19
	public OptionalConfig(final com.typesafe.config.Config config) {
20
		this.config = config;
21
	}
22

    
23
	/**
24
	 * Safe.
25
	 * 
26
	 * @param path
27
	 *            the path
28
	 * @return the object
29
	 */
30
	protected Object safe(final String path) {
31
		return config.hasPath(path) ? config.getAnyRef(path) : null;
32
	}
33

    
34
	/**
35
	 * Gets the int.
36
	 * 
37
	 * @param path
38
	 *            the path
39
	 * @return the int
40
	 */
41
	public Integer getInt(final String path) {
42
		return (Integer) safe(path);
43
	}
44

    
45
	/**
46
	 * Gets the string.
47
	 * 
48
	 * @param path
49
	 *            the path
50
	 * @return the string
51
	 */
52
	public String getString(final String path) {
53
		return (String) safe(path);
54
	}
55

    
56
	/**
57
	 * Gets the list.
58
	 * 
59
	 * @param <T>
60
	 *            the generic type
61
	 * @param path
62
	 *            the path
63
	 * @return the list
64
	 */
65
	@SuppressWarnings("unchecked")
66
	public <T> List<T> getList(final String path) {
67
		return (List<T>) safe(path);
68
	}
69

    
70
	/**
71
	 * Gets the double.
72
	 * 
73
	 * @param path
74
	 *            the path
75
	 * @return the double
76
	 */
77
	public Double getDouble(final String path) {
78
		Object safe = safe(path);
79
		if (safe instanceof Integer) return Double.parseDouble(safe.toString());
80
		return (Double) safe;
81
	}
82

    
83
	/**
84
	 * Gets the object.
85
	 * 
86
	 * @param path
87
	 *            the path
88
	 * @return the object
89
	 */
90
	public Object getObject(final String path) {
91
		return safe(path);
92
	}
93

    
94
	/**
95
	 * Gets the boolean.
96
	 * 
97
	 * @param path
98
	 *            the path
99
	 * @return the boolean
100
	 */
101
	public Boolean getBoolean(final String path) {
102
		return (Boolean) safe(path);
103
	}
104

    
105
	/**
106
	 * Checks for path.
107
	 * 
108
	 * @param path
109
	 *            the path
110
	 * @return the boolean
111
	 */
112
	public Boolean hasPath(final String path) {
113
		return getConfig().hasPath(path);
114
	}
115

    
116
	/**
117
	 * Gets the config.
118
	 * 
119
	 * @return the config
120
	 */
121
	public com.typesafe.config.Config getConfig() {
122
		return config;
123
	}
124

    
125
}
(6-6/9)