[logback-dev] svn commit: r916 - 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
Tue Nov 14 17:37:41 CET 2006


Author: ceki
Date: Tue Nov 14 17:37:41 2006
New Revision: 916

Added:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Duration.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/DurationTest.java
Modified:
   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/PropertySetterTest.java

Log:
added support for setting duration values from strings

Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Duration.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Duration.java	Tue Nov 14 17:37:41 2006
@@ -0,0 +1,70 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework for Java.
+ * 
+ * Copyright (C) 2000-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.util;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+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 int UNIT_GROUP = 3;
+
+  private static final Pattern DURATION_PATTERN = Pattern.compile(DOUBLE_PART
+      + "\\s*" + UNIT_PART, Pattern.CASE_INSENSITIVE);
+
+  static final long SECONDS_COEFFICIENT = 1000;
+  static final long MINUTES_COEFFICIENT = 60 * SECONDS_COEFFICIENT;
+  static final long HOURS_COEFFICIENT = 60 * MINUTES_COEFFICIENT;
+  static final long DAYS_COEFFICIENT = 24 * HOURS_COEFFICIENT;
+
+  final long millis;
+
+  Duration(long millis) {
+    this.millis = millis;
+  }
+
+  long getMilliSeconds() {
+    return millis;
+  }
+
+  static Duration valueOf(String durationStr) {
+    Matcher matcher = DURATION_PATTERN.matcher(durationStr);
+
+    long coefficient;
+    if (matcher.matches()) {
+      String doubleStr = matcher.group(DOUBLE_GROUP);
+      String unitStr = matcher.group(UNIT_GROUP);
+
+      double doubleValue = Double.valueOf(doubleStr);
+      if (unitStr.equalsIgnoreCase("millisecond")) {
+        coefficient = 1;
+      } else if (unitStr.equalsIgnoreCase("second")) {
+        coefficient = SECONDS_COEFFICIENT;
+      } else if (unitStr.equalsIgnoreCase("minute")) {
+        coefficient = MINUTES_COEFFICIENT;
+      } else if (unitStr.equalsIgnoreCase("hour")) {
+        coefficient = HOURS_COEFFICIENT;
+      } else if (unitStr.equalsIgnoreCase("day")) {
+        coefficient = DAYS_COEFFICIENT;
+      } else {
+        throw new IllegalStateException("Unexpected" + unitStr);
+      }
+      return new Duration((long) (doubleValue * coefficient));
+    } else {
+      throw new IllegalArgumentException("String value [" + durationStr
+          + "] is not in the expected format.");
+    }
+
+  }
+}

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	Tue Nov 14 17:37:41 2006
@@ -231,7 +231,9 @@
         return X_AS_PROPERTY;
       } else if ("java.lang".equals(p.getName())) {
         return X_AS_PROPERTY;
-      } else {
+      } else if(Duration.class.isAssignableFrom(clazz)) {
+        return X_AS_PROPERTY;
+      }else {
         return X_AS_COMPONENT;
       }
     }
@@ -390,6 +392,8 @@
       } else if ("false".equalsIgnoreCase(v)) {
         return Boolean.FALSE;
       }
+    } else if(Duration.class.isAssignableFrom(type)) {
+      return Duration.valueOf(val);
     }
 
     return null;

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/DurationTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/DurationTest.java	Tue Nov 14 17:37:41 2006
@@ -0,0 +1,78 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework for Java.
+ * 
+ * Copyright (C) 2000-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package ch.qos.logback.core.util;
+
+import junit.framework.TestCase;
+
+public class DurationTest extends TestCase {
+
+  static long HOURS_CO = 60*60;
+  static long DAYS_CO = 24*60*60;
+  
+  public DurationTest(String name) {
+    super(name);
+  }
+
+  protected void setUp() throws Exception {
+    super.setUp();
+  }
+
+  protected void tearDown() throws Exception {
+    super.tearDown();
+  }
+
+  public void test() {
+    {
+      Duration d = Duration.valueOf("8 milliseconds");
+      assertEquals(8, d.getMilliSeconds());
+    }
+    
+    {
+      Duration d = Duration.valueOf("10.7 millisecond");
+      assertEquals(10, d.getMilliSeconds());
+    }
+    
+    {
+      Duration d = Duration.valueOf("10 seconds");
+      assertEquals(10 * 1000, d.getMilliSeconds());
+    }
+    
+    {
+      Duration d = Duration.valueOf("10.7 seconds");
+      assertEquals(10700, d.getMilliSeconds());
+    }
+    
+    {
+      Duration d = Duration.valueOf("1 minute");
+      assertEquals(1000*60, d.getMilliSeconds());
+    }
+    
+    {
+      Duration d = Duration.valueOf("2.2 minutes");
+      assertEquals(2200*60, d.getMilliSeconds());
+    }
+    
+    {
+      Duration d = Duration.valueOf("1 hour");
+      assertEquals(1000*HOURS_CO, d.getMilliSeconds());
+    }
+    
+    {
+      Duration d = Duration.valueOf("4.2 hours");
+      assertEquals(4200*HOURS_CO, d.getMilliSeconds());
+    }
+
+    {
+      Duration d = Duration.valueOf("5 days");
+      assertEquals(5000*DAYS_CO, d.getMilliSeconds());
+    }
+  }
+}

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	Tue Nov 14 17:37:41 2006
@@ -19,12 +19,15 @@
     assertEquals(ContainmentType.AS_SINGLE_PROPERTY, setter.canContainComponent("name"));
     assertEquals(ContainmentType.AS_SINGLE_PROPERTY, setter.canContainComponent("Name"));
     
+    assertEquals(ContainmentType.AS_SINGLE_PROPERTY, setter.canContainComponent("Duration"));
+    
     assertEquals(ContainmentType.AS_SINGLE_PROPERTY, setter.canContainComponent("open"));
     assertEquals(ContainmentType.AS_SINGLE_PROPERTY, setter.canContainComponent("Open"));
     
     assertEquals(ContainmentType.AS_COMPONENT_COLLECTION, setter.canContainComponent("Window"));
     assertEquals(ContainmentType.AS_PROPERTY_COLLECTION, setter.canContainComponent("adjective"));
     
+    
   }
 
   public void testSetProperty() {
@@ -105,6 +108,13 @@
     assertEquals(pool, house.getSwimmingPool());
   }
 
+  public void testDuration() {
+    House house = new House();
+    PropertySetter setter = new PropertySetter(house);
+    setter.setProperty("duration", "1.4 seconds");
+    assertEquals(1400, house.getDuration().getMilliSeconds());
+  }
+  
 }
 
 class House {
@@ -114,6 +124,7 @@
   String name;
   String camelCase;
   SwimmingPool pool;
+  Duration duration;
   
   List<String> adjectiveList = new ArrayList<String>();
   List<Window> windowList = new ArrayList<Window>();
@@ -174,6 +185,14 @@
   public void addAdjective(String s) {
     adjectiveList.add(s);
   }
+
+  public Duration getDuration() {
+    return duration;
+  }
+
+  public void setDuration(Duration duration) {
+    this.duration = duration;
+  }
 }
 
 class Door {



More information about the logback-dev mailing list