1 package jp.sourceforge.stigmata.result;
8 import java.util.Collection;
9 import java.util.HashMap;
10 import java.util.Iterator;
13 import jp.sourceforge.stigmata.BirthmarkContext;
14 import jp.sourceforge.stigmata.BirthmarkSet;
15 import jp.sourceforge.stigmata.BirthmarkStoreException;
16 import jp.sourceforge.stigmata.ComparisonPair;
17 import jp.sourceforge.stigmata.ExtractionResultSet;
18 import jp.sourceforge.stigmata.ExtractionTarget;
19 import jp.sourceforge.stigmata.result.history.ExtractedBirthmarkServiceManager;
22 * Concrete class for ComparisonResultSet. This instance compare class files by
23 * certain pair. The pair is guessed by system with class name, or specified by
26 * @author Haruaki TAMADA
27 * @version $Revision$ $Date$
29 public class CertainPairComparisonResultSet extends AbstractComparisonResultSet{
30 private int compareCount = -1;
31 private Collection<BirthmarkSet> sources = null;
33 public CertainPairComparisonResultSet(ExtractionResultSet extraction){
37 public CertainPairComparisonResultSet(ExtractionResultSet extraction, Map<String, String> nameMap){
39 BirthmarkContext context = extraction.getContext();
40 for(Map.Entry<String, String> entry: nameMap.entrySet()){
41 context.addNameMapping(entry.getKey(), entry.getValue());
46 * This constructor is the comparison pair list is specified.
48 public CertainPairComparisonResultSet(ComparisonPair[] pairs, BirthmarkContext context){
49 super(createExtractionResultSet(pairs, context));
53 * This constructor is the comparison pair was guessed with class name.
55 public CertainPairComparisonResultSet(BirthmarkSet[] targetX,
56 BirthmarkSet[] targetY, BirthmarkContext context){
57 super(createExtractionResultSet(targetX, targetY, context));
61 * This constructor is the comparison pair was specified as mapping.
63 public CertainPairComparisonResultSet(BirthmarkSet[] targetX, BirthmarkSet[] targetY,
64 Map<String, String> mapping, BirthmarkContext context){
65 super(createExtractionResultSet(targetX, targetY, context));
67 for(Map.Entry<String, String> entry : mapping.entrySet()){
68 context.addNameMapping(entry.getKey(), entry.getValue());
73 * return comparison count.
76 public int getPairCount(){
77 BirthmarkContext context = getContext();
80 if(context.hasNameMapping()){
81 count = context.getNameMappingCount();
84 count = super.getPairCount();
92 * return the iterator of each pair.
95 public Iterator<ComparisonPair> iterator(){
96 Iterator<ComparisonPair> iterator = null;
97 final BirthmarkContext context = getContext();
98 if(context.hasNameMapping()){
99 iterator = new NameMappingIterator(extraction);
102 iterator = new NameFindIterator(extraction);
108 public Iterator<BirthmarkSet> pairSources(){
110 sources = createSources();
112 return sources.iterator();
115 public BirthmarkSet[] getPairSources(){
117 sources = createSources();
119 return sources.toArray(new BirthmarkSet[sources.size()]);
122 private Collection<BirthmarkSet> createSources(){
123 Map<URL, BirthmarkSet> map = new HashMap<URL, BirthmarkSet>();
124 for(Iterator<ComparisonPair> i = iterator(); i.hasNext(); ){
125 ComparisonPair pair = i.next();
126 addToMap(map, pair.getTarget1());
127 addToMap(map, pair.getTarget2());
132 private void addToMap(Map<URL, BirthmarkSet> map, BirthmarkSet set){
133 map.put(set.getLocation(), set);
136 private static ExtractionResultSet createExtractionResultSet(ComparisonPair[] pairs, BirthmarkContext context){
137 ExtractedBirthmarkServiceManager historyManager = context.getEnvironment().getHistoryManager();
138 ExtractionResultSet ers = historyManager.createDefaultResultSet(context);
139 ers.setTableType(false);
141 for(int i = 0; i < pairs.length; i++){
142 ers.addBirthmarkSet(ExtractionTarget.TARGET_X, pairs[i].getTarget1());
143 ers.addBirthmarkSet(ExtractionTarget.TARGET_Y, pairs[i].getTarget2());
145 }catch(BirthmarkStoreException e){
146 throw new InternalError("never thrown BirthmarkStoreException is thrown");
151 private static ExtractionResultSet createExtractionResultSet(BirthmarkSet[] targetX, BirthmarkSet[] targetY, BirthmarkContext context){
152 ExtractionResultSet ers = context.getEnvironment().getHistoryManager().createDefaultResultSet(context);
153 ers.setTableType(true);
155 ers.setBirthmarkSets(ExtractionTarget.TARGET_X, targetX);
156 ers.setBirthmarkSets(ExtractionTarget.TARGET_Y, targetY);
157 }catch(BirthmarkStoreException e){
158 throw new InternalError("never thrown BirthmarkStoreException is thrown");
163 private static class NameFindIterator implements Iterator<ComparisonPair>{
164 private ComparisonPair next;
165 private BirthmarkSet setX = null;
166 private Iterator<BirthmarkSet> iteratorX;
167 private Iterator<BirthmarkSet> iteratorY;
168 private ExtractionResultSet extraction;
170 public NameFindIterator(ExtractionResultSet extraction){
171 this.extraction = extraction;
172 iteratorX = extraction.birthmarkSets(ExtractionTarget.TARGET_X);
173 setX = iteratorX.next();
177 public boolean hasNext(){
181 public ComparisonPair next(){
182 ComparisonPair returnValue = next;
187 public void remove(){
190 private ComparisonPair findNext(){
191 ComparisonPair next = null;
192 if(iteratorY == null || !iteratorY.hasNext()){
193 iteratorY = extraction.birthmarkSets(ExtractionTarget.TARGET_Y);
197 for(; iteratorY.hasNext(); ){
198 BirthmarkSet setY = iteratorY.next();
199 if(setX.getName().equals(setY.getName())){
200 next = new ComparisonPair(setX, setY, extraction.getContext());
205 if(iteratorX.hasNext()){
206 setX = iteratorX.next();
219 private static class NameMappingIterator implements Iterator<ComparisonPair>{
220 private Iterator<Map.Entry<String, String>> names;
221 private ComparisonPair nextPair;
222 private ExtractionResultSet ers;
224 public NameMappingIterator(ExtractionResultSet ers){
226 names = ers.getContext().nameMappingEntries();
227 nextPair = findNextPair();
230 public ComparisonPair next(){
231 ComparisonPair cp = nextPair;
232 nextPair = findNextPair();
236 public boolean hasNext(){
237 return nextPair != null;
240 public void remove(){
243 private ComparisonPair findNextPair(){
244 ComparisonPair pair = null;
246 Map.Entry<String, String> entry = names.next();
247 String n1 = entry.getKey();
248 String n2 = entry.getValue();
250 BirthmarkSet bs1 = ers.getBirthmarkSet(ExtractionTarget.TARGET_X, n1);
251 BirthmarkSet bs2 = ers.getBirthmarkSet(ExtractionTarget.TARGET_Y, n2);
253 if(bs1 == null || bs2 == null){
254 pair = findNextPair();
257 pair = new ComparisonPair(bs1, bs2, ers.getContext());