📄 defaultgroovymethods.java
字号:
answer.add(object); } } return answer; } /** * Counts the number of occurencies of the given value inside this collection * * @param self the collection within which we count the number of occurencies * @param value the value * @return the number of occurrencies */ public static int count(Collection self, Object value) { int answer = 0; for (Iterator iter = self.iterator(); iter.hasNext();) { if (InvokerHelper.compareEqual(iter.next(), value)) { ++answer; } } return answer; } /** * Convert a collection to a List. * * @param self a collection * @return a List */ public static List toList(Collection self) { List answer = new ArrayList(self.size()); answer.addAll(self); return answer; } /** * Iterates through this object transforming each object into a new value using the closure * as a transformer, returning a list of transformed values. * * @param self the values of the object to map * @param closure the closure used to map each element of the collection * @return a List of the mapped values */ public static List collect(Object self, Closure closure) { return (List) collect(self, new ArrayList(), closure); } /** * Iterates through this object transforming each object into a new value using the closure * as a transformer and adding it to the collection, returning the resulting collection. * * @param self the values of the object to map * @param collection the Collection to which the mapped values are added * @param closure the closure used to map each element of the collection * @return the resultant collection */ public static Collection collect(Object self, Collection collection, Closure closure) { for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { collection.add(closure.call(iter.next())); } return collection; } /** * Iterates through this collection transforming each entry into a new value using the closure * as a transformer, returning a list of transformed values. * * @param self a collection * @param closure the closure used for mapping * @return a List of the mapped values */ public static List collect(Collection self, Closure closure) { return (List) collect(self, new ArrayList(self.size()), closure); } /** * Iterates through this collection transforming each entry into a new value using the closure * as a transformer, returning a list of transformed values. * * @param self a collection * @param collection the Collection to which the mapped values are added * @param closure the closure used to map each element of the collection * @return the resultant collection */ public static Collection collect(Collection self, Collection collection, Closure closure) { for (Iterator iter = self.iterator(); iter.hasNext();) { collection.add(closure.call(iter.next())); if (closure.getDirective() == Closure.DONE) { break; } } return collection; } /** * Iterates through this Map transforming each entry into a new value using the closure * as a transformer, returning a list of transformed values. * * @param self a Map * @param closure the closure used for mapping, which can be with one(Map.Entry) or two(key, value) parameters * @return a List of the mapped values */ public static Collection collect(Map self, Collection collection, Closure closure) { boolean isTwoParams = (closure.getParameterTypes().length == 2); for (Iterator iter = self.entrySet().iterator(); iter.hasNext();) { if (isTwoParams) { Map.Entry entry = (Map.Entry) iter.next(); collection.add(closure.call(new Object[]{entry.getKey(), entry.getValue()})); } else { collection.add(closure.call(iter.next())); } } return collection; } /** * Iterates through this Map transforming each entry into a new value using the closure * as a transformer, returning a list of transformed values. * * @param self a Map * @param closure the closure used to map each element of the collection * @return the resultant collection */ public static List collect(Map self, Closure closure) { return (List) collect(self, new ArrayList(self.size()), closure); } /** * Finds the first value matching the closure condition * * @param self an Object with an iterator returning its values * @param closure a closure condition * @return the first Object found */ public static Object find(Object self, Closure closure) { for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { Object value = iter.next(); if (InvokerHelper.asBool(closure.call(value))) { return value; } } return null; } /** * Finds the first value matching the closure condition * * @param self a Collection * @param closure a closure condition * @return the first Object found */ public static Object find(Collection self, Closure closure) { for (Iterator iter = self.iterator(); iter.hasNext();) { Object value = iter.next(); if (InvokerHelper.asBool(closure.call(value))) { return value; } } return null; } /** * Finds the first value matching the closure condition * * @param self a Map * @param closure a closure condition * @return the first Object found */ public static Object find(Map self, Closure closure) { for (Iterator iter = self.entrySet().iterator(); iter.hasNext();) { Object value = iter.next(); if (InvokerHelper.asBool(closure.call(value))) { return value; } } return null; } /** * Finds all values matching the closure condition * * @param self an Object with an Iterator returning its values * @param closure a closure condition * @return a List of the values found */ public static List findAll(Object self, Closure closure) { List answer = new ArrayList(); for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { Object value = iter.next(); if (InvokerHelper.asBool(closure.call(value))) { answer.add(value); } } return answer; } /** * Finds all values matching the closure condition * * @param self a Collection * @param closure a closure condition * @return a List of the values found */ public static List findAll(Collection self, Closure closure) { List answer = new ArrayList(self.size()); for (Iterator iter = self.iterator(); iter.hasNext();) { Object value = iter.next(); if (InvokerHelper.asBool(closure.call(value))) { answer.add(value); } } return answer; } /** * Finds all entries matching the closure condition. If the * closure takes one parameter then it will be passed the Map.Entry * otherwise if the closure takes two parameters then it will be * passed the key and the value. * * @param self a Map * @param closure a closure condition applying on the entries * @return a new subMap */ public static Map findAll(Map self, Closure closure) { Map answer = new HashMap(self.size()); for (Iterator iter = self.entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); if (InvokerHelper.asBool(callClosureForMapEntry(closure, entry))) { answer.put(entry.getKey(),entry.getValue()); } } return answer; } // internal helper method protected static Object callClosureForMapEntry(Closure closure, Map.Entry entry) { if (closure.getMaximumNumberOfParameters() == 2) { return closure.call(new Object[]{entry.getKey(), entry.getValue()}); } return closure.call(entry); } /** * Iterates through the given collection, passing in the initial value to * the closure along with the current iterated item then passing into the * next iteration the value of the previous closure. * * @param self a Collection * @param value a value * @param closure a closure * @return the last value of the last iteration */ public static Object inject(Collection self, Object value, Closure closure) { Object[] params = new Object[2]; for (Iterator iter = self.iterator(); iter.hasNext();) { Object item = iter.next(); params[0] = value; params[1] = item; value = closure.call(params); } return value; } /** * Iterates through the given array of objects, passing in the initial value to * the closure along with the current iterated item then passing into the * next iteration the value of the previous closure. * * @param self an Object[] * @param value a value * @param closure a closure * @return the last value of the last iteration */ public static Object inject(Object[] self, Object value, Closure closure) { Object[] params = new Object[2]; for (int i = 0; i < self.length; i++) { params[0] = value; params[1] = self[i]; value = closure.call(params); } return value; } /** * Sums a collection of numeric values. <code>coll.sum()</code> is equivalent to: * <code>coll.inject(0) {value, item -> value + item}</code>. * * @param self Collection of values to add together. * @return The sum of all of the list itmems. */ public static Object sum(Collection self) { Object result = null; if (self.size() == 0) return result; boolean isNumber = true; Class classref = null; try { classref = Class.forName("java.lang.Number"); } catch (Exception ex) { } for (Iterator iter = self.iterator(); iter.hasNext(); ) { if (!classref.isInstance(iter.next())) { isNumber = false; break; } } if (isNumber) { result = new Integer(0); } else { result = new String(); } Object[] param = new Object[1]; for (Iterator iter = self.iterator(); iter.hasNext();) { Object operand = iter.next(); param[0] = operand; MetaClass metaClass = InvokerHelper.getMetaClass(result); result = metaClass.invokeMethod(result, "plus", param); } return result; } /** * Sums the result of apply a closure to each item of a collection. * <code>coll.sum(closure)</code> is equivalent to: * <code>coll.collect(closure).sum()</code>. * * @param self a Collection * @param closure a single parameter closure that returns a numeric value. * @return The sum of the values returned by applying the closure to each * item of the list. */ public static Object sum(Collection self, Closure closure) { Object result = new Integer(0); Object[] closureParam = new Object[1]; Object[] plusParam = new Object[1]; for (Iterator iter = self.iterator(); iter.hasNext();) { Object item = iter.next(); closureParam[0] = item; plusParam[0] = closure.call(closureParam); MetaClass metaClass = InvokerHelper.getMetaClass(result); result = metaClass.invokeMethod(result, "plus", plusParam); } return result; } /** * Concatenates all of the items of the collection together with the given String as a separator * * @param self a Collection of objects * @param separator a String separator * @return the joined String */ public static String join(Collection self, String separator) { StringBuffer buffer = new StringBuffer(); boolean first = true; if (separator == null) separator = ""; for (Iterator iter = self.iterator(); iter.hasNext();) { Object value = iter.next(); if (first) { first = false; } else { buffer.append(separator); } buffer.append(InvokerHelper.toString(value)); } return buffer.toString(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -