11package de .rwth .idsg .steve .utils ;
22
3+ import com .google .common .base .Strings ;
4+
35import java .io .FileNotFoundException ;
46import java .io .InputStream ;
57import java .util .Properties ;
@@ -26,15 +28,59 @@ public PropertiesFileLoader(String fileName) {
2628 }
2729 }
2830
31+ // -------------------------------------------------------------------------
32+ // Strict
33+ // -------------------------------------------------------------------------
34+
2935 public String getString (String key ) {
30- return prop .getProperty (key );
36+ String s = prop .getProperty (key );
37+
38+ if (s == null ) {
39+ throw new IllegalArgumentException ("The property '" + key + "' is not found" );
40+ }
41+
42+ if (s .isEmpty ()) {
43+ throw new IllegalArgumentException ("The property '" + key + "' has no value set" );
44+ }
45+
46+ return s ;
3147 }
3248
3349 public boolean getBoolean (String key ) {
34- return Boolean .parseBoolean (prop . getProperty (key ));
50+ return Boolean .parseBoolean (getString (key ));
3551 }
3652
3753 public int getInt (String key ) {
38- return Integer .parseInt (prop .getProperty (key ));
54+ return Integer .parseInt (getString (key ));
55+ }
56+
57+ // -------------------------------------------------------------------------
58+ // Return null if not set
59+ // -------------------------------------------------------------------------
60+
61+ public String getOptionalString (String key ) {
62+ String s = prop .getProperty (key );
63+ if (Strings .isNullOrEmpty (s )) {
64+ return null ;
65+ }
66+ return s ;
67+ }
68+
69+ public Boolean getOptionalBoolean (String key ) {
70+ String s = getOptionalString (key );
71+ if (s == null ) {
72+ return null ;
73+ } else {
74+ return Boolean .parseBoolean (s );
75+ }
76+ }
77+
78+ public Integer getOptionalInt (String key ) {
79+ String s = getOptionalString (key );
80+ if (s == null ) {
81+ return null ;
82+ } else {
83+ return Integer .parseInt (s );
84+ }
3985 }
4086}
0 commit comments