From c4b20d52fa50404b6b8d0a910a41b3fc47cfb90e Mon Sep 17 00:00:00 2001
From: matthew_swift <matthew_swift@localhost>
Date: Tue, 24 Apr 2007 08:21:24 +0000
Subject: [PATCH] This change (no associated issue no.) addresses some counter-intuitive behavior discovered when specifying some duration based properties:

---
 opends/src/server/org/opends/server/admin/SizeUnit.java |   49 +++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/opends/src/server/org/opends/server/admin/SizeUnit.java b/opends/src/server/org/opends/server/admin/SizeUnit.java
index e4d4913..df735b2 100644
--- a/opends/src/server/org/opends/server/admin/SizeUnit.java
+++ b/opends/src/server/org/opends/server/admin/SizeUnit.java
@@ -121,17 +121,39 @@
 
 
   /**
-   * Parse the provided size string and return its equivalent size in bytes.
+   * Parse the provided size string and return its equivalent size in
+   * bytes. The size string must specify the unit e.g. "10kb".
    *
    * @param s
    *          The size string to be parsed.
-   * @return Returns the parsed size in bytes.
+   * @return Returns the parsed duration in bytes.
    * @throws NumberFormatException
    *           If the provided size string could not be parsed.
    */
   public static long parseValue(String s) throws NumberFormatException {
+    return parseValue(s, null);
+  }
+
+
+
+  /**
+   * Parse the provided size string and return its equivalent size in
+   * bytes.
+   *
+   * @param s
+   *          The size string to be parsed.
+   * @param defaultUnit
+   *          The default unit to use if there is no unit specified in
+   *          the size string, or <code>null</code> if the string
+   *          must always contain a unit.
+   * @return Returns the parsed size in bytes.
+   * @throws NumberFormatException
+   *           If the provided size string could not be parsed.
+   */
+  public static long parseValue(String s, SizeUnit defaultUnit)
+      throws NumberFormatException {
     // Value must be a floating point number followed by a unit.
-    Pattern p = Pattern.compile("^\\s*(\\d+(\\.\\d+)?)\\s*(\\w+)\\s*$");
+    Pattern p = Pattern.compile("^\\s*(\\d+(\\.\\d+)?)\\s*(\\w+)?\\s*$");
     Matcher m = p.matcher(s);
 
     if (!m.matches()) {
@@ -147,12 +169,23 @@
     }
 
     // Group 3 is the unit.
-    try {
-      SizeUnit u = getUnit(m.group(3));
-      return u.toBytes(d);
-    } catch (IllegalArgumentException e) {
-      throw new NumberFormatException("Invalid size value \"" + s + "\"");
+    String unitString = m.group(3);
+    SizeUnit unit;
+    if (unitString == null) {
+      if (defaultUnit == null) {
+        throw new NumberFormatException("Invalid size value \"" + s + "\"");
+      } else {
+        unit = defaultUnit;
+      }
+    } else {
+      try {
+        unit = getUnit(unitString);
+      } catch (IllegalArgumentException e) {
+        throw new NumberFormatException("Invalid size value \"" + s + "\"");
+      }
     }
+
+    return unit.toBytes(d);
   }
 
   // The size of the unit in bytes.

--
Gitblit v1.10.0