📄 groupingfunctions.java
字号:
} public void checkType (Object o, Class expected, Expression exp) throws QueryExecutionException { if (!expected.isInstance (o)) { throw new QueryExecutionException ("Expression: " + exp + " returns type: " + o.getClass ().getName () + " however must return instance of: " + expected.getName ()); } } public Double sum (List allobjs, Expression exp, String saveValueName) throws QueryExecutionException { if (saveValueName != null) { Object o = this.q.getSaveValue (saveValueName); if (o != null) { return (Double) o; } } if ((allobjs == null) || (allobjs.size () == 0) ) { return new Double (0); } double total = this.getTotal (allobjs, exp); Double d = new Double (total); if ((saveValueName != null) && (q != null) ) { this.q.setSaveValue (saveValueName, d); } return d; } public Double sum (Expression exp) throws QueryExecutionException { return this.sum ((List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), exp); } public Double sum (List objs, Expression exp) throws QueryExecutionException { Class c = null; try { c = exp.getExpectedReturnType (this.q); } catch (Exception e) { throw new QueryExecutionException ("Unable to determine expected return type for expression: " + exp, e); } boolean dyn = false; if (!c.getName ().equals (Object.class.getName ())) { // Should return a number... if (!Utilities.isNumber (c)) { throw new QueryExecutionException ("This function expects the expression: " + exp + " to return a number (sub-class of: " + Number.class.getName () + ") but evaluation of the expression will return an instance of: " + c.getName ()); } } else { dyn = true; } Object co = this.q.getCurrentObject (); int s = objs.size () - 1; double d = 0; for (int i = s; i > -1; i--) { Object o = objs.get (i); this.q.setCurrentObject (o); Object v = null; try { v = exp.getValue (o, this.q); } catch (Exception e) { throw new QueryExecutionException ("Unable to evaluate expression: " + exp + " on item: " + i, e); } if (v == null) { // Skip... i.e. assume it's zero. continue; } if (dyn) { if (!(Utilities.isNumber (v))) { throw new QueryExecutionException ("Expected expression: " + exp + " to return a number (sub-class of: " + Number.class.getName () + ") but returns instance of: " + o.getClass ().getName () + " for item: " + i + " (class: " + v.getClass ().getName () + ")"); } } d += ((Number) v).doubleValue (); } this.q.setCurrentObject (co); return new Double (d); } /** * This function allows you to specify your own accessor as a string that will * be used to access the relevant value for each of the objects in the <b>objs</b> * List. * * @param objs The List of objects you wish to sum over. * @param acc The accessor to create for accessing the value in each of the objects in <b>objs</b>. * @return The summed value. * @throws QueryExecutionException If the accessor is not valid for the objects in the list or * if the accessor throws an exception. */ public Double sum (List objs, String acc) throws QueryExecutionException { if ((objs == null) || (objs.size () == 0) ) { return new Double (0); } // Get the first object. Object o = objs.get (0); Getter get = null; try { get = new Getter (acc, o.getClass ()); } catch (Exception e) { throw new QueryExecutionException ("Unable to create accessor for: " + acc + " with class: " + o.getClass ().getName (), e); } if (!get.getType ().getName ().equals (Object.class.getName ())) { // Should return a number... if (!Utilities.isNumber (get.getType ())) { throw new QueryExecutionException ("This function expects the accessor (second parm): " + acc + " to return a number (sub-class of: " + Number.class.getName () + ") but evaluation of the accessor will return an instance of: " + get.getType ().getName ()); } } int s = objs.size () - 1; double d = 0; for (int i = s; i > -1; i--) { o = objs.get (i); Object v = null; try { v = get.getValue (o); } catch (Exception e) { throw new QueryExecutionException ("Unable to evaluate accessor: " + acc + " on item: " + i, e); } if (v == null) { // Skip... i.e. assume it's zero. continue; } d += ((Number) v).doubleValue (); } return new Double (d); } public String concat (List allobjs, Expression exp, String sep, String saveValueName) throws QueryExecutionException { if (saveValueName != null) { Object o = this.q.getSaveValue (saveValueName); if (o != null) { return (String) o; } } StringBuffer buf = new StringBuffer (); int size = allobjs.size (); int size1 = size - 1; for (int i = 0; i < size; i++) { Object o = allobjs.get (i); Object v = null; try { v = exp.getValue (o, this.q); } catch (Exception e) { throw new QueryExecutionException ("Unable to get value from expression: " + exp + " for item: " + i + " from the list of objects.", e); } buf.append (v); if ((sep != null) && (i < size1) ) { buf.append (sep); } } String r = buf.toString (); if ((saveValueName != null) && (q != null) ) { q.setSaveValue (saveValueName, r); } return r; } public String concat (List allobjs, Expression exp, String sep) throws QueryExecutionException { return this.concat (allobjs, exp, sep, null); } public String concat (Expression exp) throws QueryExecutionException { return this.concat ((List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), exp); } public String concat (List allobjs, Expression exp) throws QueryExecutionException { return this.concat (allobjs, exp, null, null); } public Double avg (List allobjs, Expression exp, String saveValueName) throws QueryExecutionException { if (saveValueName != null) { Object o = this.q.getSaveValue (saveValueName); if (o != null) { return (Double) o; } } if ((allobjs == null) || (allobjs.size () == 0) ) { return new Double (0); } double total = this.getTotal (allobjs, exp); double avg = total / allobjs.size (); Double d = new Double (avg); if (saveValueName != null) { q.setSaveValue (saveValueName, d); } return d; } public Double avg (Expression exp) throws QueryExecutionException { return this.avg ((List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), exp); } public Double avg (List allobjs, Expression exp) throws QueryExecutionException { return this.avg (allobjs, exp, null); } /** * A function that will take each item from the passed in List and * determine a "count" for each item, i.e. how many times each item appears. * * @param objs The List of objects to operate on. * @return A Map of object to a count of the number of times the object appears in the list. * @throws QueryExecutionException Won't happen in this method. */ public Map occurrence (List objs) throws QueryExecutionException { return this.occurrence (objs, null); } /* public Map occurrence (Expression exp) throws QueryExecutionException { return this.occurrence ((List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), exp); } */ /** * A function that will take each item from the passed in List and * determine a "count" for each item, i.e. how many times each item appears. * * @param objs The List of objects to operate on. * @param exp An optional expression that should be performed on each object * and the value returned used instead. * @return A Map of object to a count of the number of times the object appears in the list. * @throws QueryExecutionException If the expression cannot be evaluated. */ public Map occurrence (List objs, Expression exp) throws QueryExecutionException { Map occs = new HashMap (); if (objs == null) { return occs; } int s = objs.size (); for (int i = 0; i < s; i++) { Object o = objs.get (i); if (exp != null) { try { o = exp.getValue (o, this.q); } catch (Exception e) { throw new QueryExecutionException ("Unable to get value for expression: " + exp + " for object: " + i + " from the list of objects.", e); } } Integer c = (Integer) occs.get (o); int co = 1; if (c != null) { co = c.intValue (); co++; } occs.put (o, new Integer (co)); } return occs; } /** * This is the same as {@link #occurrence(List,Expression)} except that the * second expression should evaluate to a number that will be used to limit the * results, the occurrence count must be greater than or equal to the value from * the expression. * * @param objs The List of objects to operate on. * @param exp An optional expression that should be performed on each object * and the value returned used instead. * @param limitExp An expression that when evaluated should return a number, this * will then be used to limit the results returned to those that have an * occurrence count >= that number. * @return A Map of object to a count of the number of times the object appears in the list. * @throws QueryExecutionException If the expression cannot be evaluated or the <b>limitExp</b> * arg does not evaulate to a number. */ public Map occurrence (List objs, Expression exp, Expression limitExp) throws QueryExecutionException { Map rs = this.occurrence (objs, exp); // Evaluate the limit expression. Object o = limitExp.getValue (this.q.getCurrentObject (), this.q); if (!(o instanceof Number)) { throw new QueryExecutionException ("Limit expression: " + limitExp + " does not evaluate to a number"); } int i = ((Number) o).intValue (); Map ret = new HashMap (); Iterator iter = rs.keySet ().iterator (); while (iter.hasNext ()) { Object k = iter.next (); Integer c = (Integer) rs.get (k); if (c.intValue () >= i) { ret.put (k, c); } } return ret; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -