You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tv...@apache.org on 2026/03/19 09:13:46 UTC
(commons-jcs) 02/11: Support Duration as a data type
This is an automated email from the ASF dual-hosted git repository.
tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit 578e11f3fbd20f0c464cfa7bb4eab8ab04de2087
Author: Thomas Vandahl <tv...@apache.org>
AuthorDate: Tue Mar 17 18:06:20 2026 +0100
Support Duration as a data type
---
.../jcs4/utils/config/ConfigurationBuilder.java | 37 +++++++++++-----------
.../commons/jcs4/utils/config/PropertySetter.java | 30 +++++++++++-------
.../jcs4/utils/config/PropertySetterUnitTest.java | 5 ++-
3 files changed, 41 insertions(+), 31 deletions(-)
diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/config/ConfigurationBuilder.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/config/ConfigurationBuilder.java
index 0b7eb7dc..ce35ea58 100644
--- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/config/ConfigurationBuilder.java
+++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/config/ConfigurationBuilder.java
@@ -22,11 +22,11 @@ package org.apache.commons.jcs4.utils.config;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.RecordComponent;
+import java.time.Duration;
+import java.time.format.DateTimeParseException;
import java.util.HashMap;
-import java.util.Locale;
import java.util.Map;
import java.util.Properties;
-import java.util.regex.Pattern;
import org.apache.commons.jcs4.log.Log;
@@ -111,11 +111,7 @@ public class ConfigurationBuilder<T>
for (RecordComponent component : components)
{
- // Capitalize first letter of component name
- String name = Pattern.compile("^.").matcher(component.getName()).replaceFirst(
- m -> m.group().toUpperCase(Locale.ENGLISH));
-
- String fullKey = String.join(".", prefix, name);
+ String fullKey = String.join(".", prefix, component.getName());
String value = OptionConverter.findAndSubst(fullKey, props);
if (value != null)
@@ -188,24 +184,17 @@ public class ConfigurationBuilder<T>
{
return v;
}
- if ( Integer.TYPE.isAssignableFrom( type ) )
+ else if ( Integer.TYPE.isAssignableFrom( type ) )
{
return Integer.valueOf( v );
}
- if ( Long.TYPE.isAssignableFrom( type ) )
+ else if ( Long.TYPE.isAssignableFrom( type ) )
{
return Long.valueOf( v );
}
- if ( Boolean.TYPE.isAssignableFrom( type ) )
+ else if ( Boolean.TYPE.isAssignableFrom( type ) )
{
- if (Boolean.parseBoolean(v))
- {
- return Boolean.TRUE;
- }
- else
- {
- return Boolean.FALSE;
- }
+ return Boolean.valueOf(v);
}
else if( type.isEnum() )
{
@@ -217,6 +206,18 @@ public class ConfigurationBuilder<T>
{
return new File( v );
}
+ else if ( Duration.class.isAssignableFrom( type ) )
+ {
+ try
+ {
+ return Duration.parse(v);
+ }
+ catch (DateTimeParseException e)
+ {
+ log.warn("Parsing Duration failed for {0}, assuming milliseconds", v);
+ return Duration.ofMillis(Long.parseLong(v));
+ }
+ }
log.warn("Unknown type for conversion: {0} and value {1}", type, value);
return null;
diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/config/PropertySetter.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/config/PropertySetter.java
index bbc7687c..fbbff84b 100644
--- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/config/PropertySetter.java
+++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/config/PropertySetter.java
@@ -25,6 +25,8 @@ import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.Method;
+import java.time.Duration;
+import java.time.format.DateTimeParseException;
import java.util.Properties;
import org.apache.commons.jcs4.log.Log;
@@ -102,24 +104,17 @@ public class PropertySetter
{
return val;
}
- if ( Integer.TYPE.isAssignableFrom( type ) )
+ else if ( Integer.TYPE.isAssignableFrom( type ) )
{
return Integer.valueOf( v );
}
- if ( Long.TYPE.isAssignableFrom( type ) )
+ else if ( Long.TYPE.isAssignableFrom( type ) )
{
return Long.valueOf( v );
}
- if ( Boolean.TYPE.isAssignableFrom( type ) )
+ else if ( Boolean.TYPE.isAssignableFrom( type ) )
{
- if ( "true".equalsIgnoreCase( v ) )
- {
- return Boolean.TRUE;
- }
- if ( "false".equalsIgnoreCase( v ) )
- {
- return Boolean.FALSE;
- }
+ return Boolean.valueOf(v);
}
else if( type.isEnum() )
{
@@ -131,6 +126,18 @@ public class PropertySetter
{
return new File( v );
}
+ else if ( Duration.class.isAssignableFrom( type ) )
+ {
+ try
+ {
+ return Duration.parse(v);
+ }
+ catch (DateTimeParseException e)
+ {
+ log.warn("Parsing Duration failed for {0}, assuming milliseconds", v);
+ return Duration.ofMillis(Long.parseLong(v));
+ }
+ }
return null;
}
@@ -201,7 +208,6 @@ public class PropertySetter
setProperty( key.substring( len ), value );
}
}
-
}
/**
diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/config/PropertySetterUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/config/PropertySetterUnitTest.java
index 9218619b..6c5ed732 100644
--- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/config/PropertySetterUnitTest.java
+++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/config/PropertySetterUnitTest.java
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import java.io.File;
+import java.time.Duration;
import org.junit.jupiter.api.Test;
@@ -54,6 +55,8 @@ class PropertySetterUnitTest
final Object f = ps.convertArg("test.conf", File.class);
assertInstanceOf( File.class, f, "Should be a file" );
- }
+ final Object d = ps.convertArg("PT5s", Duration.class);
+ assertInstanceOf( Duration.class, d, "Should be a duration" );
+ }
}