You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/11/04 16:44:52 UTC
groovy git commit: Make `GProperties` support getting property with
more specified type
Repository: groovy
Updated Branches:
refs/heads/master c04ba7206 -> 257f764dc
Make `GProperties` support getting property with more specified type
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/257f764d
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/257f764d
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/257f764d
Branch: refs/heads/master
Commit: 257f764dc17cb0c26aff4a83ba7d6a86d3a75614
Parents: c04ba72
Author: Daniel Sun <su...@apache.org>
Authored: Mon Nov 5 00:41:58 2018 +0800
Committer: Daniel Sun <su...@apache.org>
Committed: Mon Nov 5 00:43:53 2018 +0800
----------------------------------------------------------------------
src/main/groovy/groovy/util/GProperties.groovy | 48 +++++++++++--
.../groovy/util/gproperties.properties | 3 +
src/test/groovy/util/GPropertiesTest.groovy | 76 ++++++++++++++++----
3 files changed, 107 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/257f764d/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 19ecc58..e9565f0 100644
--- a/src/main/groovy/groovy/util/GProperties.groovy
+++ b/src/main/groovy/groovy/util/GProperties.groovy
@@ -178,6 +178,18 @@ class GProperties extends Properties {
}
}
+ Byte getByte(String key) {
+ getPropertyWithType(key) { String p ->
+ Byte.valueOf(p)
+ }
+ }
+
+ Byte getByte(String key, Byte defaultValue) {
+ getDefaultIfAbsent(key, defaultValue) {
+ getByte(key)
+ }
+ }
+
Short getShort(String key) {
getPropertyWithType(key) { String p ->
Short.valueOf(p)
@@ -250,14 +262,28 @@ class GProperties extends Properties {
}
}
- private <V> V getPropertyWithType(String key, Closure<V> c) {
- def p = getProperty(key)
- null == p ? null : c(p)
+ BigInteger getBigInteger(String key) {
+ getPropertyWithType(key) { String p ->
+ new BigInteger(p)
+ }
}
- private <V> V getDefaultIfAbsent(String key, V defaultValue, Closure<V> c) {
- def p = c(key)
- null == p ? defaultValue : p
+ BigInteger getBigInteger(String key, BigInteger defaultValue) {
+ getDefaultIfAbsent(key, defaultValue) {
+ getBigInteger(key)
+ }
+ }
+
+ BigDecimal getBigDecimal(String key) {
+ getPropertyWithType(key) { String p ->
+ new BigDecimal(p)
+ }
+ }
+
+ BigDecimal getBigDecimal(String key, BigDecimal defaultValue) {
+ getDefaultIfAbsent(key, defaultValue) {
+ getBigDecimal(key)
+ }
}
@Override
@@ -302,4 +328,14 @@ class GProperties extends Properties {
importPropertiesList << importProperties
}
}
+
+ 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
+ }
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/257f764d/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 93f69d5..e096c48 100644
--- a/src/test-resources/groovy/util/gproperties.properties
+++ b/src/test-resources/groovy/util/gproperties.properties
@@ -24,9 +24,12 @@ groovy.greeting.with.missing=Hello,{none} {0}
groovy.greeting.with.escapes=Hello,{{some.name}}
property.character=a
property.character.invalid=abc
+property.byte=1
property.short=126
property.integer=1104
property.long=181104
property.float=18.1104f
property.double=18.1104d
property.boolean=true
+property.biginteger=20181104
+property.bigdecimal=2018.1104
http://git-wip-us.apache.org/repos/asf/groovy/blob/257f764d/src/test/groovy/util/GPropertiesTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/util/GPropertiesTest.groovy b/src/test/groovy/util/GPropertiesTest.groovy
index 9a1186a..e7fb691 100644
--- a/src/test/groovy/util/GPropertiesTest.groovy
+++ b/src/test/groovy/util/GPropertiesTest.groovy
@@ -90,8 +90,24 @@ class GPropertiesTest extends GroovyTestCase {
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)
+ assert new Character('a' as char) == gp.getCharacter('property.character', new Character('b' as char))
+ assert new Character('b' as char) == gp.getCharacter('property.character.missing', new Character('b' as char))
+ }
+
+ void testGetByte() {
+ def gp = new GProperties()
+ gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+ assert new Byte(1 as byte) == gp.getByte('property.byte')
+ assert null == gp.getByte('property.byte.missing')
+ }
+
+ void testGetByteWithDefault() {
+ def gp = new GProperties()
+ gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+ assert new Byte(1 as byte) == gp.getByte('property.byte', new Byte(2 as byte))
+ assert new Byte(2 as byte) == gp.getByte('property.byte.missing', new Byte(2 as byte))
}
void testGetShort() {
@@ -106,8 +122,8 @@ class GPropertiesTest extends GroovyTestCase {
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)
+ assert new Short(126 as short) == gp.getShort('property.short', new Short(125 as short))
+ assert new Short(125 as short) == gp.getShort('property.short.missing', new Short(125 as short))
}
void testGetInteger() {
@@ -122,8 +138,8 @@ class GPropertiesTest extends GroovyTestCase {
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)
+ assert new Integer(1104) == gp.getInteger('property.integer', new Integer(1101))
+ assert new Integer(1101) == gp.getInteger('property.integer.missing', new Integer(1101))
}
void testGetLong() {
@@ -138,8 +154,8 @@ class GPropertiesTest extends GroovyTestCase {
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)
+ assert new Long(181104) == gp.getLong('property.long', new Long(181101))
+ assert new Long(181101) == gp.getLong('property.long.missing', new Long(181101))
}
void testGetFloat() {
@@ -154,8 +170,8 @@ class GPropertiesTest extends GroovyTestCase {
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)
+ assert new Float(18.1104f) == gp.getFloat('property.float', new Float(18.1101f))
+ assert new Float(18.1101f) == gp.getFloat('property.float.missing', new Float(18.1101f))
}
void testGetDouble() {
@@ -170,8 +186,8 @@ class GPropertiesTest extends GroovyTestCase {
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)
+ assert new Double(18.1104d) == gp.getDouble('property.double', new Double(18.1101d))
+ assert new Double(18.1101d) == gp.getDouble('property.double.missing', new Double(18.1101d))
}
void testGetBoolean() {
@@ -186,8 +202,40 @@ class GPropertiesTest extends GroovyTestCase {
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)
+ assert new Boolean(true) == gp.getBoolean('property.boolean', new Boolean(false))
+ assert new Boolean(false) == gp.getBoolean('property.boolean.missing', new Boolean(false))
+ }
+
+ void testGetBigInteger() {
+ def gp = new GProperties()
+ gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+ assert new BigInteger(20181104) == gp.getBigInteger('property.biginteger')
+ assert null == gp.getBigInteger('property.biginteger.missing')
+ }
+
+ void testGetBigIntegerWithDefault() {
+ def gp = new GProperties()
+ gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+ assert new BigInteger(20181104) == gp.getBigInteger('property.biginteger', new BigInteger(20181101))
+ assert new BigInteger(20181101) == gp.getBigInteger('property.biginteger.missing', new BigInteger(20181101))
+ }
+
+ void testGetBigDecimal() {
+ def gp = new GProperties()
+ gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+ assert new BigDecimal('2018.1104') == gp.getBigDecimal('property.bigdecimal')
+ assert null == gp.getBigDecimal('property.bigdecimal.missing')
+ }
+
+ void testGetBigDecimalWithDefault() {
+ def gp = new GProperties()
+ gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+ assert new BigDecimal('2018.1104') == gp.getBigDecimal('property.bigdecimal', new BigDecimal('2018.1101'))
+ assert new BigDecimal('2018.1101') == gp.getBigDecimal('property.bigdecimal.missing', new BigDecimal('2018.1101'))
}
void testConstructor() {