[logback-dev] svn commit: r921 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/util test/java/ch/qos/logback/core/util

noreply.ceki at qos.ch noreply.ceki at qos.ch
Wed Nov 15 15:37:40 CET 2006


Author: ceki
Date: Wed Nov 15 15:37:40 2006
New Revision: 921

Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Duration.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/PropertySetter.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/DurationTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PropertySetterTest.java

Log:
Finalizing work on Duration and co.

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Duration.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Duration.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Duration.java	Wed Nov 15 15:37:40 2006
@@ -12,12 +12,25 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+/**
+ * Duration instances represent a lapse of time. Internally, the duration is stored
+ * in milliseconds.
+ * <p>
+ * 
+ * The {@link #valueOf} method can convert strings such as "3.5 minutes", "5 hours", into
+ * Duration instances. The recognized units of time are the "millisecond", "second", "minute"
+ * "hour" and "day". The unit name may be followed by an "s". Thus, "2 day" and "2 days" are
+ * equivalent. In the absence of a time unit specification, milliseconds are assumed.
+ * 
+ * 
+ * @author Ceki Gulcu
+ */
 public class Duration {
 
   private final static String DOUBLE_PART = "([0-9]*(.[0-9]+)?)";
   private final static int DOUBLE_GROUP = 1;
 
-  private final static String UNIT_PART = "(millisecond|second|minute|hour|day)s?";
+  private final static String UNIT_PART = "(|millisecond|second|minute|hour|day)s?";
   private final static int UNIT_GROUP = 3;
 
   private static final Pattern DURATION_PATTERN = Pattern.compile(DOUBLE_PART
@@ -30,7 +43,7 @@
 
   final long millis;
 
-  Duration(long millis) {
+  public Duration(long millis) {
     this.millis = millis;
   }
 
@@ -53,9 +66,12 @@
   public static Duration buildByDays(double value) {
     return new Duration((long) (DAYS_COEFFICIENT*value));
   }
+
+  public static Duration buildUnbounded() {
+    return new Duration(Long.MAX_VALUE);
+  }
   
-  
-  public long getMilliSeconds() {
+  public long getMilliseconds() {
     return millis;
   }
 
@@ -67,7 +83,7 @@
       String unitStr = matcher.group(UNIT_GROUP);
 
       double doubleValue = Double.valueOf(doubleStr);
-      if (unitStr.equalsIgnoreCase("millisecond")) {
+      if (unitStr.equalsIgnoreCase("millisecond") || unitStr.length() == 0) {
         return buildByMilliseconds(doubleValue);
       } else if (unitStr.equalsIgnoreCase("second")) {
         return buildBySeconds(doubleValue);

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/PropertySetter.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/PropertySetter.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/PropertySetter.java	Wed Nov 15 15:37:40 2006
@@ -147,11 +147,12 @@
     }
 
     Class[] paramTypes = setter.getParameterTypes();
-
+  
+    
     if (paramTypes.length != 1) {
       throw new PropertySetterException("#params for setter != 1");
     }
-
+    
     Object arg;
 
     try {

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/DurationTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/DurationTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/DurationTest.java	Wed Nov 15 15:37:40 2006
@@ -31,48 +31,53 @@
 
   public void test() {
     {
+      Duration d = Duration.valueOf("12");
+      assertEquals(12, d.getMilliseconds());
+    }
+    
+    {
       Duration d = Duration.valueOf("8 milliseconds");
-      assertEquals(8, d.getMilliSeconds());
+      assertEquals(8, d.getMilliseconds());
     }
     
     {
       Duration d = Duration.valueOf("10.7 millisecond");
-      assertEquals(10, d.getMilliSeconds());
+      assertEquals(10, d.getMilliseconds());
     }
     
     {
       Duration d = Duration.valueOf("10 seconds");
-      assertEquals(10 * 1000, d.getMilliSeconds());
+      assertEquals(10 * 1000, d.getMilliseconds());
     }
     
     {
       Duration d = Duration.valueOf("10.7 seconds");
-      assertEquals(10700, d.getMilliSeconds());
+      assertEquals(10700, d.getMilliseconds());
     }
     
     {
       Duration d = Duration.valueOf("1 minute");
-      assertEquals(1000*60, d.getMilliSeconds());
+      assertEquals(1000*60, d.getMilliseconds());
     }
     
     {
       Duration d = Duration.valueOf("2.2 minutes");
-      assertEquals(2200*60, d.getMilliSeconds());
+      assertEquals(2200*60, d.getMilliseconds());
     }
     
     {
       Duration d = Duration.valueOf("1 hour");
-      assertEquals(1000*HOURS_CO, d.getMilliSeconds());
+      assertEquals(1000*HOURS_CO, d.getMilliseconds());
     }
     
     {
       Duration d = Duration.valueOf("4.2 hours");
-      assertEquals(4200*HOURS_CO, d.getMilliSeconds());
+      assertEquals(4200*HOURS_CO, d.getMilliseconds());
     }
 
     {
       Duration d = Duration.valueOf("5 days");
-      assertEquals(5000*DAYS_CO, d.getMilliSeconds());
+      assertEquals(5000*DAYS_CO, d.getMilliseconds());
     }
   }
 }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java	Wed Nov 15 15:37:40 2006
@@ -10,6 +10,8 @@
 
 	public static Test suite() {
     TestSuite suite = new TestSuite();
+    suite.addTestSuite(DurationTest.class);
+    suite.addTestSuite(PackageTest.class);
     suite.addTestSuite(PropertySetterTest.class);
     return suite;
   }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PropertySetterTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PropertySetterTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PropertySetterTest.java	Wed Nov 15 15:37:40 2006
@@ -113,7 +113,7 @@
     House house = new House();
     PropertySetter setter = new PropertySetter(house);
     setter.setProperty("duration", "1.4 seconds");
-    assertEquals(1400, house.getDuration().getMilliSeconds());
+    assertEquals(1400, house.getDuration().getMilliseconds());
   }
 
   public void testFileSize() {



More information about the logback-dev mailing list