OSDN Git Service

change package name. jp.naist.se.stigmata -> jp.sourceforge.stigmata
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / result / CertainPairComparisonResultSet.java
1 package jp.sourceforge.stigmata.result;
2
3 /*
4  * $Id$
5  */
6
7 import java.net.URL;
8 import java.util.Collection;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.Map;
12
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;
20
21 /**
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
24  * user.
25  * 
26  * @author Haruaki TAMADA
27  * @version $Revision$ $Date$
28  */
29 public class CertainPairComparisonResultSet extends AbstractComparisonResultSet{
30     private int compareCount = -1;
31     private Collection<BirthmarkSet> sources = null;
32
33     public CertainPairComparisonResultSet(ExtractionResultSet extraction){
34         super(extraction);
35     }
36
37     public CertainPairComparisonResultSet(ExtractionResultSet extraction, Map<String, String> nameMap){
38         super(extraction);
39         BirthmarkContext context = extraction.getContext();
40         for(Map.Entry<String, String> entry: nameMap.entrySet()){
41             context.addNameMapping(entry.getKey(), entry.getValue());
42         }
43     }
44
45     /**
46      * This constructor is the comparison pair list is specified.
47      */
48     public CertainPairComparisonResultSet(ComparisonPair[] pairs, BirthmarkContext context){
49         super(createExtractionResultSet(pairs, context));
50     }
51
52     /**
53      * This constructor is the comparison pair was guessed with class name.
54      */
55     public CertainPairComparisonResultSet(BirthmarkSet[] targetX,
56             BirthmarkSet[] targetY, BirthmarkContext context){
57         super(createExtractionResultSet(targetX, targetY, context));
58     }
59
60     /**
61      * This constructor is the comparison pair was specified as mapping.
62      */
63     public CertainPairComparisonResultSet(BirthmarkSet[] targetX, BirthmarkSet[] targetY,
64             Map<String, String> mapping, BirthmarkContext context){
65         super(createExtractionResultSet(targetX, targetY, context));
66
67         for(Map.Entry<String, String> entry : mapping.entrySet()){
68             context.addNameMapping(entry.getKey(), entry.getValue());
69         }
70     }
71
72     /**
73      * return comparison count.
74      */
75     @Override
76     public int getPairCount(){
77         BirthmarkContext context = getContext();
78         if(compareCount < 0){
79             int count = 0;
80             if(context.hasNameMapping()){
81                 count = context.getNameMappingCount();
82             }
83             else{
84                 count = super.getPairCount();
85             }
86             compareCount = count;
87         }
88         return compareCount;
89     }
90
91     /**
92      * return the iterator of each pair.
93      */
94     @Override
95     public Iterator<ComparisonPair> iterator(){
96         Iterator<ComparisonPair> iterator = null;
97         final BirthmarkContext context = getContext();
98         if(context.hasNameMapping()){
99             iterator = new NameMappingIterator(extraction);
100         }
101         else{
102             iterator = new NameFindIterator(extraction);
103         }
104         return iterator;
105     }
106
107     @Override
108     public Iterator<BirthmarkSet> pairSources(){
109         if(sources == null){
110             sources = createSources();
111         }
112         return sources.iterator();
113     }
114
115     public BirthmarkSet[] getPairSources(){
116         if(sources == null){
117             sources = createSources();
118         }
119         return sources.toArray(new BirthmarkSet[sources.size()]);
120     }
121
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());
128         }
129         return map.values();
130     }
131
132     private void addToMap(Map<URL, BirthmarkSet> map, BirthmarkSet set){
133         map.put(set.getLocation(), set);
134     }
135
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);
140         try{
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());
144             }
145         }catch(BirthmarkStoreException e){
146             throw new InternalError("never thrown BirthmarkStoreException is thrown");
147         }
148         return ers;
149     }
150
151     private static ExtractionResultSet createExtractionResultSet(BirthmarkSet[] targetX, BirthmarkSet[] targetY, BirthmarkContext context){
152         ExtractionResultSet ers = context.getEnvironment().getHistoryManager().createDefaultResultSet(context);
153         ers.setTableType(true);
154         try{
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");
159         }
160         return ers;
161     }
162
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;
169
170         public NameFindIterator(ExtractionResultSet extraction){
171             this.extraction = extraction;
172             iteratorX = extraction.birthmarkSets(ExtractionTarget.TARGET_X);
173             setX = iteratorX.next();
174             next = findNext();
175         }
176
177         public boolean hasNext(){
178             return next != null;
179         }
180
181         public ComparisonPair next(){
182             ComparisonPair returnValue = next;
183             next = findNext();
184             return returnValue;
185         }
186
187         public void remove(){
188         }
189
190         private ComparisonPair findNext(){
191             ComparisonPair next = null;
192             if(iteratorY == null || !iteratorY.hasNext()){
193                 iteratorY = extraction.birthmarkSets(ExtractionTarget.TARGET_Y);
194             }
195
196             if(setX != null){
197                 for(; iteratorY.hasNext(); ){
198                     BirthmarkSet setY = iteratorY.next();
199                     if(setX.getName().equals(setY.getName())){
200                         next = new ComparisonPair(setX, setY, extraction.getContext());
201                         break;
202                     }
203                 }
204
205                 if(iteratorX.hasNext()){
206                     setX = iteratorX.next();
207                 }
208                 else{
209                     setX = null;
210                 }
211                 if(next == null){
212                     next = findNext();
213                 }
214             }
215             return next;
216         }
217     };
218
219     private static class NameMappingIterator implements Iterator<ComparisonPair>{
220         private Iterator<Map.Entry<String, String>> names;
221         private ComparisonPair nextPair;
222         private ExtractionResultSet ers;
223
224         public NameMappingIterator(ExtractionResultSet ers){
225             this.ers = ers;
226             names = ers.getContext().nameMappingEntries();
227             nextPair = findNextPair();
228         }
229
230         public ComparisonPair next(){
231             ComparisonPair cp = nextPair;
232             nextPair = findNextPair();
233             return cp;
234         }
235
236         public boolean hasNext(){
237             return nextPair != null;
238         }
239
240         public void remove(){
241         }
242
243         private ComparisonPair findNextPair(){
244             ComparisonPair pair = null;
245             if(names.hasNext()){
246                 Map.Entry<String, String> entry = names.next();
247                 String n1 = entry.getKey();
248                 String n2 = entry.getValue();
249
250                 BirthmarkSet bs1 = ers.getBirthmarkSet(ExtractionTarget.TARGET_X, n1);
251                 BirthmarkSet bs2 = ers.getBirthmarkSet(ExtractionTarget.TARGET_Y, n2);
252
253                 if(bs1 == null || bs2 == null){
254                     pair = findNextPair();
255                 }
256                 else{
257                     pair = new ComparisonPair(bs1, bs2, ers.getContext());
258                 }
259             }
260             return pair;
261         }
262     };
263 }