OSDN Git Service

change kgram format master
authorHaruaki Tamada <tamadaf@kecafebabe.jp>
Thu, 27 Nov 2014 10:13:38 +0000 (19:13 +0900)
committerHaruaki Tamada <tamadaf@kecafebabe.jp>
Thu, 27 Nov 2014 10:13:38 +0000 (19:13 +0900)
kgram/src/main/java/jp/sourceforge/stigmata/birthmarks/kgram/KGram.java
kgram/src/main/java/jp/sourceforge/stigmata/birthmarks/kgram/KGramBasedBirthmarkExtractor.java
kgram/src/test/java/jp/sourceforge/stigmata/birthmarks/kgram/KGramTest.java
wsp/pom.xml

index 7fc6df7..bdff039 100644 (file)
@@ -5,8 +5,8 @@ import java.lang.reflect.Array;
 import java.util.Arrays;
 
 /**
- * This class represents k-gram of the some sequence. 
- * 
+ * This class represents k-gram of the some sequence.
+ *
  * @author Haruaki TAMADA
  */
 public class KGram<T> implements Serializable{
@@ -24,7 +24,7 @@ public class KGram<T> implements Serializable{
     }
 
     /**
-     * sets k-value. 
+     * sets k-value.
      * @param kvalue the number of elements of this object.
      */
     public void setKValue(int kvalue){
@@ -44,12 +44,11 @@ public class KGram<T> implements Serializable{
      */
     @Override
     public String toString(){
-        StringBuffer buffer = new StringBuffer("{ ");
+        StringBuffer buffer = new StringBuffer();
         for(int i = 0; i < maxLength; i++){
-            if(i != 0) buffer.append(", ");
+            if(i != 0) buffer.append(" ");
             buffer.append(get(i));
         }
-        buffer.append(" }");
         return new String(buffer);
     }
 
@@ -93,14 +92,14 @@ public class KGram<T> implements Serializable{
 
     /**
      * adds value at last index.
-     * 
-     * this object is called with given 2 when following situation, 
+     *
+     * this object is called with given 2 when following situation,
      * <ul>
      *   <li>{ 1, 3, null, null } -&gt; { 1, 2, 3, null } and return 2<li>
      *   <li>{ 1, null, 3, null } -&gt; { 1, 2, 3, null } and return 1<li>
      *   <li>{ 1, 2, 3, 4 } -&gt; { 1, 2, 3, 4 } and return -1<li>
      * </ul>
-     * 
+     *
      * @param value value for addition.
      * @return added index.
      */
index 6a0135a..b8d4920 100644 (file)
@@ -1,5 +1,7 @@
 package jp.sourceforge.stigmata.birthmarks.kgram;
 
+import java.util.Iterator;
+
 import jp.sourceforge.stigmata.Birthmark;
 import jp.sourceforge.stigmata.BirthmarkContext;
 import jp.sourceforge.stigmata.BirthmarkElement;
@@ -7,6 +9,7 @@ import jp.sourceforge.stigmata.ExtractionUnit;
 import jp.sourceforge.stigmata.birthmarks.ASMBirthmarkExtractor;
 import jp.sourceforge.stigmata.birthmarks.BirthmarkExtractVisitor;
 import jp.sourceforge.stigmata.spi.BirthmarkService;
+import jp.sourceforge.stigmata.utils.ArrayIterator;
 
 import org.objectweb.asm.ClassWriter;
 
@@ -24,6 +27,23 @@ public class KGramBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
         super();
     }
 
+    @Override
+    public Iterator<String> getPropertyKeys(){
+        return new ArrayIterator<String>(new String[] { "KValue" });
+    }
+
+    @Override
+    public void setProperty(String key, Object value){
+        if(key.equalsIgnoreCase("kvalue")){
+            if(value instanceof Integer){
+                kvalue = ((Integer)value).intValue();
+            }
+            else if(value instanceof String){
+                kvalue = Integer.parseInt((String)value);
+            }
+        }
+    }
+
     public void setKValue(int kvalue){
         this.kvalue = kvalue;
     }
@@ -33,9 +53,9 @@ public class KGramBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
     }
 
     @Override
-    public BirthmarkExtractVisitor createExtractVisitor(ClassWriter writer, 
+    public BirthmarkExtractVisitor createExtractVisitor(ClassWriter writer,
             Birthmark birthmark, BirthmarkContext context){
-        KGramBasedBirthmarkExtractVisitor extractor = 
+        KGramBasedBirthmarkExtractVisitor extractor =
             new KGramBasedBirthmarkExtractVisitor(writer, birthmark, context);
         extractor.setKValue(getKValue());
         return extractor;
@@ -45,7 +65,7 @@ public class KGramBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
     public ExtractionUnit[] getAcceptableUnits(){
         return new ExtractionUnit[] {
             ExtractionUnit.CLASS, ExtractionUnit.PACKAGE,
-            ExtractionUnit.ARCHIVE, 
+            ExtractionUnit.ARCHIVE,
         };
     }
 
@@ -53,15 +73,12 @@ public class KGramBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
     @Override
     public BirthmarkElement buildElement(String value) {
         value = value.trim();
-        if(value.startsWith("{") && value.endsWith("}")){
-            String[] param = 
-                value.substring(1, value.length() - 1).split(", *");
-            KGram<Integer> kgram = new KGram<Integer>(param.length);
-            for(int i = 0; i < param.length; i++){
-                kgram.set(i, new Integer(param[i].trim()));
-            }
-            return new KGramBasedBirthmarkElement<Integer>(kgram);
+        String[] param =
+            value.substring(1, value.length() - 1).split(" *");
+        KGram<Integer> kgram = new KGram<Integer>(param.length);
+        for(int i = 0; i < param.length; i++){
+            kgram.set(i, new Integer(param[i].trim()));
         }
-        return null;
+        return new KGramBasedBirthmarkElement<Integer>(kgram);
     }
 }
index b34ecb2..22b2564 100644 (file)
@@ -5,7 +5,7 @@ import org.junit.Before;
 import org.junit.Test;
 
 /**
- * 
+ *
  * @author Haruaki Tamada
  */
 public class KGramTest{
@@ -19,6 +19,15 @@ public class KGramTest{
     }
 
     @Test
+    public void testStringRepresentation(){
+        KGram<String>[] kgrams = KGramBuilder.getInstance().buildKGram(plainValues, 4);
+        Assert.assertEquals("a b c d", kgrams[0].toString());
+        Assert.assertEquals("b c d e", kgrams[1].toString());
+        Assert.assertEquals("c d e f", kgrams[2].toString());
+        Assert.assertEquals("d e f g", kgrams[3].toString());
+    }
+
+    @Test
     public void checkPlainKGram(){
         KGram<String>[] kgrams = KGramBuilder.getInstance().buildKGram(plainValues, 4);
         Assert.assertEquals(4, kgrams.length);
index 3df8e30..c48d77f 100644 (file)
@@ -8,7 +8,7 @@
   <parent>\r
     <groupId>jp.sourceforge.stigmata</groupId>\r
     <artifactId>stigmata-plugins</artifactId>\r
-    <version>4.0-SNAPSHOT</version>\r
+    <version>4.0</version>\r
   </parent>\r
 \r
   <groupId>jp.sourceforge.stigmata.plugins</groupId>\r