📄 collectionfunctions.java
字号:
} catch (Exception e) { throw new QueryExecutionException ("Unable to execute expression: \"" + exp + " on object at index: " + i + " from the list of objects.", e); } } if (saveValueName != null) { this.q.setSaveValue (saveValueName, retVals); } // Reset the current object. this.q.setCurrentObject (co); return retVals; } public List collect (Expression exp) throws QueryExecutionException { return this.collect ((List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), exp); } public List collect (List allobjs, Expression exp) throws QueryExecutionException { return this.collect (allobjs, exp, null); } public List toList (Expression exp) throws QueryExecutionException { return this.toList ((List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), exp); } public List toList (List allobjs, Expression exp) throws QueryExecutionException { return this.collect (allobjs, exp, null); } public List foreach (Expression exp) throws QueryExecutionException { return this.foreach ((List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), exp); } public List foreach (List allobjs, Expression exp) throws QueryExecutionException { if (allobjs == null) { return null; } List res = new ArrayList (); int s = allobjs.size (); for (int i = 0; i < s; i++) { Object o = allobjs.get (i); this.q.setCurrentObject (o); res.add (exp.getValue (o, this.q)); } return res; } private void fillMap (Object o, Getter k, Getter v) throws QueryExecutionException { } /* public Map toMap (Object o, Getter k, Getter v) throws QueryExecutionException { } public Map toMap (Object o, Getter k, Getter v, String type) throws QueryExecutionException { // Try and create the correct "Map" type. } */ /** * Given a list of objects, execute the expression against each one and return * those objects that return a <code>true</code> value for the expression. * In effect this is equivalent to executing the WHERE clause of a JoSQL statement * against each object (which in fact is what happens internally). The class * for the objects if found by examining the list passed in. * * @param objs The list of objects. * @param exp The expression (basically a where clause, it is ok for the expression * to start with "WHERE", case-insensitive) to execute for each of the * objects. * @return The list of matching objects. */ public List foreach (List objs, String exp) throws QueryExecutionException { List l = new ArrayList (); if ((objs == null) || (objs.size () == 0) ) { return l; } Query q = null; // See if we have the expression in our cache. if (this.foreachQueryCache != null) { q = (Query) this.foreachQueryCache.get (exp); } if (q == null) { // Init our query. Class c = null; Object o = objs.get (0); if (o == null) { int s = objs.size () - 1; // Bugger now need to cycle until we get a class. for (int i = s; s > -1; i--) { o = objs.get (i); if (o != null) { c = o.getClass (); break; } } } else { c = o.getClass (); } if (exp.toLowerCase ().trim ().startsWith ("where")) { exp = exp.trim ().substring (5); } String query = "SELECT * FROM " + c.getName () + " WHERE " + exp; q = new Query (); try { q.parse (query); } catch (Exception e) { throw new QueryExecutionException ("Unable to create statement using WHERE clause: " + exp + " and class: " + c.getName () + " (gained from objects in list passed in)", e); } // Cache it. if (this.foreachQueryCache == null) { this.foreachQueryCache = new HashMap (); } this.foreachQueryCache.put (exp, q); } return q.execute (objs).getResults (); } public List foreach (Expression listFunction, Expression exp) throws QueryExecutionException { // Execute the list function. Object o = listFunction.getValue (this.q.getCurrentObject (), this.q); if (!(o instanceof List)) { throw new QueryExecutionException ("Expected expression: " + listFunction + " to return instance of: " + List.class.getName () + " but returned instance of: " + o.getClass ().getName ()); } List l = (List) o; return this.foreach (l, exp); } /** * Find objects from the List based upon the expression passed in. If * the expression evaluates to <code>true</code> then the object will * be returned. * Note: in accordance with the general operating methodology for the Query * object, the ":_allobjs" special bind variable will be set to the * the List passed in and the "_currobj" will be set to the relevant * object in the List. * * @param objs The List of objects to search. * @param exp The expression to evaulate against each object in the List. * @return The List of matching objects, if none match then an empty list is returned. * @throws QueryExecutionException If the expression cannot be evaulated against each * object. */ public List find (List objs, Expression exp) throws QueryExecutionException { // Get the current object, it's important that we leave the Query in the // same state at the end of this function as when we started! Object currobj = this.q.getCurrentObject (); List allobjs = (List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME); this.q.setAllObjects (objs); List r = new ArrayList (); int s = objs.size (); for (int i = 0; i < s; i++) { Object o = objs.get (i); this.q.setCurrentObject (o); try { if (exp.isTrue (o, this.q)) { r.add (o); } } catch (Exception e) { throw new QueryExecutionException ("Unable to evaulate expression: " + exp + " against object: " + i + " (class: " + o.getClass ().getName () + ")", e); } } // Restore the currobj and allobjs. this.q.setCurrentObject (currobj); this.q.setAllObjects (allobjs); return r; } /** * Group objects from the List based upon the expression passed in. The expression * is evaulated for each object, by calling: {@link Expression#getValue(Object,Query)} * and the return value used as the key to the Map. All objects with that value are * added to a List held against the key. To maintain the ordering of the keys (if * desirable) a {@link LinkedHashMap} is used as the return Map. * * Note: in accordance with the general operating methodology for the Query * object, the ":_allobjs" special bind variable will be set to the * the List passed in and the "_currobj" will be set to the relevant * object in the List. * * @param objs The List of objects to search. * @param exp The expression to evaulate against each object in the List. * @return The LinkedHashMap of matching objects, grouped according to the return value * of executing the expression against each object in the input List. * @throws QueryExecutionException If the expression cannot be evaulated against each * object. */ public Map grp (List objs, Expression exp) throws QueryExecutionException { // Get the current object, it's important that we leave the Query in the // same state at the end of this function as when we started! Object currobj = this.q.getCurrentObject (); List allobjs = (List) this.q.getVariable (Query.ALL_OBJS_VAR_NAME); this.q.setAllObjects (objs); Map r = new LinkedHashMap (); int s = objs.size (); for (int i = 0; i < s; i++) { Object o = objs.get (i); this.q.setCurrentObject (o); try { Object v = exp.getValue (o, this.q); List vs = (List) r.get (v); if (vs == null) { vs = new ArrayList (); r.put (v, vs); } vs.add (v); } catch (Exception e) { throw new QueryExecutionException ("Unable to evaulate expression: " + exp + " against object: " + i + " (class: " + o.getClass ().getName () + ")", e); } } // Restore the currobj and allobjs. this.q.setCurrentObject (currobj); this.q.setAllObjects (allobjs); return r; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -