OSDN Git Service

change package name. jp.naist.se.stigmata -> jp.sourceforge.stigmata
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / filter / BirthmarkElementCountComparisonPairFilter.java
1 package jp.sourceforge.stigmata.filter;
2
3 /*
4  * $Id$
5  */
6
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import jp.sourceforge.stigmata.ComparisonPair;
11 import jp.sourceforge.stigmata.spi.ComparisonPairFilterSpi;
12
13 /**
14  * 
15  * @author Haruaki TAMADA
16  * @version $Revision$ $Date$
17  */
18 public class BirthmarkElementCountComparisonPairFilter extends AbstractComparisonPairFilter{
19     private static final List<Criterion> CRITERIA = new ArrayList<Criterion>();
20
21     private int threshold = 0;
22     private FilterTarget target;
23     private String birthmarkType;
24
25     static{
26         CRITERIA.add(Criterion.GREATER_EQUALS);
27         CRITERIA.add(Criterion.GREATER_THAN);
28         CRITERIA.add(Criterion.LESS_EQUALS);
29         CRITERIA.add(Criterion.LESS_THAN);
30         CRITERIA.add(Criterion.EQUALS_AS);
31         CRITERIA.add(Criterion.NOT_EQUALS_AS);
32     }
33
34     public BirthmarkElementCountComparisonPairFilter(ComparisonPairFilterSpi service){
35         super(service);
36     }
37
38     public String getBirthmarkType(){
39         return birthmarkType;
40     }
41
42     public void setBirthmarkType(String birthmarkType){
43         this.birthmarkType = birthmarkType;
44     }
45
46     public static Criterion[] getValidCriteria(){
47         return CRITERIA.toArray(new Criterion[CRITERIA.size()]);
48     }
49
50     public Criterion[] getAcceptableCriteria(){
51         return getValidCriteria();
52     }
53
54     private boolean isFilteredTwo(ComparisonPair pair){
55         boolean flag = false;
56
57         String type = getBirthmarkType();
58         if(pair.getTarget1().hasBirthmark(type) && pair.getTarget2().hasBirthmark(type)){
59             int elem1 = pair.getTarget1().getBirthmark(type).getElementCount();
60             int elem2 = pair.getTarget2().getBirthmark(type).getElementCount();
61
62             switch(getCriterion()){
63             case GREATER_EQUALS:
64                 flag = (target == FilterTarget.BOTH_TARGETS && elem1 >= threshold && elem2 >= threshold) ||
65                 (target == FilterTarget.ONE_OF_TARGETS && (elem1 >= threshold || elem2 >= threshold));
66                 break;
67             case GREATER_THAN:
68                 flag = (target == FilterTarget.BOTH_TARGETS && elem1 > threshold && elem2 > threshold) ||
69                     (target == FilterTarget.ONE_OF_TARGETS && (elem1 > threshold || elem2 > threshold));
70                 break;
71             case LESS_EQUALS:
72                 flag = (target == FilterTarget.BOTH_TARGETS && elem1 <= threshold && elem2 <= threshold) ||
73                 (target == FilterTarget.ONE_OF_TARGETS && (elem1 <= threshold || elem2 <= threshold));
74                 break;
75             case LESS_THAN:
76                 flag = (target == FilterTarget.BOTH_TARGETS && elem1 < threshold && elem2 < threshold) ||
77                     (target == FilterTarget.ONE_OF_TARGETS && (elem1 < threshold || elem2 < threshold));
78                 break;
79             case EQUALS_AS:
80                 flag = (target == FilterTarget.BOTH_TARGETS && elem1 == threshold && elem2 == threshold) ||
81                 (target == FilterTarget.ONE_OF_TARGETS && (elem1 == threshold || elem2 == threshold));
82                 break;
83             case NOT_EQUALS_AS:
84                 flag = (target == FilterTarget.BOTH_TARGETS && elem1 != threshold && elem2 != threshold) ||
85                     (target == FilterTarget.ONE_OF_TARGETS && (elem1 != threshold || elem2 != threshold));
86                 break;
87             default:
88                 flag = false;
89                 break;
90             }
91         }
92         return flag;
93     }
94
95     public boolean isFiltered(ComparisonPair pair){
96         if(target == FilterTarget.BOTH_TARGETS || target == FilterTarget.ONE_OF_TARGETS){
97             return isFilteredTwo(pair);
98         }
99         boolean flag = false;
100         String type = getBirthmarkType();
101         if(pair.getTarget1().hasBirthmark(type) && pair.getTarget2().hasBirthmark(type)){
102             int total = 0;
103             int threshold = getThreshold();
104             if(target == FilterTarget.TARGET_1){
105                 total = pair.getTarget1().getBirthmark(type).getElementCount();
106             }
107             if(target == FilterTarget.TARGET_2){
108                 total = pair.getTarget2().getBirthmark(type).getElementCount();
109             }
110             switch(getCriterion()){
111             case GREATER_EQUALS:
112                 flag = total >= threshold;
113                 break;
114             case GREATER_THAN:
115                 flag = total > threshold;
116                 break;
117             case LESS_EQUALS:
118                 flag = total <= threshold;
119                 break;
120             case LESS_THAN:
121                 flag = total < threshold;
122                 break;
123             case EQUALS_AS:
124                 flag = total == threshold;
125                 break;
126             case NOT_EQUALS_AS:
127                 flag = total != threshold;
128                 break;
129             default:
130                 flag = false;
131                 break;
132             }
133         }
134         return flag;
135     }
136
137     public int getThreshold(){
138         return threshold;
139     }
140
141     public void setThreshold(int threshold){
142         if(threshold < 0){
143             throw new IllegalArgumentException("threshold must be positive value: " + threshold);
144         }
145         this.threshold = threshold;
146     }
147
148     public FilterTarget getTarget(){
149         return target;
150     }
151
152     public void setTarget(FilterTarget target){
153         this.target = target;
154     }
155
156     public String toString(){
157         StringBuilder sb = new StringBuilder();
158         switch(getTarget()){
159         case TARGET_1:       sb.append("target1"); break;
160         case TARGET_2:       sb.append("target2"); break;
161         case BOTH_TARGETS:   sb.append("(target1&target2)");    break;
162         case ONE_OF_TARGETS: sb.append("(target1|target2)");
163         }
164         sb.append(".").append(birthmarkType);
165         sb.append(".size");
166         switch(getCriterion()){
167         case GREATER_EQUALS: sb.append(" >= "); break;
168         case GREATER_THAN:   sb.append(" >  "); break;
169         case LESS_EQUALS:    sb.append(" <= "); break;
170         case LESS_THAN:      sb.append(" <  "); break;
171         case EQUALS_AS:      sb.append(" == "); break;
172         case NOT_EQUALS_AS:  sb.append(" != "); break;
173         }
174         sb.append(Integer.toString(getThreshold()));
175
176         return new String(sb);
177     }
178 }