From: tama3 Date: Tue, 25 Sep 2007 08:51:24 +0000 (+0000) Subject: update control flow X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=53cb0b2d48cbea683eb75721be7e82cc73d831bd;p=stigmata%2Fstigmata.git update control flow git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/stigmata/trunk@210 acee48c3-7b26-0410-bdac-b3d0e5314bbc --- diff --git a/src/main/java/jp/naist/se/stigmata/BirthmarkEngine.java b/src/main/java/jp/naist/se/stigmata/BirthmarkEngine.java index b690a57..72b0d84 100755 --- a/src/main/java/jp/naist/se/stigmata/BirthmarkEngine.java +++ b/src/main/java/jp/naist/se/stigmata/BirthmarkEngine.java @@ -9,11 +9,9 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Stack; @@ -35,11 +33,12 @@ import jp.naist.se.stigmata.reader.WarClassFileArchive; import jp.naist.se.stigmata.result.CertainPairComparisonResultSet; import jp.naist.se.stigmata.result.ExtractionResultSetFactory; import jp.naist.se.stigmata.result.RoundRobinComparisonResultSet; -import jp.naist.se.stigmata.spi.BirthmarkSpi; - -import org.apache.commons.beanutils.BeanUtils; /** + * This class is birthmarking engine. + * This class extracts birthmarks from given Java class files, compares results of extractions, and filters results of comparisons. + * + * This class is not thread safe. * * @author Haruaki Tamada * @version $Revision$ $Date$ @@ -53,11 +52,17 @@ public class BirthmarkEngine{ private OperationType targetType; private BirthmarkExtractorFactory factory; + /** + * constructor. + */ public BirthmarkEngine(BirthmarkEnvironment env){ this.environment = env; factory = new BirthmarkExtractorFactory(env); } + /** + * returns an environment of this object. + */ public BirthmarkEnvironment getEnvironment(){ return environment; } @@ -70,7 +75,14 @@ public class BirthmarkEngine{ listeners.remove(listener); } - public ComparisonResultSet filter(String[] target, BirthmarkContext context) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ + /** + * filters comparison of birthmarks from target files. + * @see #extract(String[], BirthmarkContext) + * @see #compare(String[], BirthmarkContext) + * @see #filter(ComparisonResultSet) + * @see BirthmarkContext#getFilterTypes() + */ + public synchronized ComparisonResultSet filter(String[] target, BirthmarkContext context) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ operationStart(OperationType.FILTER_BIRTHMARKS); ComparisonResultSet crs = compare(target, context); @@ -81,7 +93,14 @@ public class BirthmarkEngine{ return crs; } - public ComparisonResultSet filter(String[] targetX, String[] targetY, BirthmarkContext context) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ + /** + * filters comparison of birthmarks from given two targets targetx, and targetY + * @see #extract(String[], String[], BirthmarkContext) + * @see #compare(String[], String[], BirthmarkContext) + * @see #filter(ComparisonResultSet) + * @see BirthmarkContext#getFilterTypes() + */ + public synchronized ComparisonResultSet filter(String[] targetX, String[] targetY, BirthmarkContext context) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ operationStart(OperationType.FILTER_BIRTHMARKS); ComparisonResultSet crs = compare(targetX, targetY, context); @@ -92,7 +111,14 @@ public class BirthmarkEngine{ return crs; } - public ComparisonResultSet filter(ExtractionResultSet er) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ + /** + * filters comparison of birthmarks from given extraction result set. + * @see #compare(ExtractionResultSet) + * @see #filter(ComparisonResultSet) + * @see ExtractionResultSet#getContext() + * @see BirthmarkContext#getFilterTypes() + */ + public synchronized ComparisonResultSet filter(ExtractionResultSet er) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ operationStart(OperationType.FILTER_BIRTHMARKS); ComparisonResultSet crs = compare(er); @@ -103,7 +129,12 @@ public class BirthmarkEngine{ return crs; } - public ComparisonResultSet filter(ComparisonResultSet crs) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ + /** + * filters comparison of birthmarks. + * @see ExtractionResultSet#getContext() + * @see BirthmarkContext#getFilterTypes() + */ + public synchronized ComparisonResultSet filter(ComparisonResultSet crs) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ operationStart(OperationType.FILTER_BIRTHMARKS); String[] filterTypes = crs.getContext().getFilterTypes(); @@ -129,11 +160,14 @@ public class BirthmarkEngine{ return crs; } - public ComparisonPair compareDetails(BirthmarkSet bs1, BirthmarkSet bs2, BirthmarkContext context) throws BirthmarkComparisonFailedException{ + /** + * compares two given birthmarks and returns comparison pair. + */ + public synchronized ComparisonPair compareDetails(BirthmarkSet bs1, BirthmarkSet bs2, BirthmarkContext context) throws BirthmarkComparisonFailedException{ return new ComparisonPair(bs1, bs2, context); } - public ComparisonResultSet compare(String[] target, BirthmarkContext context) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ + public synchronized ComparisonResultSet compare(String[] target, BirthmarkContext context) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ operationStart(OperationType.COMPARE_BIRTHMARKS); ExtractionResultSet er = extract(target, context); @@ -144,7 +178,7 @@ public class BirthmarkEngine{ return crs; } - public ComparisonResultSet compare(String[] targetX, String[] targetY, BirthmarkContext context) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ + public synchronized ComparisonResultSet compare(String[] targetX, String[] targetY, BirthmarkContext context) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ operationStart(OperationType.COMPARE_BIRTHMARKS); ExtractionResultSet er = extract(targetX, targetY, context); @@ -155,7 +189,7 @@ public class BirthmarkEngine{ return crs; } - public ComparisonResultSet compare(ExtractionResultSet er) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ + public synchronized ComparisonResultSet compare(ExtractionResultSet er) throws BirthmarkExtractionFailedException, BirthmarkComparisonFailedException{ operationStart(OperationType.COMPARE_BIRTHMARKS); BirthmarkContext context = er.getContext(); @@ -181,46 +215,14 @@ public class BirthmarkEngine{ return crs; } - public ExtractionResultSet extract(String[] target, BirthmarkContext context) throws BirthmarkExtractionFailedException{ + public synchronized ExtractionResultSet extract(String[] target, BirthmarkContext context) throws BirthmarkExtractionFailedException{ operationStart(OperationType.EXTRACT_BIRTHMARKS); ExtractionResultSet er = extract(target, null, context); operationDone(OperationType.EXTRACT_BIRTHMARKS); return er; } - public ExtractionResultSet extract(String[] targetX, String[] targetY, BirthmarkContext context) throws BirthmarkExtractionFailedException{ - operationStart(OperationType.EXTRACT_BIRTHMARKS); - ExtractionResultSet er = ExtractionResultSetFactory.getInstance().createResultSet(context); - - try{ - switch(context.getComparisonMethod()){ - case ROUND_ROBIN_SAME_PAIR: - case ROUND_ROBIN_WITHOUT_SAME_PAIR: - er.setTableType(false); - String[] targetXY = mergeTarget(targetX, targetY); - extractImpl2(targetXY, er, ExtractionTarget.TARGET_XY); - break; - case GUESSED_PAIR: - case SPECIFIED_PAIR: - case ROUND_ROBIN_XY: - default: - if(targetX == null || targetY == null){ - throw new BirthmarkExtractionFailedException("targetX or targetY is null"); - } - er.setTableType(true); - extractImpl2(targetX, er, ExtractionTarget.TARGET_X); - extractImpl2(targetY, er, ExtractionTarget.TARGET_Y); - break; - } - return er; - } catch(IOException e){ - throw new BirthmarkExtractionFailedException(e); - } finally{ - operationDone(OperationType.EXTRACT_BIRTHMARKS); - } - } - - public ExtractionResultSet extract2(String[] targetX, String[] targetY, BirthmarkContext context) throws BirthmarkExtractionFailedException{ + public synchronized ExtractionResultSet extract(String[] targetX, String[] targetY, BirthmarkContext context) throws BirthmarkExtractionFailedException{ operationStart(OperationType.EXTRACT_BIRTHMARKS); ExtractionResultSet er = ExtractionResultSetFactory.getInstance().createResultSet(context); @@ -230,8 +232,7 @@ public class BirthmarkEngine{ case ROUND_ROBIN_WITHOUT_SAME_PAIR: er.setTableType(false); String[] targetXY = mergeTarget(targetX, targetY); - BirthmarkSet[] s = extractImpl(targetXY, context); - er.setBirthmarkSets(ExtractionTarget.TARGET_XY, s); + extractImpl(targetXY, er, ExtractionTarget.TARGET_XY); break; case GUESSED_PAIR: case SPECIFIED_PAIR: @@ -241,11 +242,8 @@ public class BirthmarkEngine{ throw new BirthmarkExtractionFailedException("targetX or targetY is null"); } er.setTableType(true); - BirthmarkSet[] extractResultX = extractImpl(targetX, context); - BirthmarkSet[] extractResultY = extractImpl(targetY, context); - - er.setBirthmarkSets(ExtractionTarget.TARGET_X, extractResultX); - er.setBirthmarkSets(ExtractionTarget.TARGET_Y, extractResultY); + extractImpl(targetX, er, ExtractionTarget.TARGET_X); + extractImpl(targetY, er, ExtractionTarget.TARGET_Y); break; } return er; @@ -272,88 +270,25 @@ public class BirthmarkEngine{ } } - private BirthmarkSet[] extractImpl2(String[] target, ExtractionResultSet er, ExtractionTarget et) throws BirthmarkExtractionFailedException, IOException{ + private BirthmarkSet[] extractImpl(String[] target, ExtractionResultSet er, ExtractionTarget et) throws BirthmarkExtractionFailedException, IOException{ ClassFileArchive[] archives = createArchives(target, environment); BirthmarkContext context = er.getContext(); ExtractionUnit unit = context.getExtractionUnit(); BirthmarkSet[] extractResult = null; if(unit == ExtractionUnit.CLASS){ - extractFromClass2(archives, er, et); + extractFromClass(archives, er, et); } else if(unit == ExtractionUnit.PACKAGE){ - extractFromPackage2(archives, er, et); + extractFromPackage(archives, er, et); } else if(unit == ExtractionUnit.ARCHIVE){ - extractFromProduct2(archives, er, et); + extractFromProduct(archives, er, et); } return extractResult; } - private BirthmarkSet[] extractImpl(String[] target, BirthmarkContext context) throws BirthmarkExtractionFailedException, IOException{ - ClassFileArchive[] archives = createArchives(target, environment); - BirthmarkExtractor[] extractors = createExtractors(context.getExtractionTypes(), environment); - ExtractionUnit unit = context.getExtractionUnit(); - - BirthmarkSet[] extractResult = null; - if(unit == ExtractionUnit.CLASS){ - extractResult = extractFromClass(archives, extractors, environment); - } - else if(unit == ExtractionUnit.PACKAGE){ - extractResult = extractFromPackage(archives, extractors, environment); - } - else if(unit == ExtractionUnit.ARCHIVE){ - extractResult = extractFromProduct(archives, extractors, environment); - } - - return extractResult; - } - - private BirthmarkExtractor[] createExtractors(String[] birthmarkTypes, BirthmarkEnvironment environment){ - List list = new ArrayList(); - for(String type: birthmarkTypes){ - BirthmarkExtractor extractor = createExtractor(type, environment); - list.add(extractor); - } - return list.toArray(new BirthmarkExtractor[list.size()]); - } - - @SuppressWarnings("unchecked") - private BirthmarkExtractor createExtractor(String birthmarkType, BirthmarkEnvironment environment){ - BirthmarkSpi spi = environment.getService(birthmarkType); - BirthmarkExtractor extractor = null; - if(spi != null){ - extractor = spi.getExtractor(); - try{ - if(extractor != null){ - Map props = BeanUtils.describe(extractor); - props.remove("class"); - props.remove("provider"); - for(Object keyObject: props.keySet()){ - String key = "extractor." + spi.getType() + "." + String.valueOf(keyObject); - if(environment.getProperty(key) != null){ - BeanUtils.setProperty( - extractor, (String)keyObject, environment.getProperty(key) - ); - } - } - } - } catch(InvocationTargetException e){ - throw new InternalError(e.getMessage()); - } catch(NoSuchMethodException e){ - throw new InternalError(e.getMessage()); - } catch(IllegalAccessException e){ - throw new InternalError(e.getMessage()); - } - } - if(extractor == null){ - warnings.addMessage(new ExtractorNotFoundException("extractor not found"), birthmarkType); - } - - return extractor; - } - private byte[] inputStreamToByteArray(InputStream in) throws IOException{ ByteArrayOutputStream bout = new ByteArrayOutputStream(); int read; @@ -367,7 +302,7 @@ public class BirthmarkEngine{ return data; } - private void extractFromPackage2(ClassFileArchive[] archives, ExtractionResultSet er, ExtractionTarget et) throws IOException, BirthmarkExtractionFailedException{ + private void extractFromPackage(ClassFileArchive[] archives, ExtractionResultSet er, ExtractionTarget et) throws IOException, BirthmarkExtractionFailedException{ Map map = new HashMap(); BirthmarkContext context = er.getContext(); @@ -408,40 +343,6 @@ public class BirthmarkEngine{ } } - private BirthmarkSet[] extractFromPackage(ClassFileArchive[] archives, BirthmarkExtractor[] extractors, BirthmarkEnvironment context) throws IOException, BirthmarkExtractionFailedException{ - Map list = new HashMap(); - - for(ClassFileArchive archive: archives){ - for(ClassFileEntry entry: archive){ - try{ - String name = entry.getClassName(); - String packageName = parsePackageName(name); - BirthmarkSet bs = list.get(packageName); - if(bs == null){ - bs = new BirthmarkSet(packageName, archive.getLocation()); - list.put(packageName, bs); - } - - byte[] data = inputStreamToByteArray(entry.getLocation().openStream()); - for(BirthmarkExtractor extractor: extractors){ - if(extractor.isAcceptable(ExtractionUnit.PACKAGE)){ - Birthmark b = bs.getBirthmark(extractor.getProvider().getType()); - if(b == null){ - b = extractor.createBirthmark(); - bs.addBirthmark(b); - } - extractor.extract(b, new ByteArrayInputStream(data), context); - } - } - } catch(IOException e){ - warnings.addMessage(e, archive.getName()); - } - } - } - - return list.values().toArray(new BirthmarkSet[list.size()]); - } - private String parsePackageName(String name){ String n = name.replace('/', '.'); int index = n.lastIndexOf('.'); @@ -452,7 +353,7 @@ public class BirthmarkEngine{ return n; } - private void extractFromClass2(ClassFileArchive[] archives, ExtractionResultSet er, + private void extractFromClass(ClassFileArchive[] archives, ExtractionResultSet er, ExtractionTarget et) throws IOException, BirthmarkExtractionFailedException{ BirthmarkContext context = er.getContext(); @@ -481,31 +382,7 @@ public class BirthmarkEngine{ } } - private BirthmarkSet[] extractFromClass(ClassFileArchive[] archives, - BirthmarkExtractor[] extractors, BirthmarkEnvironment context) throws IOException, BirthmarkExtractionFailedException{ - List list = new ArrayList(); - - for(ClassFileArchive archive: archives){ - for(ClassFileEntry entry: archive){ - try{ - BirthmarkSet birthmarkset = new BirthmarkSet(entry.getClassName(), entry.getLocation()); - list.add(birthmarkset); - byte[] data = inputStreamToByteArray(entry.getLocation().openStream()); - for(BirthmarkExtractor extractor: extractors){ - if(extractor.isAcceptable(ExtractionUnit.CLASS)){ - Birthmark b = extractor.extract(new ByteArrayInputStream(data), context); - birthmarkset.addBirthmark(b); - } - } - } catch(IOException e){ - warnings.addMessage(e, entry.getClassName()); - } - } - } - return list.toArray(new BirthmarkSet[list.size()]); - } - - private void extractFromProduct2(ClassFileArchive[] archives, ExtractionResultSet er, ExtractionTarget et) throws IOException, BirthmarkExtractionFailedException{ + private void extractFromProduct(ClassFileArchive[] archives, ExtractionResultSet er, ExtractionTarget et) throws IOException, BirthmarkExtractionFailedException{ BirthmarkContext context = er.getContext(); for(ClassFileArchive archive: archives){ @@ -539,41 +416,6 @@ public class BirthmarkEngine{ } } - private BirthmarkSet[] extractFromProduct(ClassFileArchive[] archives, BirthmarkExtractor[] extractors, BirthmarkEnvironment context) throws IOException, BirthmarkExtractionFailedException{ - List list = new ArrayList(); - - for(ClassFileArchive archive: archives){ - BirthmarkSet birthmarkset = new BirthmarkSet(archive.getName(), archive.getLocation()); - list.add(birthmarkset); - - for(ClassFileEntry entry: archive){ - try{ - byte[] data = inputStreamToByteArray(entry.getLocation().openStream()); - for(BirthmarkExtractor extractor: extractors){ - if(extractor.isAcceptable(ExtractionUnit.ARCHIVE)){ - Birthmark b = birthmarkset.getBirthmark(extractor.getProvider().getType()); - if(b == null){ - b = extractor.createBirthmark(); - birthmarkset.addBirthmark(b); - } - extractor.extract(b, new ByteArrayInputStream(data), context); - } - } - } catch(IOException e){ - warnings.addMessage(e, entry.getClassName()); - } - } - } - for(Iterator i = list.iterator(); i.hasNext(); ){ - BirthmarkSet set = i.next(); - if(set.getBirthmarksCount() == 0){ - i.remove(); - } - } - - return list.toArray(new BirthmarkSet[list.size()]); - } - private ClassFileArchive[] createArchives(String[] files, BirthmarkEnvironment environment) throws IOException, MalformedURLException{ ClasspathContext bytecode = environment.getClasspathContext(); List archives = new ArrayList(); diff --git a/src/main/java/jp/naist/se/stigmata/ComparisonResultSet.java b/src/main/java/jp/naist/se/stigmata/ComparisonResultSet.java index 426d87d..1f824b0 100644 --- a/src/main/java/jp/naist/se/stigmata/ComparisonResultSet.java +++ b/src/main/java/jp/naist/se/stigmata/ComparisonResultSet.java @@ -48,5 +48,8 @@ public interface ComparisonResultSet extends Iterable{ */ public BirthmarkSet[] getPairSources(); + /** + * returns an array of comparison sources. + */ public Iterator pairSources(); } diff --git a/src/main/java/jp/naist/se/stigmata/ExtractionResultSet.java b/src/main/java/jp/naist/se/stigmata/ExtractionResultSet.java index 5cb5838..e2d86fd 100755 --- a/src/main/java/jp/naist/se/stigmata/ExtractionResultSet.java +++ b/src/main/java/jp/naist/se/stigmata/ExtractionResultSet.java @@ -7,56 +7,123 @@ package jp.naist.se.stigmata; import java.util.Iterator; /** + * This interface represents a set of extracted birthmarks. * * @author Haruaki Tamada * @version $Revision$ $Date$ */ public interface ExtractionResultSet extends Iterable{ + /** + * returns an environment of extraction result. + */ public BirthmarkEnvironment getEnvironment(); + /** + * returns a context of extraction result. + */ public BirthmarkContext getContext(); /** - * + * sets extraction */ public boolean isTableType(); /** - * + * sets table type comparison flag. + * @see #isTableType() */ public void setTableType(boolean flag); + /** + * returns types of extracted birthmarks. + */ public String[] getBirthmarkTypes(); + /** + * returns the number of target birthmark-set size (# of classes, packages, or jar files). + */ public int getBirthmarkSetSize(); + /** + * returns an iterator for all of birthmark-sets. + */ public Iterator iterator(); + /** + * returns a birthmark-set of given index. + */ public BirthmarkSet getBirthmarkSet(int index); + /** + * returns a birthmark-set of given name. + */ public BirthmarkSet getBirthmarkSet(String name); + /** + * returns an array for all of birthmark-sets. + */ public BirthmarkSet[] getBirthmarkSets(); - public int getBirthmarkSetSize(ExtractionTarget target); - + /** + * removes given birthmark-set from this object. + */ public void removeBirthmarkSet(BirthmarkSet bs); + /** + * removes all of birthmark-set this object has. + */ public void removeAllBirthmarkSets(); + /** + * returns the number of birthmark-set to specified extraction target (TARGET_X, TARGET_Y, TARGET_XY, or TARGET_BOTH). + */ + public int getBirthmarkSetSize(ExtractionTarget target); + + /** + * returns an iterator of birthmark-sets from specified extraction target. + */ public Iterator birthmarkSets(ExtractionTarget target); + /** + * + */ public BirthmarkSet getBirthmarkSet(ExtractionTarget target, int index); + /** + * + */ public BirthmarkSet getBirthmarkSet(ExtractionTarget target, String setname); + /** + * + */ public BirthmarkSet[] getBirthmarkSets(ExtractionTarget target); + /** + * adds birthmark-set to extraction target. + * This method must be called when building birthmark-set is completely finished. + * All of birthmark must be added to birthmark-set. + * Because, if the concrete class of this interface stores given birthmark-set to database, + * birthmarks is parsed and store into target database in this method. + * @throws IllegalArgumentsException target is ExtractionTarget.TARGET_BOTH + */ public void addBirthmarkSet(ExtractionTarget target, BirthmarkSet set); + /** + * remove all of birthmark-set this object has, then, adds each birthmark-sets to this object. + * @see #removeAllBirthmarkSets(ExtractionTarget) + * @see #addBirthmarkSet(ExtractionTarget, BirthmarkSet) + * @throws IllegalArgumentsException target is ExtractionTarget.TARGET_BOTH + */ public void setBirthmarkSets(ExtractionTarget target, BirthmarkSet[] sets); + /** + * remove specified birthmark-set from specified extraction target. + */ public void removeBirthmarkSet(ExtractionTarget target, BirthmarkSet set); + /** + * remove all birthmark-sets from specified extraction target. + */ public void removeAllBirthmarkSets(ExtractionTarget target); } diff --git a/src/main/java/jp/naist/se/stigmata/ExtractionTarget.java b/src/main/java/jp/naist/se/stigmata/ExtractionTarget.java index f16d17d..510f251 100755 --- a/src/main/java/jp/naist/se/stigmata/ExtractionTarget.java +++ b/src/main/java/jp/naist/se/stigmata/ExtractionTarget.java @@ -5,6 +5,7 @@ package jp.naist.se.stigmata; */ /** + * This enum represents the mean of extracted birthmarks. * * @author Haruaki Tamada * @version $Revision$ $Date$ diff --git a/src/main/java/jp/naist/se/stigmata/ExtractionUnit.java b/src/main/java/jp/naist/se/stigmata/ExtractionUnit.java index 66772e7..94ad087 100755 --- a/src/main/java/jp/naist/se/stigmata/ExtractionUnit.java +++ b/src/main/java/jp/naist/se/stigmata/ExtractionUnit.java @@ -5,13 +5,19 @@ package jp.naist.se.stigmata; */ /** - * This enum type represents birthmark extraction unit. The name of - * {@link jp.naist.se.stigmata.BirthmarkSet BirthmarkSet} is class - * name, method name, package name, or product name. + * This enum type represents birthmark extraction unit. + * + * The name of + * {@link jp.naist.se.stigmata.BirthmarkSet BirthmarkSet} will be + * class name, method name, package name, or product name. * * @author Haruaki TAMADA * @version $Revision$ $Date$ */ public enum ExtractionUnit{ - CLASS, PACKAGE, ARCHIVE, + CLASS, + PACKAGE, + ARCHIVE, + @Deprecated + METHOD, } diff --git a/src/main/java/jp/naist/se/stigmata/Stigmata.java b/src/main/java/jp/naist/se/stigmata/Stigmata.java index 824ee3b..6028a1b 100755 --- a/src/main/java/jp/naist/se/stigmata/Stigmata.java +++ b/src/main/java/jp/naist/se/stigmata/Stigmata.java @@ -25,14 +25,23 @@ import jp.naist.se.stigmata.utils.ConfigFileImporter; * @version $Revision$ $Date$ */ public class Stigmata{ + /** + * instance. singleton pattern. + */ private static Stigmata stigmata; private BirthmarkEnvironment defaultEnvironment; private List listeners = new ArrayList(); + /** + * private constructor. + */ private Stigmata(){ configuration(); } + /** + * gets only instance of this class. + */ public static synchronized Stigmata getInstance(){ if(stigmata == null){ stigmata = new Stigmata(); @@ -40,21 +49,35 @@ public class Stigmata{ return stigmata; } + /** + * creates a new birthmark context. + */ public BirthmarkContext createContext(){ return new BirthmarkContext(createEnvironment()); } + /** + * creates a new birthmark environment. + */ public BirthmarkEnvironment createEnvironment(){ return new BirthmarkEnvironment(defaultEnvironment); } + /** + * creates a new birthmark engine. + */ public BirthmarkEngine createEngine(){ - BirthmarkEngine engine = new BirthmarkEngine(createEnvironment()); - return engine; + return createEngine(createEnvironment()); } + /** + * creates a new birthmark engine with given environment. + */ public BirthmarkEngine createEngine(BirthmarkEnvironment environment){ BirthmarkEngine engine = new BirthmarkEngine(environment); + for(BirthmarkEngineListener listener: listeners){ + engine.addBirthmarkEngineListener(listener); + } return engine; } diff --git a/src/main/java/jp/naist/se/stigmata/birthmarks/kgram/KGram.java b/src/main/java/jp/naist/se/stigmata/birthmarks/kgram/KGram.java index 90a2634..1974c39 100644 --- a/src/main/java/jp/naist/se/stigmata/birthmarks/kgram/KGram.java +++ b/src/main/java/jp/naist/se/stigmata/birthmarks/kgram/KGram.java @@ -5,60 +5,106 @@ package jp.naist.se.stigmata.birthmarks.kgram; */ import java.io.Serializable; -import java.util.ArrayList; +import java.lang.reflect.Array; +import java.util.Arrays; import java.util.LinkedHashSet; -import java.util.List; import java.util.Set; /** + * This class represents k-gram of the some sequence. * * @author Haruaki TAMADA * @version $Revision$ $Date$ */ public class KGram implements Serializable{ private static final long serialVersionUID = 273465874532523L; - private List list = new ArrayList(); + // private List list = new ArrayList(); private int maxLength = 4; + private T[] values; + /** + * constructor. + * @param kvalue the number of elements of this object. + */ public KGram(int kvalue){ setKValue(kvalue); } + /** + * sets k-value. + * @param kvalue the number of elements of this object. + */ public void setKValue(int kvalue){ this.maxLength = kvalue; } + /** + * returns k-value which is the number of elements. + * @return the number of elements. + */ public int getKValue(){ return maxLength; } + /** + * returns string representation of this object. + */ public String toString(){ StringBuffer buffer = new StringBuffer("{ "); for(int i = 0; i < maxLength; i++){ if(i != 0) buffer.append(", "); - buffer.append(list.get(i)); + buffer.append(get(i)); } buffer.append(" }"); return new String(buffer); } + /** + * sets a element to given index. + * @param index element index. + * @param value element value. + */ + @SuppressWarnings("unchecked") public void set(int index, T value){ if(index < 0 || index >= maxLength){ - throw new ArrayIndexOutOfBoundsException("expected 0-" + maxLength + ": " + index); + throw new ArrayIndexOutOfBoundsException("expected 0-" + (maxLength - 1) + ": " + index); } - if(list.size() == index){ - list.add(value); + if(value == null){ + throw new NullPointerException("null value"); } - else{ - list.set(index, value); + if(values == null){ + values = (T[])Array.newInstance(value.getClass(), getKValue()); } + values[index] = value; } + /** + * returns an object of given index. + */ public T get(int index){ + T returnValue = null; if(index < 0 || index >= maxLength){ - throw new ArrayIndexOutOfBoundsException("expected 0-" + maxLength + ": " + index); + throw new ArrayIndexOutOfBoundsException("expected 0-" + (maxLength - 1) + ": " + index); + } + if(values != null){ + returnValue = values[index]; + } + + return returnValue; + } + + /** + * returns an array of elements this object has. + * @return + */ + @SuppressWarnings("unchecked") + public T[] toArray(){ + if(values == null){ + throw new IllegalStateException("this object has no elements."); } - return list.get(index); + T[] newarray = (T[])Array.newInstance(values[0].getClass(), getKValue()); + System.arraycopy(values, 0, newarray, 0, getKValue()); + return newarray; } @SuppressWarnings("unchecked") @@ -77,7 +123,7 @@ public class KGram implements Serializable{ } public int hashCode(){ - return list.hashCode(); + return Arrays.hashCode(values); } @SuppressWarnings("unchecked") diff --git a/src/main/java/jp/naist/se/stigmata/ui/swing/StigmataFrame.java b/src/main/java/jp/naist/se/stigmata/ui/swing/StigmataFrame.java index 3c16cf2..69f164f 100644 --- a/src/main/java/jp/naist/se/stigmata/ui/swing/StigmataFrame.java +++ b/src/main/java/jp/naist/se/stigmata/ui/swing/StigmataFrame.java @@ -177,7 +177,7 @@ public class StigmataFrame extends JFrame{ public void compareRoundRobin(String[] targetX, String[] targetY, BirthmarkContext context){ try{ - BirthmarkEngine engine = new BirthmarkEngine(context.getEnvironment()); + BirthmarkEngine engine = getStigmata().createEngine(context.getEnvironment()); ExtractionResultSet ers = engine.extract(targetX, targetY, context); RoundRobinComparisonResultPane compare = new RoundRobinComparisonResultPane(this, ers); @@ -200,7 +200,7 @@ public class StigmataFrame extends JFrame{ public void compareRoundRobinFilter(String[] targetX, String[] targetY, BirthmarkContext context){ try{ - BirthmarkEngine engine = new BirthmarkEngine(context.getEnvironment()); + BirthmarkEngine engine = getStigmata().createEngine(context.getEnvironment()); ExtractionResultSet ers = engine.extract(targetX, targetY, context); ComparisonResultSet resultset = engine.compare(ers); @@ -225,7 +225,7 @@ public class StigmataFrame extends JFrame{ public void compareGuessedPair(String[] targetX, String[] targetY, BirthmarkContext context){ try{ - BirthmarkEngine engine = new BirthmarkEngine(context.getEnvironment()); + BirthmarkEngine engine = getStigmata().createEngine(context.getEnvironment()); ExtractionResultSet extraction = engine.extract(targetX, targetY, context); int comparePair = getNextCount("compare_pair"); @@ -253,7 +253,7 @@ public class StigmataFrame extends JFrame{ Map mapping = constructMapping(file); try{ - BirthmarkEngine engine = new BirthmarkEngine(context.getEnvironment()); + BirthmarkEngine engine = getStigmata().createEngine(context.getEnvironment()); context.setNameMappings(mapping); ComparisonResultSet crs = engine.compare(targetX, targetY, context); int comparePair = getNextCount("compare_pair"); @@ -318,7 +318,7 @@ public class StigmataFrame extends JFrame{ public void extract(String[] targets, BirthmarkContext context){ try{ - BirthmarkEngine engine = new BirthmarkEngine(context.getEnvironment()); + BirthmarkEngine engine = getStigmata().createEngine(context.getEnvironment()); ExtractionResultSet ers = engine.extract(targets, context); showExtractionResult(ers); }catch(Throwable e){ diff --git a/src/main/java/jp/naist/se/stigmata/utils/WarClassLoader.java b/src/main/java/jp/naist/se/stigmata/utils/WarClassLoader.java index ca69e94..fd48031 100755 --- a/src/main/java/jp/naist/se/stigmata/utils/WarClassLoader.java +++ b/src/main/java/jp/naist/se/stigmata/utils/WarClassLoader.java @@ -56,11 +56,14 @@ public class WarClassLoader extends URLClassLoader{ in.close(); out.close(); - return defineClass(name, classdata, 0, classdata.length); + clazz = defineClass(name, classdata, 0, classdata.length); + break; } catch(IOException exp){ } } } + } + if(clazz == null){ throw new ClassNotFoundException(name); } return clazz;