Make `GProperties` support getting property with specified type

Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/88ed93e7
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/88ed93e7
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/88ed93e7

Branch: refs/heads/master
Commit: 88ed93e7c8d77a9c5c9263ba7039d68f420a69b4
Parents: 1d20e30
Author: Daniel Sun <sun...@apache.org>
Authored: Sun Nov 4 23:17:47 2018 +0800
Committer: Daniel Sun <sun...@apache.org>
Committed: Sun Nov 4 23:43:06 2018 +0800

----------------------------------------------------------------------
 src/main/groovy/groovy/util/GProperties.groovy  | 109 +++++++++++++++++
 .../groovy/util/gproperties.properties          |  10 +-
 src/test/groovy/util/GPropertiesTest.groovy     | 119 +++++++++++++++++++
 3 files changed, 237 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/88ed93e7/src/main/groovy/groovy/util/GProperties.groovy
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/util/GProperties.groovy 
b/src/main/groovy/groovy/util/GProperties.groovy
index fe42982..2a0686c 100644
--- a/src/main/groovy/groovy/util/GProperties.groovy
+++ b/src/main/groovy/groovy/util/GProperties.groovy
@@ -68,6 +68,17 @@ import java.util.regex.Pattern
  *      assert '{groovy.greeting},{some.name}' == 
gp.getProperty('greeting.daniel')
  * </pre>
  *
+ * 4) Getting property with the specified type(integer, long, boolean, etc.), 
e.g.
+ * <pre>
+ *      # gproperties.properties
+ *      property.integer=1104
+ *
+ *      // groovy script
+ *      def gp = new GProperties()
+ *      
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+ *      assert new Integer(1104) == gp.getInteger('property.integer')
+ * </pre>
+ *
  * @since 3.0.0
  */
 @CompileStatic
@@ -121,6 +132,104 @@ class GProperties extends Properties {
         }
     }
 
+    Character getCharacter(String key) {
+        getPropertyWithType(key) { String p ->
+            if (!p || p.length() > 1) {
+                throw new IllegalArgumentException("Invalid character: ${p}")
+            }
+
+            Character.valueOf(p as char)
+        }
+    }
+
+    Character getCharacter(String key, Character defaultValue) {
+        getDefaultIfAbsent(key, defaultValue) {
+            getCharacter(key)
+        }
+    }
+
+    Short getShort(String key) {
+        getPropertyWithType(key) { String p ->
+            Short.valueOf(p)
+        }
+    }
+
+    Short getShort(String key, Short defaultValue) {
+        getDefaultIfAbsent(key, defaultValue) {
+            getShort(key)
+        }
+    }
+
+    Integer getInteger(String key) {
+        getPropertyWithType(key) { String p ->
+            Integer.valueOf(p)
+        }
+    }
+
+    Integer getInteger(String key, Integer defaultValue) {
+        getDefaultIfAbsent(key, defaultValue) {
+            getInteger(key)
+        }
+    }
+
+    Long getLong(String key) {
+        getPropertyWithType(key) { String p ->
+            Long.valueOf(p)
+        }
+    }
+
+    Long getLong(String key, Long defaultValue) {
+        getDefaultIfAbsent(key, defaultValue) {
+            getLong(key)
+        }
+    }
+
+    Float getFloat(String key) {
+        getPropertyWithType(key) { String p ->
+            Float.valueOf(p)
+        }
+    }
+
+    Float getFloat(String key, Float defaultValue) {
+        getDefaultIfAbsent(key, defaultValue) {
+            getFloat(key)
+        }
+    }
+
+    Double getDouble(String key) {
+        getPropertyWithType(key) { String p ->
+            Double.valueOf(p)
+        }
+    }
+
+    Double getDouble(String key, Double defaultValue) {
+        getDefaultIfAbsent(key, defaultValue) {
+            getDouble(key)
+        }
+    }
+
+    Boolean getBoolean(String key) {
+        getPropertyWithType(key) { String p ->
+            Boolean.valueOf(p)
+        }
+    }
+
+    Boolean getBoolean(String key, Boolean defaultValue) {
+        getDefaultIfAbsent(key, defaultValue) {
+            getBoolean(key)
+        }
+    }
+
+    private <V> V getPropertyWithType(String key, Closure<V> c) {
+        def p = getProperty(key)
+        null == p ? null : c(p)
+    }
+
+    private <V> V getDefaultIfAbsent(String key, V defaultValue, Closure<V> c) 
{
+        def p = c(key)
+        null == p ? defaultValue : p
+    }
+
     @Override
     synchronized void load(Reader reader) throws IOException {
         reader.withReader { it ->

http://git-wip-us.apache.org/repos/asf/groovy/blob/88ed93e7/src/test-resources/groovy/util/gproperties.properties
----------------------------------------------------------------------
diff --git a/src/test-resources/groovy/util/gproperties.properties 
b/src/test-resources/groovy/util/gproperties.properties
index f7f7db3..93f69d5 100644
--- a/src/test-resources/groovy/util/gproperties.properties
+++ b/src/test-resources/groovy/util/gproperties.properties
@@ -21,4 +21,12 @@ 
import.properties=/groovy/util/gproperties_import.properties,/groovy/util/gprope
 groovy.greeting={greeting.word},{some.name}
 groovy.greeting.with.smile={groovy.greeting} :)
 groovy.greeting.with.missing=Hello,{none} {0}
-groovy.greeting.with.escapes=Hello,{{some.name}}
\ No newline at end of file
+groovy.greeting.with.escapes=Hello,{{some.name}}
+property.character=a
+property.character.invalid=abc
+property.short=126
+property.integer=1104
+property.long=181104
+property.float=18.1104f
+property.double=18.1104d
+property.boolean=true

http://git-wip-us.apache.org/repos/asf/groovy/blob/88ed93e7/src/test/groovy/util/GPropertiesTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/util/GPropertiesTest.groovy 
b/src/test/groovy/util/GPropertiesTest.groovy
index 8967d66..65a9c61 100644
--- a/src/test/groovy/util/GPropertiesTest.groovy
+++ b/src/test/groovy/util/GPropertiesTest.groovy
@@ -70,4 +70,123 @@ class GPropertiesTest extends GroovyTestCase {
 
         assert 'Hello,{some.name}' == 
gp.getProperty('groovy.greeting.with.escapes')
     }
+
+    void testGetCharacter() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Character('a' as char) == 
gp.getCharacter('property.character')
+        assert null == gp.getCharacter('property.character.missing')
+
+        try {
+            gp.getCharacter('property.character.invalid')
+            assert false
+        } catch (IllegalArgumentException e) {
+            assert e.message.contains('Invalid character')
+        }
+    }
+
+    void testGetCharacterWithDefault() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Character('a' as char) == 
gp.getCharacter('property.character', 'b' as char)
+        assert new Character('b' as char) == 
gp.getCharacter('property.character.missing', 'b' as char)
+    }
+
+    void testGetShort() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Short(126 as short) == gp.getShort('property.short')
+        assert null == gp.getShort('property.short.missing')
+    }
+
+    void testGetShortWithDefault() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Short(126 as short) == gp.getShort('property.short', 125 as 
short)
+        assert new Short(125 as short) == 
gp.getShort('property.short.missing', 125 as short)
+    }
+
+    void testGetInteger() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Integer(1104) == gp.getInteger('property.integer')
+        assert null == gp.getInteger('property.integer.missing')
+    }
+
+    void testGetIntegerWithDefault() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Integer(1104) == gp.getInteger('property.integer', 1101)
+        assert new Integer(1101) == gp.getInteger('property.integer.missing', 
1101)
+    }
+
+    void testGetLong() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Long(181104) == gp.getLong('property.long')
+        assert null == gp.getLong('property.long.missing')
+    }
+
+    void testGetLongWithDefault() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Long(181104) == gp.getLong('property.long', 181101)
+        assert new Long(181101) == gp.getLong('property.long.missing', 181101)
+    }
+
+    void testGetFloat() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Float(18.1104f) == gp.getFloat('property.float')
+        assert null == gp.getFloat('property.float.missing')
+    }
+
+    void testGetFloatWithDefault() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Float(18.1104f) == gp.getFloat('property.float', 18.1101f)
+        assert new Float(18.1101f) == gp.getFloat('property.float.missing', 
18.1101f)
+    }
+
+    void testGetDouble() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Double(18.1104d) == gp.getDouble('property.double')
+        assert null == gp.getDouble('property.double.missing')
+    }
+
+    void testGetDoubleWithDefault() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Double(18.1104d) == gp.getDouble('property.double', 
18.1101d)
+        assert new Double(18.1101d) == gp.getDouble('property.double.missing', 
18.1101d)
+    }
+
+    void testGetBoolean() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Boolean(true) == gp.getBoolean('property.boolean')
+        assert null == gp.getBoolean('property.boolean.missing')
+    }
+
+    void testGetBooleanWithDefault() {
+        def gp = new GProperties()
+        
gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert new Boolean(true) == gp.getBoolean('property.boolean', false)
+        assert new Boolean(false) == gp.getBoolean('property.boolean.missing', 
false)
+    }
 }

Reply via email to