OSDN Git Service

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