Revision: 17982
Author:   oleg.kulikoff
Date:     Tue Apr 19 06:09:10 2011
Log:      Issue 2502:   Add divide function
http://code.google.com/p/mobicents/source/detail?r=17982

Modified:
/trunk/servers/media/spi/src/main/java/org/mobicents/media/server/utils/Text.java /trunk/servers/media/spi/src/test/java/org/mobicents/media/server/utils/TextTest.java

=======================================
--- /trunk/servers/media/spi/src/main/java/org/mobicents/media/server/utils/Text.java Wed Mar 9 07:25:49 2011 +++ /trunk/servers/media/spi/src/main/java/org/mobicents/media/server/utils/Text.java Tue Apr 19 06:09:10 2011
@@ -73,7 +73,7 @@
     }

     /**
-     * Creates new instance and straing it into memory.
+     * Creates new instance and straining it into memory.
      * @param data memory
      * @param pos initial position
      * @param len the length from the initial position
@@ -113,6 +113,44 @@
         return (char) chars[pos + index];
     }

+    /**
+     * Copies reference to another text object.
+     *
+     * @param destination the another text object
+     */
+    public void copy(Text destination) {
+        destination.strain(chars, pos, len);
+    }
+
+    /**
+     * Divides text into parts using given separator and writes results
+     * into the parts array.
+     *
+     * @param separator the character used for splitting
+     * @param parts the array used to hold parts of the text
+     * @return the number of parts.
+     */
+    public int divide(char separator, Text[] parts) {
+        int pointer = pos;
+        int limit = pos + len;
+        int mark = pointer;
+        int count = 0;
+
+        while (pointer < limit) {
+            if (chars[pointer] == separator) {
+                parts[count].strain(chars, mark, pointer - mark);
+                mark = pointer + 1;
+                count++;
+            }
+            pointer++;
+        }
+
+        parts[count].strain(chars, mark, limit - mark);
+        count++;
+
+        return count;
+    }
+
     /**
      * (Non Java-doc.)
      * @see java.lang.CharSequence#subSequence(int, int);
@@ -146,7 +184,7 @@
     }

     /**
-     * Removes whitespaces from the head and tail of the string.
+     * Removes whitespace from the head and tail of the string.
      *
      */
     public void trim() {
@@ -237,7 +275,7 @@
      *
      * @param c1 the first char
      * @param c2 the second char
-     * @return tru if first and second chars are different.
+     * @return true if first and second chars are different.
      */
     private boolean differentChars(char c1, char c2) {
         if (65 <= c1 && c1 < 97) {
=======================================
--- /trunk/servers/media/spi/src/test/java/org/mobicents/media/server/utils/TextTest.java Tue Jan 25 08:39:03 2011 +++ /trunk/servers/media/spi/src/test/java/org/mobicents/media/server/utils/TextTest.java Tue Apr 19 06:09:10 2011
@@ -157,6 +157,25 @@
         assertEquals("test2", t.toString());
     }

+    @Test
+    public void testDivide() {
+        Text t1 = new Text("test test1 test2");
+
+        Text t2 = new Text();
+        Text t3 = new Text();
+        Text t4 = new Text();
+
+        int count = t1.divide(' ', new Text[] {t2,t3,t4});
+
+        assertEquals(3, count);
+
+        assertEquals("test", t2.toString());
+
+        assertEquals("test1", t3.toString());
+
+        assertEquals("test2", t4.toString());
+    }
+
     @Test
     public void testSplitExecutionTime() {
         Text t1 = new Text("test test1 test2");
@@ -198,6 +217,21 @@
         assertEquals("test2", t.toString());
     }

+    @Test
+    public void testCopy() {
+        Text t1 = new Text("hello");
+
+        Text t = new Text();
+        t1.copy(t);
+
+        assertEquals("hello", t.toString());
+
+        byte[] data = "world".getBytes();
+        t1.strain(data, 0, data.length);
+
+        assertEquals("hello", t.toString());
+    }
+
     @Test
     public void testMoreLines() {
         Text t1 = new Text("test\ntest1\ntest2");

Reply via email to