1 package jp.sourceforge.stigmata;
3 import java.util.ArrayList;
4 import java.util.Iterator;
8 * This class represents comparison pair.
10 * @author Haruaki TAMADA
12 public class ComparisonPair implements Iterable<ComparisonPairElement>{
13 private BirthmarkSet target1;
14 private BirthmarkSet target2;
15 private BirthmarkContext context;
20 public ComparisonPair(BirthmarkSet target1, BirthmarkSet target2, BirthmarkContext context){
21 this.target1 = target1;
22 this.target2 = target2;
23 this.context = context;
25 if(target1.getBirthmarksCount() != target2.getBirthmarksCount()){
26 throw new IllegalArgumentException("birthmark count is not matched");
34 public BirthmarkSet getTarget1(){
42 public BirthmarkSet getTarget2(){
47 * calculates similarity between target1 and target2.
49 public double calculateSimilarity(){
50 double similarity = 0d;
52 for(ComparisonPairElement elem: this){
53 double sim = elem.getSimilarity();
54 if(!Double.isNaN(sim) && !Double.isInfinite(sim)){
59 return similarity / count;
63 * Returns a number of birthmarks contained a target.
64 * Note that other target must have same birthmarks.
66 public int getBirthmarksCount(){
67 return target1.getBirthmarksCount();
71 * returns an iterator for comparing each birthmarks.
74 public synchronized Iterator<ComparisonPairElement> iterator(){
75 List<ComparisonPairElement> list = new ArrayList<ComparisonPairElement>();
76 BirthmarkEnvironment env = context.getEnvironment();
77 for(Iterator<String> i = target1.birthmarkTypes(); i.hasNext(); ){
78 String type = i.next();
80 Birthmark b1 = target1.getBirthmark(type);
81 Birthmark b2 = target2.getBirthmark(type);
82 if(b1 != null && b2 != null){
83 list.add(new ComparisonPairElement(
84 b1, b2, env.getService(type).getComparator(), context
88 return list.iterator();