]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/CountifFormulaFunction.java
f34b89f5a57026ab818f516cd95517b9fa06ae1c
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / formula / CountifFormulaFunction.java
1 package org.simantics.spreadsheet.graph.formula;
2
3 import org.simantics.spreadsheet.graph.CellFormulaFunction;
4 import org.simantics.spreadsheet.graph.CellValueVisitor;
5 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;
6 import org.simantics.spreadsheet.graph.SpreadsheetMatrix;
7 import org.simantics.spreadsheet.graph.parser.ast.AstArgList;
8 import org.simantics.spreadsheet.graph.parser.ast.AstRange;
9
10 public class CountifFormulaFunction implements CellFormulaFunction<Integer> {
11
12     @Override
13     public Integer evaluate(CellValueVisitor visitor, AstArgList args) {
14         if (args.values.size() != 2) throw new IllegalStateException();
15         
16         AstRange range = (AstRange) args.values.get(0);
17         SpreadsheetMatrix matrix = (SpreadsheetMatrix) range.accept(visitor);
18         Object crit = null;
19         try {
20                 crit = args.values.get(1).accept(visitor);
21         } catch (IllegalStateException e){
22                 return 0;
23         }
24         
25         String criteria = SpreadsheetGraphUtils.asString(crit);
26         int result = 0;
27         for (Object value : matrix.values) {
28             if (SpreadsheetGraphUtils.matchCriteria(value, criteria))
29                 result++;
30         }
31         return Integer.valueOf(result);
32     }
33 }