Author: areshetnyak
Date: 2011-11-04 06:54:33 -0400 (Fri, 04 Nov 2011)
New Revision: 5161
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/util/ConfigurationFormat.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/util/StringNumberParser.java
jcr/trunk/exo.jcr.component.core/src/main/resources/binding.xml
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/utils/StringNumberParserTest.java
Log:
EXOJCR-1566 : Serialize methods was added for long and int. Unit test was added.
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/util/ConfigurationFormat.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/util/ConfigurationFormat.java 2011-11-04
09:50:28 UTC (rev 5160)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/util/ConfigurationFormat.java 2011-11-04
10:54:33 UTC (rev 5161)
@@ -101,9 +101,37 @@
}
catch (Throwable e)
{
- LOG.warn("Unserialable time (as formated time) '" + time +
"'. Check StringNumberParser.serializeTime for details.", e);
+ LOG.warn("Unserialable time '" + time + "'. Check
StringNumberParser.serializeTime for details.", e);
return "";
}
}
+ public static String serializeInt(int integerValue)
+ {
+ try
+ {
+ return StringNumberParser.serializeInt(integerValue);
+ }
+ catch (Throwable e)
+ {
+ LOG.warn("Unserialable integer value '" + integerValue
+ + "'. Check StringNumberParser.serializeInt for details.", e);
+ return "";
+ }
+ }
+
+ public static String serializeLong(long longValue)
+ {
+ try
+ {
+ return StringNumberParser.serializeLong(longValue);
+ }
+ catch (Throwable e)
+ {
+ LOG.warn("Unserialable long value '" + longValue
+ + "'. Check StringNumberParser.serializeLong for details.",
e);
+ return "";
+ }
+ }
+
}
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/util/StringNumberParser.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/util/StringNumberParser.java 2011-11-04
09:50:28 UTC (rev 5160)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/util/StringNumberParser.java 2011-11-04
10:54:33 UTC (rev 5161)
@@ -47,6 +47,25 @@
}
/**
+ * Serialize given long as text. <br/>
+ *
+ * <br/>E.g. 2048 long will be returned as "2kb".
+ *
+ * <br/>Next formats are supported (case insensitive): <br/>kilobytes -
k,kb <br/>megabytes - m,mb
+ * <br/>gigabytes - g,gb <br/>terabytes - t,tb
+ *
+ * @param longValue
+ * - long
+ * @return String
+ * long representation as text
+ * @throws NumberFormatException
+ */
+ static public String serializeLong(final long longValue) throws NumberFormatException
+ {
+ return serializeNumber(longValue);
+ }
+
+ /**
* Parse given text as int. <br/>
*
* <br/>E.g. '2k' will be returned as 2048 number.
@@ -64,6 +83,25 @@
}
/**
+ * Serialize given int as text. <br/>
+ *
+ * <br/>E.g. 2048 nubber will be returned as "2kb".
+ *
+ * <br/>Next formats are supported (case insensitive): <br/>kilobytes -
k,kb <br/>megabytes - m,mb
+ * <br/>gigabytes - g,gb <br/>terabytes - t,tb
+ *
+ * @param integerValue
+ * - int
+ * @return String
+ * integer representation as text
+ * @throws NumberFormatException
+ */
+ static public String serializeInt(final int integerValue) throws
NumberFormatException
+ {
+ return serializeNumber(integerValue);
+ }
+
+ /**
* Parse given text as double. <br/>
*
* <br/>E.g. '2k' will be returned as 2048 number.
@@ -143,6 +181,45 @@
}
/**
+ * Serialize given number to text as number representation. <br/>
+ *
+ * <br/>E.g. 2048 number will be returned as 2kb.
+ *
+ * <br/>Next formats are supported: <br/>kilobytes - k,kb
<br/>megabytes - m,mb
+ * <br/>gigabytes - g,gb <br/>terabytes - t,tb
+ *
+ * @param number
+ * - long
+ * @return String
+ * - number representation
+ * @throws NumberFormatException
+ */
+ static public String serializeNumber(final long number) throws NumberFormatException
+ {
+ if ((number >= 1099511627776l) && (number % 1099511627776l) == 0)
+ {
+ return String.valueOf(number / 1099511627776l) + "TB";
+ }
+ else if ((number >= 1073741824l) && (number % 1073741824l) == 0)
+ {
+ return String.valueOf(number / 1073741824l) + "GB";
+ }
+ else if ((number >= 1048576l) && (number % 1048576l) == 0)
+ {
+ return String.valueOf(number / 1048576l) + "MB";
+ }
+
+ else if ((number >= 1024l) && (number % 1024l) == 0)
+ {
+ return String.valueOf(number / 1024l) + "KB";
+ }
+ else
+ {
+ return String.valueOf(number);
+ }
+ }
+
+ /**
* Parse given text as formated time and return a time in milliseconds. <br/>
<br/>Formats
* supported: <br/>milliseconds - ms <br/>seconds - without sufix
<br/>minutes - m <br/>hours - h
* <br/>days - d <br/>weeks - w
@@ -188,7 +265,8 @@
}
/**
- * Serialize given time in milliseconds and return a time as formated time in
milliseconds (ms).
+ * Serialize given time in milliseconds and return a time as formated time in ms, s,
h,d,w.
+ * Formats : ms - milliseconds, s - seconds, h hours, w - weeks.
*
* @param millisecondTime
* - time in milliseconds
@@ -198,6 +276,27 @@
*/
static public String serializeTime(final long millisecondTime) throws
NumberFormatException
{
+ if (millisecondTime >= 604800000 && (millisecondTime % 604800000) == 0)
+ {
+ return String.valueOf(millisecondTime / 604800000) + "w";
+ }
+ else if (millisecondTime >= 86400000 && (millisecondTime % 86400000) ==
0)
+ {
+ return String.valueOf(millisecondTime / 86400000) + "d";
+ }
+ else if (millisecondTime >= 3600000 && (millisecondTime % 3600000) ==
0)
+ {
+ return String.valueOf(millisecondTime / 3600000) + "h";
+ }
+ else if (millisecondTime >= 60000 && (millisecondTime % 60000) == 0)
+ {
+ return String.valueOf(millisecondTime / 60000) + "m";
+ }
+ else if (millisecondTime >= 1000 && (millisecondTime % 1000) == 0)
+ {
+ return String.valueOf(millisecondTime / 1000) + "s";
+ }
+
return String.valueOf(millisecondTime) + "ms";
}
}
Modified: jcr/trunk/exo.jcr.component.core/src/main/resources/binding.xml
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/resources/binding.xml 2011-11-04 09:50:28
UTC (rev 5160)
+++ jcr/trunk/exo.jcr.component.core/src/main/resources/binding.xml 2011-11-04 10:54:33
UTC (rev 5161)
@@ -20,16 +20,18 @@
<value name="session-max-age" field="sessionTimeOut"
usage="optional"
serializer="org.exoplatform.services.jcr.util.ConfigurationFormat.serializeTime"
deserializer="org.exoplatform.services.jcr.util.ConfigurationFormat.parseTime"
/>
- <value name="lock-remover-max-threads"
field="lockRemoverMaxThreadCount"
-
deserializer="org.exoplatform.services.jcr.util.ConfigurationFormat.parseInt"
usage="optional" />
+ <value name="lock-remover-max-threads"
field="lockRemoverMaxThreadCount" usage="optional"
+
serializer="org.exoplatform.services.jcr.util.ConfigurationFormat.serializeInt"
+
deserializer="org.exoplatform.services.jcr.util.ConfigurationFormat.parseInt"
/>
<value name="authentication-policy"
field="authenticationPolicy" />
<collection name="workspaces" field="workspaces"
item-type="org.exoplatform.services.jcr.config.WorkspaceEntry" />
</mapping>
<mapping name="workspace"
class="org.exoplatform.services.jcr.config.WorkspaceEntry">
<value name="name" field="name" style="attribute"
/>
- <value name="lazy-read-threshold" field="lazyReadThreshold"
-
deserializer="org.exoplatform.services.jcr.util.ConfigurationFormat.parseInt"
style="attribute" usage="optional" />
+ <value name="lazy-read-threshold" field="lazyReadThreshold"
style="attribute" usage="optional"
+
serializer="org.exoplatform.services.jcr.util.ConfigurationFormat.serializeInt"
+
deserializer="org.exoplatform.services.jcr.util.ConfigurationFormat.parseInt"
/>
<value name="auto-init-root-nodetype"
field="autoInitializedRootNt" style="attribute"
usage="optional" />
<value name="auto-init-permissions"
field="autoInitPermissions" style="attribute"
usage="optional" />
<structure name="container" field="container">
@@ -104,8 +106,9 @@
</mapping>
<mapping name="filter"
class="org.exoplatform.services.jcr.config.ValueStorageFilterEntry">
<value name="property-type" field="propertyType"
style="attribute" usage="optional" />
- <value name="min-value-size" field="minValueSize"
style="attribute"
-
deserializer="org.exoplatform.services.jcr.util.ConfigurationFormat.parseLong"
usage="optional" />
+ <value name="min-value-size" field="minValueSize"
style="attribute" usage="optional"
+
serializer="org.exoplatform.services.jcr.util.ConfigurationFormat.serializeLong"
+
deserializer="org.exoplatform.services.jcr.util.ConfigurationFormat.parseLong"
/>
<value name="ancestor-path" field="ancestorPath"
style="attribute" usage="optional" />
<value name="property-name" field="propertyName"
style="attribute" usage="optional" />
</mapping>
Modified:
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/utils/StringNumberParserTest.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/utils/StringNumberParserTest.java 2011-11-04
09:50:28 UTC (rev 5160)
+++
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/utils/StringNumberParserTest.java 2011-11-04
10:54:33 UTC (rev 5161)
@@ -46,6 +46,19 @@
assertEquals(1 * 1024 * 1024 * 1024, StringNumberParser.parseInt("1g"));
}
+ public void testSerializeInt()
+ {
+ assertEquals("1000", StringNumberParser.serializeInt(1000));
+
+ assertEquals("1KB", StringNumberParser.serializeInt(1024));
+
+ assertEquals("5KB", StringNumberParser.serializeInt(5 * 1024));
+
+ assertEquals("127MB", StringNumberParser.serializeInt(127 * 1024 *
1024));
+
+ assertEquals("1GB", StringNumberParser.serializeInt(1 * 1024 * 1024 *
1024));
+ }
+
public void testParseLong()
{
assertEquals(1000l, StringNumberParser.parseLong("1000"));
@@ -61,6 +74,21 @@
assertEquals(5l * 1024 * 1024 * 1024 * 1024,
StringNumberParser.parseLong("5TB"));
}
+ public void testSerializeLong()
+ {
+ assertEquals("1000", StringNumberParser.serializeLong(1000l));
+
+ assertEquals("1KB", StringNumberParser.serializeLong(1024l));
+
+ assertEquals("5KB", StringNumberParser.serializeLong(5l * 1024));
+
+ assertEquals("127MB", StringNumberParser.serializeLong(127l * 1024 *
1024));
+
+ assertEquals("4GB", StringNumberParser.serializeLong(4l * 1024 * 1024 *
1024));
+
+ assertEquals("5TB", StringNumberParser.serializeLong(5l * 1024 * 1024 *
1024 * 1024));
+ }
+
public void testParseNumber()
{
assertEquals(10.27d,
StringNumberParser.parseNumber("10.27").doubleValue());
@@ -83,4 +111,19 @@
assertEquals(12l, StringNumberParser.parseTime("12ms"));
}
+ public void testSerialiseTime()
+ {
+ assertEquals("63s", StringNumberParser.serializeTime(63l * 1000));
+
+ assertEquals("2m", StringNumberParser.serializeTime(2l * 60 * 1000));
+
+ assertEquals("15h", StringNumberParser.serializeTime(15l * 60 * 60 *
1000));
+
+ assertEquals("3d", StringNumberParser.serializeTime(3l * 24 * 60 * 60 *
1000));
+
+ assertEquals("5w", StringNumberParser.serializeTime(5l * 7 * 24 * 60 * 60
* 1000));
+
+ assertEquals("12ms", StringNumberParser.serializeTime(12l));
+ }
+
}