1 15 package org.josql.internal; 16 17 import java.util.List ; 18 import java.util.ArrayList ; 19 import java.util.Map ; 20 import java.util.HashMap ; 21 22 import org.josql.Query; 23 import org.josql.QueryExecutionException; 24 25 import org.josql.expressions.Expression; 26 27 public class Grouper 28 { 29 30 private List cols = new ArrayList (); 31 private Query q = null; 32 private int cs = -1; 33 34 public Grouper (Query q) 35 { 36 37 this.q = q; 38 39 } 40 41 public List getExpressions () 42 { 43 44 return this.cols; 45 46 } 47 48 public void addExpression (Expression e) 49 { 50 51 this.cols.add (e); 52 this.cs = cols.size (); 53 54 } 55 56 public Map group (List objs) 57 throws QueryExecutionException 58 { 59 60 Map retVals = new HashMap (); 61 62 int s = objs.size (); 63 64 List l = null; 65 66 for (int j = 0; j < s; j++) 67 { 68 69 Object o = objs.get (j); 70 71 this.q.setCurrentObject (o); 72 73 l = new ArrayList (); 74 75 for (int i = 0; i < this.cs; i++) 77 { 78 79 Expression exp = (Expression) this.cols.get (i); 80 81 try 82 { 83 84 l.add (exp.getValue (o, 85 this.q)); 86 87 } catch (Exception e) { 88 89 throw new QueryExecutionException ("Unable to get group by value for expression: " + 90 exp, 91 e); 92 93 } 94 95 } 96 97 List v = (List ) retVals.get (l); 98 99 if (v == null) 100 { 101 102 v = new ArrayList (); 103 104 retVals.put (l, 105 v); 106 107 } 108 109 v.add (o); 110 111 } 112 113 return retVals; 114 115 } 116 117 } 118 | Popular Tags |