📄 groupingfunctions.java
字号:
/* * Copyright 2004-2005 Gary Bentley * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.josql.functions;import java.util.List;import java.util.ArrayList;import java.util.Collections;import java.util.Map;import java.util.HashMap;import java.util.Iterator;import com.gentlyweb.utils.Getter;import org.josql.QueryExecutionException;import org.josql.internal.Utilities;import org.josql.expressions.Expression;import org.josql.Query;public class GroupingFunctions extends AbstractFunctionHandler{ public static final String VALUE = "value"; public static final String HANDLER_ID = "_internal_grouping"; public Object least (List allobjs, Expression exp, String saveValueName) throws QueryExecutionException { if (saveValueName != null) { Object o = this.q.getSaveValue (saveValueName); if (o != null) { return o; } } if (allobjs.size () == 0) { return null; } Object g = null; int s = allobjs.size (); for (int i = 0; i < s; 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 maximum value" + e); } if (g == null) { g = v; } else { int c = Utilities.compare (v, g); if (c < 0) { g = v; } } } if ((saveValueName != null) && (q != null) ) { q.setSaveValue (saveValueName, g); } return g; } public Object minObject (Expression exp) throws QueryExecutionException { return this.minObject ((List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), exp); } public Object minObject (List allobjs, Expression exp) throws QueryExecutionException { return this.leastObject (allobjs, exp); } public Object leastObject (List allobjs, Expression exp) throws QueryExecutionException { if (allobjs.size () == 0) { return null; } Object l = null; Object lo = null; int s = allobjs.size (); for (int i = 0; i < s; 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 maximum value" + e); } if (l == null) { l = v; lo = o; } else { int c = Utilities.compare (v, l); if (c < 0) { l = v; lo = o; } } } return lo; } public Object maxObject (List allobjs, Expression exp) throws QueryExecutionException { return this.greatestObject (allobjs, exp); } public Object maxObject (Expression exp) throws QueryExecutionException { return this.maxObject ((List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), exp); } public Object greatestObject (List allobjs, Expression exp) throws QueryExecutionException { if (allobjs.size () == 0) { return null; } Object g = null; Object go = null; int s = allobjs.size (); for (int i = 0; i < s; 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 maximum value" + e); } if (g == null) { g = v; go = o; } else { int c = Utilities.compare (v, g); if (c > 0) { g = v; go = o; } } } return go; } public Object least (List allobjs, Expression exp) throws QueryExecutionException { return this.least (allobjs, exp, null); } public Object min (Expression exp) throws QueryExecutionException { return this.min ((List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), exp); } public Object min (List allobjs, Expression exp) throws QueryExecutionException { return this.least (allobjs, exp, null); } public Object min (List allobjs, Expression exp, String saveValueName) throws QueryExecutionException { return this.least (allobjs, exp, saveValueName); } public Map.Entry maxEntry (Map m, String type) { int t = 0; if (type.equals (GroupingFunctions.VALUE)) { t = 1; } Map.Entry r = null; Map.Entry le = null; Iterator iter = m.entrySet ().iterator (); while (iter.hasNext ()) { r = (Map.Entry) iter.next (); if (le != null) { if (t == 0) { if (Utilities.isGTEquals (r.getKey (), le.getKey ())) { le = r; } } else { if (Utilities.isGTEquals (r.getValue (), le.getValue ())) { le = r; } } } else { le = r; } } return le; } public Map.Entry minEntry (Object m, String type) throws QueryExecutionException { if (!(m instanceof Map)) { throw new QueryExecutionException ("Only instances of: " + Map.class.getName () + " are supported, passed: " + m.getClass ().getName ()); } return this.minEntry ((Map) m, type); } public Map.Entry minEntry (Map m, String type) { int t = 0; if (type.equals (GroupingFunctions.VALUE)) { t = 1; } Map.Entry r = null; Map.Entry le = null; Iterator iter = m.entrySet ().iterator (); while (iter.hasNext ()) { r = (Map.Entry) iter.next (); if (le != null) { if (t == 0) { if (Utilities.isLTEquals (r.getKey (), le.getKey ())) { le = r; } } else { if (Utilities.isLTEquals (r.getValue (), le.getValue ())) { le = r; } } } else { le = r; } } return le; } public Object max (List allobjs, Expression exp, String saveValueName) throws QueryExecutionException { return this.greatest (allobjs, exp, saveValueName); } public Object greatest (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.size () == 0) { return null; } Object g = null; int s = allobjs.size (); for (int i = 0; i < s; 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 maximum value" + e); } if (g == null) { g = v; } else { int c = Utilities.compare (v, g); if (c > 0) { g = v; } } } if (saveValueName != null) { this.q.setSaveValue (saveValueName, g); } return g; } public Object greatest (List allobjs, Expression exp) throws QueryExecutionException { return this.greatest (allobjs, exp, null); } public Object max (Expression exp) throws QueryExecutionException { return this.max ((List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), exp); } public Object max (List allobjs, Expression exp) throws QueryExecutionException { return this.greatest (allobjs, exp, null); } private double getTotal (List allobjs, Expression exp) throws QueryExecutionException { Object currObj = this.q.getCurrentObject (); double total = 0; int size = allobjs.size (); for (int i = 0; i < size; i++) { Object o = allobjs.get (i); this.q.setCurrentObject (o); Number n = null; try { n = (Number) 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); } total += n.doubleValue (); } this.q.setCurrentObject (currObj); return total;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -