OSDN Git Service

change internal process method, which introducing ExtractionResultSet
[stigmata/stigmata.git] / src / main / java / jp / naist / se / stigmata / ComparisonPair.java
1 package jp.naist.se.stigmata;\r
2 \r
3 /*\r
4  * $Id$\r
5  */\r
6 \r
7 import java.util.ArrayList;\r
8 import java.util.Iterator;\r
9 import java.util.List;\r
10 \r
11 /**\r
12  * This class represents comparison pair.\r
13  * @author  Haruaki TAMADA\r
14  * @version  $Revision$ $Date$\r
15  */\r
16 public class ComparisonPair implements Iterable<ComparisonPairElement>{\r
17     private BirthmarkSet target1;\r
18     private BirthmarkSet target2;\r
19     private BirthmarkContext context;\r
20 \r
21     /**\r
22      * constructor.\r
23      */\r
24     public ComparisonPair(BirthmarkSet target1, BirthmarkSet target2, BirthmarkContext context){\r
25         this.target1 = target1;\r
26         this.target2 = target2;\r
27         this.context = context;\r
28 \r
29         if(target1.getBirthmarksCount() != target2.getBirthmarksCount()){\r
30             throw new IllegalArgumentException("birthmark count is not matched");\r
31         }\r
32     }\r
33 \r
34     /**\r
35      * return a target.\r
36      * @see  #getTarget2()\r
37      */\r
38     public BirthmarkSet getTarget1(){\r
39         return target1;\r
40     }\r
41 \r
42     /**\r
43      * return other target\r
44      * @see  #getTarget1()\r
45      */\r
46     public BirthmarkSet getTarget2(){\r
47         return target2;\r
48     }\r
49 \r
50     /**\r
51      * calculate similarity between target1 and target2.\r
52      */\r
53     public double calculateSimilarity(){\r
54         double similarity = 0d;\r
55         int count = 0;\r
56         for(ComparisonPairElement elem: this){\r
57             double sim = elem.getSimilarity();\r
58             if(!Double.isNaN(sim) && !Double.isInfinite(sim)){\r
59                 similarity += sim;\r
60                 count++;\r
61             }\r
62         }\r
63         return similarity / count;\r
64     }\r
65 \r
66     /**\r
67      * Return the number of birthmarks contained a target.\r
68      * Note that other target must have same birthmarks.\r
69      */\r
70     public int getBirthmarksCount(){\r
71         return target1.getBirthmarksCount();\r
72     }\r
73 \r
74     /**\r
75      * return the iterator for comparing each birthmarks.\r
76      */\r
77     public synchronized Iterator<ComparisonPairElement> iterator(){\r
78         List<ComparisonPairElement> list = new ArrayList<ComparisonPairElement>();\r
79         BirthmarkEnvironment env = context.getEnvironment();\r
80         for(Iterator<String> i = target1.birthmarkTypes(); i.hasNext(); ){\r
81             String type = i.next();\r
82 \r
83             Birthmark b1 = target1.getBirthmark(type);\r
84             Birthmark b2 = target2.getBirthmark(type);\r
85             if(b1 != null && b2 != null){\r
86                 list.add(new ComparisonPairElement(\r
87                     b1, b2, env.getService(type).getComparator()\r
88                 ));\r
89             }\r
90         }\r
91         return list.iterator();\r
92     }\r
93 }\r