1 package jp.naist.se.stigmata;
\r
7 import java.util.ArrayList;
\r
8 import java.util.Iterator;
\r
9 import java.util.List;
\r
12 * This class represents comparison pair.
\r
13 * @author Haruaki TAMADA
\r
14 * @version $Revision$ $Date$
\r
16 public class ComparisonPair implements Iterable<ComparisonPairElement>{
\r
17 private BirthmarkSet target1;
\r
18 private BirthmarkSet target2;
\r
19 private BirthmarkContext context;
\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
29 if(target1.getBirthmarksCount() != target2.getBirthmarksCount()){
\r
30 throw new IllegalArgumentException("birthmark count is not matched");
\r
36 * @see #getTarget2()
\r
38 public BirthmarkSet getTarget1(){
\r
43 * return other target
\r
44 * @see #getTarget1()
\r
46 public BirthmarkSet getTarget2(){
\r
51 * calculate similarity between target1 and target2.
\r
53 public double calculateSimilarity(){
\r
54 double similarity = 0d;
\r
56 for(ComparisonPairElement elem: this){
\r
57 double sim = elem.getSimilarity();
\r
58 if(!Double.isNaN(sim) && !Double.isInfinite(sim)){
\r
63 return similarity / count;
\r
67 * Return the number of birthmarks contained a target.
\r
68 * Note that other target must have same birthmarks.
\r
70 public int getBirthmarksCount(){
\r
71 return target1.getBirthmarksCount();
\r
75 * return the iterator for comparing each birthmarks.
\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
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
91 return list.iterator();
\r