1 package jp.sourceforge.stigmata.filter;
3 import java.util.ArrayList;
6 import jp.sourceforge.stigmata.ComparisonPair;
7 import jp.sourceforge.stigmata.spi.ComparisonPairFilterService;
11 * @author Haruaki TAMADA
13 public class BirthmarkElementCountComparisonPairFilter extends AbstractComparisonPairFilter{
14 private static final List<Criterion> CRITERIA = new ArrayList<Criterion>();
16 private int threshold = 0;
17 private FilterTarget target;
18 private String birthmarkType;
21 CRITERIA.add(Criterion.GREATER_EQUALS);
22 CRITERIA.add(Criterion.GREATER_THAN);
23 CRITERIA.add(Criterion.LESS_EQUALS);
24 CRITERIA.add(Criterion.LESS_THAN);
25 CRITERIA.add(Criterion.EQUALS_AS);
26 CRITERIA.add(Criterion.NOT_EQUALS_AS);
29 public BirthmarkElementCountComparisonPairFilter(ComparisonPairFilterService service){
33 public String getBirthmarkType(){
37 public void setBirthmarkType(String birthmarkType){
38 this.birthmarkType = birthmarkType;
41 public static Criterion[] getValidCriteria(){
42 return CRITERIA.toArray(new Criterion[CRITERIA.size()]);
46 public Criterion[] getAcceptableCriteria(){
47 return getValidCriteria();
50 private boolean isFilteredTwo(ComparisonPair pair){
53 String type = getBirthmarkType();
54 if(pair.getTarget1().hasBirthmark(type) && pair.getTarget2().hasBirthmark(type)){
55 int elem1 = pair.getTarget1().getBirthmark(type).getElementCount();
56 int elem2 = pair.getTarget2().getBirthmark(type).getElementCount();
58 switch(getCriterion()){
60 flag = (target == FilterTarget.BOTH_TARGETS && elem1 >= threshold && elem2 >= threshold) ||
61 (target == FilterTarget.ONE_OF_TARGETS && (elem1 >= threshold || elem2 >= threshold));
64 flag = (target == FilterTarget.BOTH_TARGETS && elem1 > threshold && elem2 > threshold) ||
65 (target == FilterTarget.ONE_OF_TARGETS && (elem1 > threshold || elem2 > threshold));
68 flag = (target == FilterTarget.BOTH_TARGETS && elem1 <= threshold && elem2 <= threshold) ||
69 (target == FilterTarget.ONE_OF_TARGETS && (elem1 <= threshold || elem2 <= threshold));
72 flag = (target == FilterTarget.BOTH_TARGETS && elem1 < threshold && elem2 < threshold) ||
73 (target == FilterTarget.ONE_OF_TARGETS && (elem1 < threshold || elem2 < threshold));
76 flag = (target == FilterTarget.BOTH_TARGETS && elem1 == threshold && elem2 == threshold) ||
77 (target == FilterTarget.ONE_OF_TARGETS && (elem1 == threshold || elem2 == threshold));
80 flag = (target == FilterTarget.BOTH_TARGETS && elem1 != threshold && elem2 != threshold) ||
81 (target == FilterTarget.ONE_OF_TARGETS && (elem1 != threshold || elem2 != threshold));
92 public boolean isFiltered(ComparisonPair pair){
93 if(target == FilterTarget.BOTH_TARGETS || target == FilterTarget.ONE_OF_TARGETS){
94 return isFilteredTwo(pair);
97 String type = getBirthmarkType();
98 if(pair.getTarget1().hasBirthmark(type) && pair.getTarget2().hasBirthmark(type)){
100 int threshold = getThreshold();
101 if(target == FilterTarget.TARGET_1){
102 total = pair.getTarget1().getBirthmark(type).getElementCount();
104 if(target == FilterTarget.TARGET_2){
105 total = pair.getTarget2().getBirthmark(type).getElementCount();
107 switch(getCriterion()){
109 flag = total >= threshold;
112 flag = total > threshold;
115 flag = total <= threshold;
118 flag = total < threshold;
121 flag = total == threshold;
124 flag = total != threshold;
134 public int getThreshold(){
138 public void setThreshold(int threshold){
140 throw new IllegalArgumentException("threshold must be positive value: " + threshold);
142 this.threshold = threshold;
145 public FilterTarget getTarget(){
149 public void setTarget(FilterTarget target){
150 this.target = target;
154 public String toString(){
155 StringBuilder sb = new StringBuilder();
157 case TARGET_1: sb.append("target1"); break;
158 case TARGET_2: sb.append("target2"); break;
159 case BOTH_TARGETS: sb.append("(target1&target2)"); break;
160 case ONE_OF_TARGETS: sb.append("(target1|target2)");
162 sb.append(".").append(birthmarkType);
164 switch(getCriterion()){
165 case GREATER_EQUALS: sb.append(" >= "); break;
166 case GREATER_THAN: sb.append(" > "); break;
167 case LESS_EQUALS: sb.append(" <= "); break;
168 case LESS_THAN: sb.append(" < "); break;
169 case EQUALS_AS: sb.append(" == "); break;
170 case NOT_EQUALS_AS: sb.append(" != "); break;
172 sb.append(Integer.toString(getThreshold()));
174 return new String(sb);