1 package jp.sourceforge.stigmata.result;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.Iterator;
9 import jp.sourceforge.stigmata.BirthmarkContext;
10 import jp.sourceforge.stigmata.BirthmarkSet;
11 import jp.sourceforge.stigmata.BirthmarkStoreException;
12 import jp.sourceforge.stigmata.ComparisonPair;
13 import jp.sourceforge.stigmata.ExtractionResultSet;
14 import jp.sourceforge.stigmata.ExtractionTarget;
15 import jp.sourceforge.stigmata.result.history.ExtractedBirthmarkServiceManager;
18 * Concrete class for ComparisonResultSet. This instance compare class files by
19 * certain pair. The pair is guessed by system with class name, or specified by
22 * @author Haruaki TAMADA
24 public class CertainPairComparisonResultSet extends AbstractComparisonResultSet{
25 private int compareCount = -1;
26 private Collection<BirthmarkSet> sources = null;
28 public CertainPairComparisonResultSet(ExtractionResultSet extraction){
32 public CertainPairComparisonResultSet(ExtractionResultSet extraction, Map<String, String> nameMap){
34 BirthmarkContext context = extraction.getContext();
35 for(Map.Entry<String, String> entry: nameMap.entrySet()){
36 context.addNameMapping(entry.getKey(), entry.getValue());
41 * This constructor is the comparison pair list is specified.
43 public CertainPairComparisonResultSet(ComparisonPair[] pairs, BirthmarkContext context){
44 super(createExtractionResultSet(pairs, context));
48 * This constructor is the comparison pair was guessed with class name.
50 public CertainPairComparisonResultSet(BirthmarkSet[] targetX,
51 BirthmarkSet[] targetY, BirthmarkContext context){
52 super(createExtractionResultSet(targetX, targetY, context));
56 * This constructor is the comparison pair was specified as mapping.
58 public CertainPairComparisonResultSet(BirthmarkSet[] targetX, BirthmarkSet[] targetY,
59 Map<String, String> mapping, BirthmarkContext context){
60 super(createExtractionResultSet(targetX, targetY, context));
62 for(Map.Entry<String, String> entry : mapping.entrySet()){
63 context.addNameMapping(entry.getKey(), entry.getValue());
68 * return comparison count.
71 public int getPairCount(){
72 BirthmarkContext context = getContext();
75 if(context.hasNameMapping()){
76 count = context.getNameMappingCount();
79 count = super.getPairCount();
87 * return the iterator of each pair.
90 public Iterator<ComparisonPair> iterator(){
91 Iterator<ComparisonPair> iterator = null;
92 final BirthmarkContext context = getContext();
93 if(context.hasNameMapping()){
94 iterator = new NameMappingIterator(extraction);
97 iterator = new NameFindIterator(extraction);
103 public Iterator<BirthmarkSet> pairSources(){
105 sources = createSources();
107 return sources.iterator();
111 public BirthmarkSet[] getPairSources(){
113 sources = createSources();
115 return sources.toArray(new BirthmarkSet[sources.size()]);
118 private Collection<BirthmarkSet> createSources(){
119 Map<URL, BirthmarkSet> map = new HashMap<URL, BirthmarkSet>();
120 for(Iterator<ComparisonPair> i = iterator(); i.hasNext(); ){
121 ComparisonPair pair = i.next();
122 addToMap(map, pair.getTarget1());
123 addToMap(map, pair.getTarget2());
128 private void addToMap(Map<URL, BirthmarkSet> map, BirthmarkSet set){
129 map.put(set.getLocation(), set);
132 private static ExtractionResultSet createExtractionResultSet(ComparisonPair[] pairs, BirthmarkContext context){
133 ExtractedBirthmarkServiceManager historyManager = context.getEnvironment().getHistoryManager();
134 ExtractionResultSet ers = historyManager.createDefaultResultSet(context);
135 ers.setTableType(false);
137 for(int i = 0; i < pairs.length; i++){
138 ers.addBirthmarkSet(ExtractionTarget.TARGET_X, pairs[i].getTarget1());
139 ers.addBirthmarkSet(ExtractionTarget.TARGET_Y, pairs[i].getTarget2());
141 }catch(BirthmarkStoreException e){
142 throw new InternalError("never thrown BirthmarkStoreException is thrown");
147 private static ExtractionResultSet createExtractionResultSet(BirthmarkSet[] targetX, BirthmarkSet[] targetY, BirthmarkContext context){
148 ExtractionResultSet ers = context.getEnvironment().getHistoryManager().createDefaultResultSet(context);
149 ers.setTableType(true);
151 ers.setBirthmarkSets(ExtractionTarget.TARGET_X, targetX);
152 ers.setBirthmarkSets(ExtractionTarget.TARGET_Y, targetY);
153 }catch(BirthmarkStoreException e){
154 throw new InternalError("never thrown BirthmarkStoreException is thrown");
159 private static class NameFindIterator implements Iterator<ComparisonPair>{
160 private ComparisonPair next;
161 private BirthmarkSet setX = null;
162 private Iterator<BirthmarkSet> iteratorX;
163 private Iterator<BirthmarkSet> iteratorY;
164 private ExtractionResultSet extraction;
166 public NameFindIterator(ExtractionResultSet extraction){
167 this.extraction = extraction;
168 iteratorX = extraction.birthmarkSets(ExtractionTarget.TARGET_X);
169 setX = iteratorX.next();
174 public boolean hasNext(){
179 public ComparisonPair next(){
180 ComparisonPair returnValue = next;
186 public void remove(){
189 private ComparisonPair findNext(){
190 ComparisonPair next = null;
191 if(iteratorY == null || !iteratorY.hasNext()){
192 iteratorY = extraction.birthmarkSets(ExtractionTarget.TARGET_Y);
196 for(; iteratorY.hasNext(); ){
197 BirthmarkSet setY = iteratorY.next();
198 if(setX.getName().equals(setY.getName())){
199 next = new ComparisonPair(setX, setY, extraction.getContext());
204 if(iteratorX.hasNext()){
205 setX = iteratorX.next();
218 private static class NameMappingIterator implements Iterator<ComparisonPair>{
219 private Iterator<Map.Entry<String, String>> names;
220 private ComparisonPair nextPair;
221 private ExtractionResultSet ers;
223 public NameMappingIterator(ExtractionResultSet ers){
225 names = ers.getContext().nameMappingEntries();
226 nextPair = findNextPair();
230 public ComparisonPair next(){
231 ComparisonPair cp = nextPair;
232 nextPair = findNextPair();
237 public boolean hasNext(){
238 return nextPair != null;
242 public void remove(){
245 private ComparisonPair findNextPair(){
246 ComparisonPair pair = null;
248 Map.Entry<String, String> entry = names.next();
249 String n1 = entry.getKey();
250 String n2 = entry.getValue();
252 BirthmarkSet bs1 = ers.getBirthmarkSet(ExtractionTarget.TARGET_X, n1);
253 BirthmarkSet bs2 = ers.getBirthmarkSet(ExtractionTarget.TARGET_Y, n2);
255 if(bs1 == null || bs2 == null){
256 pair = findNextPair();
259 pair = new ComparisonPair(bs1, bs2, ers.getContext());