ctf: Hide internal functionalities of StringDefinition.
authorEtienne Bergeron <etienne.bergeron@gmail.com>
Tue, 3 Dec 2013 09:20:12 +0000 (04:20 -0500)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Wed, 4 Dec 2013 14:25:02 +0000 (09:25 -0500)
The StringDefinition should not expose its internal data structures.
The StringBuffer is a temporary buffer used by read. Nothing outside
the 'read' method should ever play with this object.

Change-Id: I2edda57f9208f4d1a56a3aa619545f803ea4278d
Signed-off-by: Etienne Bergeron <etienne.bergeron@gmail.com>
Reviewed-on: https://git.eclipse.org/r/19235
Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
IP-Clean: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Tested-by: Hudson CI
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/event/CTFEventFieldTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDefinitionTest.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StringDefinition.java

index aebbc4d4b9289f3b1d3842adcac8a974d5cb9527..379dee49546b472e51521a3ed6c367d7c1a0483c 100644 (file)
@@ -105,7 +105,7 @@ public class CTFEventFieldTest {
     public void testParseField_simple3() {
         StringDefinition fieldDef = new StringDefinition(
                 new StringDeclaration(), null, fieldName);
-        fieldDef.setString(new StringBuilder("Hello World"));
+        fieldDef.setValue("Hello World");
 
         String other = "\"Hello World\"";
         assertNotNull(fieldDef);
index 8044e72ff867306293a2a72b7c441ca691db8d06..cb07c76dedd90d91736dcea193df3485e791a51d 100644 (file)
@@ -111,7 +111,7 @@ public class ArrayDefinitionTest {
             String content = "test" + i;
             defs[i] = new StringDefinition(
                     new StringDeclaration(Encoding.UTF8), null, content);
-            defs[i].setString(new StringBuilder(content));
+            defs[i].setValue(content);
         }
         return defs;
     }
index aa890612b89ebad29b0f960558637a7d706a5248..2d263ec20e4d3cd6bfdbc2519abd6eb44f4de26e 100644 (file)
@@ -11,6 +11,7 @@
 
 package org.eclipse.linuxtools.ctf.core.tests.types;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
@@ -63,29 +64,28 @@ public class StringDefinitionTest {
      */
     @Test
     public void testGetDeclaration() {
-        fixture.setString(new StringBuilder());
         StringDeclaration result = fixture.getDeclaration();
         assertNotNull(result);
     }
 
     /**
-     * Run the StringBuilder getString() method test.
+     * Run the String getValue() method test.
      */
     @Test
-    public void testGetString() {
-        fixture.setString(new StringBuilder());
-        StringBuilder result = fixture.getString();
+    public void testGetValue() {
+        String result = fixture.getValue();
         assertNotNull(result);
     }
 
     /**
-     * Run the String getValue() method test.
+     * Run the String setValue() method test.
      */
     @Test
-    public void testGetValue() {
-        fixture.setString(new StringBuilder());
+    public void testSetValue() {
+        fixture.setValue("dummy");
         String result = fixture.getValue();
         assertNotNull(result);
+        assertEquals("dummy", result);
     }
 
     /**
@@ -94,7 +94,6 @@ public class StringDefinitionTest {
      */
     @Test
     public void testRead() throws CTFReaderException {
-        fixture.setString(new StringBuilder());
         BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
         fixture.read(input);
     }
@@ -104,27 +103,15 @@ public class StringDefinitionTest {
      */
     @Test
     public void testSetDeclaration() {
-        fixture.setString(new StringBuilder());
         StringDeclaration declaration = new StringDeclaration();
         fixture.setDeclaration(declaration);
     }
 
-    /**
-     * Run the void setString(StringBuilder) method test.
-     */
-    @Test
-    public void testSetString() {
-        fixture.setString(new StringBuilder());
-        StringBuilder string = new StringBuilder();
-        fixture.setString(string);
-    }
-
     /**
      * Run the String toString() method test.
      */
     @Test
     public void testToString() {
-        fixture.setString(new StringBuilder());
         String result = fixture.toString();
         assertNotNull(result);
     }
index 6ed3c0c87df647a8b44b52536042cceadf5397c7..ff27e849be33c820dd96bf08857d8bced2fdf2b4 100644 (file)
@@ -32,9 +32,9 @@ public class StringDefinition extends Definition {
     // Attributes
     // ------------------------------------------------------------------------
 
-    private StringDeclaration declaration;
+    private StringDeclaration fDeclaration;
 
-    private StringBuilder string;
+    private String fString;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -54,9 +54,9 @@ public class StringDefinition extends Definition {
             IDefinitionScope definitionScope, String fieldName) {
         super(definitionScope, fieldName);
 
-        this.declaration = declaration;
+        fDeclaration = declaration;
 
-        string = new StringBuilder();
+        fString = ""; //$NON-NLS-1$
     }
 
     // ------------------------------------------------------------------------
@@ -65,7 +65,7 @@ public class StringDefinition extends Definition {
 
     @Override
     public StringDeclaration getDeclaration() {
-        return declaration;
+        return fDeclaration;
     }
 
     /**
@@ -75,35 +75,25 @@ public class StringDefinition extends Definition {
      *            the declaration
      */
     public void setDeclaration(StringDeclaration declaration) {
-        this.declaration = declaration;
+        fDeclaration = declaration;
     }
 
     /**
-     * Gets the string
-     *
-     * @return the stringbuilder
-     */
-    public StringBuilder getString() {
-        return string;
-    }
-
-    /**
-     * Sets a stringbuilder for the definition
+     * Gets the string (value)
      *
-     * @param string
-     *            the stringbuilder
+     * @return the string
      */
-    public void setString(StringBuilder string) {
-        this.string = string;
+    public String getValue() {
+        return fString;
     }
 
     /**
-     * Gets the string (value)
+     * Sets the string (value)
      *
-     * @return the string
+     * @param str the string
      */
-    public String getValue() {
-        return string.toString();
+    public void setValue(String str) {
+        fString = str;
     }
 
     // ------------------------------------------------------------------------
@@ -113,13 +103,15 @@ public class StringDefinition extends Definition {
     @Override
     public void read(BitBuffer input) throws CTFReaderException {
         /* Offset the buffer position wrt the current alignment */
-        alignRead(input, this.declaration);
-        string.setLength(0);
+        alignRead(input, fDeclaration);
+
+        StringBuilder sb = new StringBuilder();
         char c = (char) input.get(8, false);
         while (c != 0) {
-            string.append(c);
+            sb.append(c);
             c = (char) input.get(8, false);
         }
+        fString = sb.toString();
     }
 
     @Override
This page took 0.030358 seconds and 5 git commands to generate.