[logback-dev] svn commit: r917 - 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:58:17 CET 2006


Author: ceki
Date: Tue Nov 14 17:58:17 2006
New Revision: 917

Added:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/FileSize.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/FileSizeTest.java

Log:
adding support for filesize

Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/FileSize.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/FileSize.java	Tue Nov 14 17:58:17 2006
@@ -0,0 +1,68 @@
+/**
+ * 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 FileSize {
+
+  private final static String LENGTH_PART = "([0-9]+)";
+  private final static int DOUBLE_GROUP = 1;
+
+  private final static String UNIT_PART = "(|kb|mb|gb)s?";
+  private final static int UNIT_GROUP = 2;
+
+  private static final Pattern FILE_SIZE_PATTERN = Pattern.compile(LENGTH_PART
+      + "\\s*" + UNIT_PART, Pattern.CASE_INSENSITIVE);
+
+  static final long KB_COEFFICIENT = 1024;
+  static final long MB_COEFFICIENT = 1024 * KB_COEFFICIENT;
+  static final long GB_COEFFICIENT = 1024 * MB_COEFFICIENT;
+
+
+  final long size;
+
+  FileSize(long size) {
+    this.size = size;
+  }
+
+  long getSize() {
+    return size;
+  }
+
+  static FileSize valueOf(String fileSizeStr) {
+    Matcher matcher = FILE_SIZE_PATTERN.matcher(fileSizeStr);
+
+    long coefficient;
+    if (matcher.matches()) {
+      String lenStr = matcher.group(DOUBLE_GROUP);
+      String unitStr = matcher.group(UNIT_GROUP);
+
+      long lenValue = Long.valueOf(lenStr);
+      if (unitStr.equalsIgnoreCase("")) {
+        coefficient = 1;
+      } else if (unitStr.equalsIgnoreCase("kb")) {
+        coefficient = KB_COEFFICIENT;
+      } else if (unitStr.equalsIgnoreCase("mb")) {
+        coefficient = MB_COEFFICIENT;
+      } else if (unitStr.equalsIgnoreCase("gb")) {
+        coefficient = GB_COEFFICIENT;
+      }  else {
+        throw new IllegalStateException("Unexpected " + unitStr);
+      }
+      return new FileSize(lenValue * coefficient);
+    } else {
+      throw new IllegalArgumentException("String value [" + fileSizeStr
+          + "] is not in the expected format.");
+    }
+
+  }
+}

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/FileSizeTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/FileSizeTest.java	Tue Nov 14 17:58:17 2006
@@ -0,0 +1,60 @@
+/**
+ * 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 FileSizeTest extends TestCase {
+
+  static long KB_CO = 1024;
+  static long MB_CO = 1024*1024;
+  static long GB_CO = 1024*MB_CO;
+  
+  public FileSizeTest(String name) {
+    super(name);
+  }
+
+  protected void setUp() throws Exception {
+    super.setUp();
+  }
+
+  protected void tearDown() throws Exception {
+    super.tearDown();
+  }
+
+  public void test() {
+    {
+      FileSize fs = FileSize.valueOf("8");
+      assertEquals(8, fs.getSize());
+    }
+    
+    {
+      FileSize fs = FileSize.valueOf("8 kbs");
+      assertEquals(8*KB_CO, fs.getSize());
+    }
+  
+    {
+      FileSize fs = FileSize.valueOf("8 kb");
+      assertEquals(8*KB_CO, fs.getSize());
+    }
+    
+    {
+      FileSize fs = FileSize.valueOf("12 mb");
+      assertEquals(12*MB_CO, fs.getSize());
+    }
+
+    {
+      FileSize fs = FileSize.valueOf("5 GBs");
+      assertEquals(5*GB_CO, fs.getSize());
+    }
+
+  }
+}



More information about the logback-dev mailing list