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{
}
/**
- * sets k-value.
+ * sets k-value.
* @param kvalue the number of elements of this object.
*/
public void setKValue(int kvalue){
*/
@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);
}
/**
* 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 } -> { 1, 2, 3, null } and return 2<li>
* <li>{ 1, null, 3, null } -> { 1, 2, 3, null } and return 1<li>
* <li>{ 1, 2, 3, 4 } -> { 1, 2, 3, 4 } and return -1<li>
* </ul>
- *
+ *
* @param value value for addition.
* @return added index.
*/
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;
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;
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;
}
}
@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;
public ExtractionUnit[] getAcceptableUnits(){
return new ExtractionUnit[] {
ExtractionUnit.CLASS, ExtractionUnit.PACKAGE,
- ExtractionUnit.ARCHIVE,
+ ExtractionUnit.ARCHIVE,
};
}
@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);
}
}
import org.junit.Test;
/**
- *
+ *
* @author Haruaki Tamada
*/
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);