Skip to content

Commit

Permalink
Add ImmutableConfiguration.getDuration() methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Aug 25, 2021
1 parent 66b4f4e commit 01bae76
Show file tree
Hide file tree
Showing 6 changed files with 470 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Expand Up @@ -75,6 +75,9 @@
<action type="add" dev="ggregory" issue="CONFIGURATION-789" due-to="Gary Gregory">
Add ImmutableConfiguration.getEnum() methods.
</action>
<action type="add" dev="ggregory" issue="CONFIGURATION-789" due-to="Gary Gregory">
Add ImmutableConfiguration.getDuration() methods.
</action>
<!-- UPDATES -->
<action type="update" dev="ggregory" issue="CONFIGURATION-787" due-to="Gary Gregory">
Update from Apache Commons Lang 3.9 to 3.12.0.
Expand Down
Expand Up @@ -19,6 +19,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -986,6 +987,16 @@ public Double getDouble(final String key, final Double defaultValue) {
return convert(Double.class, key, defaultValue, false);
}

@Override
public Duration getDuration(final String key) {
return checkNonNullValue(key, convert(Duration.class, key, null, true));
}

@Override
public Duration getDuration(final String key, final Duration defaultValue) {
return convert(Duration.class, key, defaultValue, false);
}

@Override
public float getFloat(final String key) {
final Float f = convert(Float.class, key, null, true);
Expand Down
Expand Up @@ -18,11 +18,14 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Duration;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Properties;

import org.apache.commons.configuration2.convert.PropertyConverter;
import org.apache.commons.configuration2.ex.ConversionException;

/**
Expand Down Expand Up @@ -308,6 +311,38 @@ public interface ImmutableConfiguration {
*/
Double getDouble(String key, Double defaultValue);

/**
* Gets a {@link Duration} associated with the given configuration key.
*
* @param key The configuration key.
* @return The associated Duration if key is found and has valid format, default value otherwise.
* @throws org.apache.commons.configuration2.ex.ConversionException is thrown if the key maps to an object that is not a
* Duration.
* @since 2.8.0
*/
default Duration getDuration(final String key) {
final String string = getString(key);
if (string == null) {
throw new NoSuchElementException(key);
}
return PropertyConverter.toDuration(string);
}

/**
* Gets a {@link Duration} associated with the given configuration key.
*
* @param key The configuration key.
* @param defaultValue The default value.
* @return The associated Duration if key is found and has valid format, default value otherwise.
* @throws org.apache.commons.configuration2.ex.ConversionException is thrown if the key maps to an object that is not a
* Duration.
* @since 2.8.0
*/
default Duration getDuration(final String key, final Duration defaultValue) {
Object value = getProperty(key);
return value == null ? defaultValue : PropertyConverter.toDuration(value);
}

/**
* Gets the value of a string property that is stored in encoded form in this configuration using a default
* {@code ConfigurationDecoder}. This method works like the method with the same name, but it uses a default
Expand Down
Expand Up @@ -33,6 +33,8 @@
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.format.DateTimeParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
Expand All @@ -46,9 +48,9 @@
/**
* A utility class to convert the configuration properties into any type.
*
* @since 1.1
* @since 2.8.0
*/
final class PropertyConverter {
public final class PropertyConverter {

/** Constant for the prefix of hex numbers. */
private static final String HEX_PREFIX = "0x";
Expand Down Expand Up @@ -150,6 +152,8 @@ public static Object to(final Class<?> cls, final Object value, final DefaultCon
return toInternetAddress(value);
} else if (InetAddress.class.isAssignableFrom(cls)) {
return toInetAddress(value);
} else if (Duration.class.equals(cls)) {
return toDuration(value);
}

throw new ConversionException("The value '" + value + "' (" + value.getClass() + ")" + " can't be converted to a " + cls.getName() + " object");
Expand Down Expand Up @@ -285,6 +289,28 @@ public static Double toDouble(final Object value) throws ConversionException {
return Double.valueOf(n.doubleValue());
}

/**
* Convert the specified object into a Duration.
*
* @param value the value to convert
* @return the converted value
* @throws ConversionException thrown if the value cannot be converted to a Duration
* @since 2.8.0
*/
public static Duration toDuration(final Object value) throws ConversionException {
if (value instanceof Duration) {
return (Duration) value;
}
if (value instanceof CharSequence) {
try {
return Duration.parse((CharSequence) value);
} catch (DateTimeParseException e) {
throw new ConversionException("Could not convert " + value + " to Duration", e);
}
}
throw new ConversionException("The value " + value + " can't be converted to a Duration");
}

/**
* Convert the specified object into a BigInteger.
*
Expand Down
Expand Up @@ -26,6 +26,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -307,6 +308,29 @@ public void testGetDoubleUnknown() {
config.getDouble("numberNotInConfig");
}

@Test
public void testGetDuration() {
final Duration d = Duration.ofSeconds(1);
config.setProperty("durationD", d.toString());
final Duration oneD = Duration.ofSeconds(1);
final Duration twoD = Duration.ofSeconds(2);
assertEquals("This returns 1(Duration)", oneD, config.getDuration("durationD"));
assertEquals("This returns 1(Duration)", oneD, config.getDuration("durationD", twoD));
assertEquals("This returns 2(default Duration)", twoD, config.getDuration("numberNotInConfig", twoD));
assertEquals("This returns 1(Duration)", oneD, config.getDuration("durationD", twoD));
}

@Test(expected = ConversionException.class)
public void testGetDurationIncompatibleType() {
config.setProperty("test.empty", "");
config.getDuration("test.empty");
}

@Test(expected = NoSuchElementException.class)
public void testGetDurationUnknown() {
config.getDuration("numberNotInConfig");
}

@Test
public void testGetEnum() {
config.setProperty("testEnum", EnumFixture.SMALLTALK.name());
Expand Down

0 comments on commit 01bae76

Please sign in to comment.