From d5a18a324ec87740429780265702686837b09a6a Mon Sep 17 00:00:00 2001
From: matthew_swift <matthew_swift@localhost>
Date: Fri, 01 Jun 2007 17:15:27 +0000
Subject: [PATCH] Modify the DurationPropertyDefinition and DurationUnit classes so that they have more user-friendly encode/decode methods:
---
opendj-sdk/opends/src/server/org/opends/server/admin/DurationPropertyDefinition.java | 43 +++++++------------------------------------
1 files changed, 7 insertions(+), 36 deletions(-)
diff --git a/opendj-sdk/opends/src/server/org/opends/server/admin/DurationPropertyDefinition.java b/opendj-sdk/opends/src/server/org/opends/server/admin/DurationPropertyDefinition.java
index 1d1915e..d9346d9 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/admin/DurationPropertyDefinition.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/admin/DurationPropertyDefinition.java
@@ -29,11 +29,9 @@
-import static org.opends.server.util.Validator.ensureNotNull;
+import static org.opends.server.util.Validator.*;
import java.util.EnumSet;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
@@ -526,48 +524,21 @@
}
}
- // Value must be a floating point number followed by a unit.
- Pattern p = Pattern.compile("^\\s*(\\d+(\\.\\d+)?)\\s*(\\w+)\\s*$");
- Matcher m = p.matcher(value);
-
- if (!m.matches()) {
- throw new IllegalPropertyValueStringException(this, value);
- }
-
- // Group 1 is the float.
- double d;
+ // Parse the string representation.
+ long ms;
try {
- d = Double.valueOf(m.group(1));
+ ms = DurationUnit.parseValue(value);
} catch (NumberFormatException e) {
throw new IllegalPropertyValueStringException(this, value);
}
- // Group 3 is the unit.
- String unitString = m.group(3);
- DurationUnit unit;
- if (unitString == null) {
- unit = baseUnit;
- } else {
- try {
- unit = DurationUnit.getUnit(unitString);
- } catch (IllegalArgumentException e) {
- throw new IllegalPropertyValueStringException(this, value);
- }
- }
-
- // Check the unit is in range.
- if (unit.getDuration() < baseUnit.getDuration()) {
+ // Check the unit is in range - values must not be more granular
+ // than the base unit.
+ if ((ms % baseUnit.getDuration()) != 0) {
throw new IllegalPropertyValueStringException(this, value);
}
- if (maximumUnit != null) {
- if (unit.getDuration() > maximumUnit.getDuration()) {
- throw new IllegalPropertyValueStringException(this, value);
- }
- }
-
// Convert the value a long in the property's required unit.
- long ms = unit.toMilliSeconds(d);
Long i = (long) baseUnit.fromMilliSeconds(ms);
try {
validateValue(i);
--
Gitblit v1.10.0