📄 defaultgroovymethods.java
字号:
int[] ia = (int[]) arg; ans = new Integer[ia.length]; for (int i = 0; i < ia.length; i++) { ans[i] = new Integer(ia[i]); } } else if (elemType.equals("[C")) { char[] ia = (char[]) arg; ans = new Character[ia.length]; for (int i = 0; i < ia.length; i++) { ans[i] = new Character(ia[i]); } } else if (elemType.equals("[Z")) { boolean[] ia = (boolean[]) arg; ans = new Boolean[ia.length]; for (int i = 0; i < ia.length; i++) { ans[i] = new Boolean(ia[i]); } } else if (elemType.equals("[B")) { byte[] ia = (byte[]) arg; ans = new Byte[ia.length]; for (int i = 0; i < ia.length; i++) { ans[i] = new Byte(ia[i]); } } else if (elemType.equals("[S")) { short[] ia = (short[]) arg; ans = new Short[ia.length]; for (int i = 0; i < ia.length; i++) { ans[i] = new Short(ia[i]); } } else if (elemType.equals("[F")) { float[] ia = (float[]) arg; ans = new Float[ia.length]; for (int i = 0; i < ia.length; i++) { ans[i] = new Float(ia[i]); } } else if (elemType.equals("[J")) { long[] ia = (long[]) arg; ans = new Long[ia.length]; for (int i = 0; i < ia.length; i++) { ans[i] = new Long(ia[i]); } } else if (elemType.equals("[D")) { double[] ia = (double[]) arg; ans = new Double[ia.length]; for (int i = 0; i < ia.length; i++) { ans[i] = new Double(ia[i]); } } else { throw new RuntimeException("printf(String," + arg + ")"); } printf(self, format, (Object[]) ans); } /** * @return a String that matches what would be typed into a terminal to * create this object. e.g. [1, 'hello'].inspect() -> [1, "hello"] */ public static String inspect(Object self) { return InvokerHelper.inspect(self); } /** * Print to a console in interactive format */ public static void print(Object self, PrintWriter out) { if (out == null) { out = new PrintWriter(System.out); } out.print(InvokerHelper.toString(self)); } /** * Print to a console in interactive format * * @param out the PrintWriter used for printing */ public static void println(Object self, PrintWriter out) { if (out == null) { out = new PrintWriter(System.out); } InvokerHelper.invokeMethod(self, "print", out); out.println(); } /** * Provide a dynamic method invocation method which can be overloaded in * classes to implement dynamic proxies easily. */ public static Object invokeMethod(Object object, String method, Object arguments) { return InvokerHelper.invokeMethod(object, method, arguments); } // isCase methods //------------------------------------------------------------------------- public static boolean isCase(Object caseValue, Object switchValue) { return caseValue.equals(switchValue); } public static boolean isCase(String caseValue, Object switchValue) { if (switchValue == null) { return caseValue == null; } return caseValue.equals(switchValue.toString()); } public static boolean isCase(Class caseValue, Object switchValue) { if (switchValue instanceof Class) { Class val = (Class) switchValue; return caseValue.isAssignableFrom(val); } return caseValue.isInstance(switchValue); } public static boolean isCase(Collection caseValue, Object switchValue) { return caseValue.contains(switchValue); } public static boolean isCase(Pattern caseValue, Object switchValue) { if (switchValue == null) { return caseValue == null; } final Matcher matcher = caseValue.matcher(switchValue.toString()); if (matcher.matches()) { RegexSupport.setLastMatcher(matcher); return true; } else { return false; } } private static Object packArray(Object object) { if (object instanceof Object[]) return new Object[] {object}; else return object; } // Collection based methods //------------------------------------------------------------------------- /** * Remove all duplicates from a given Collection. * Works on the receiver object and returns it. * For each duplicate, only the first member which is returned * by the given Collection's iterator is retained, but all other ones are removed. * The given Collection's original order is retained. * If there exists numbers in the Collection, then they are compared * as numbers, that is, 2, 2.0, 3L, (short)4 are comparable. * * <code><pre> * def x = [2, 2.0, 3L, 1.0, (short)4, 1] * def y = x.unique() * assert( y == x && x == [2, 3L, 1.0, (short)4] ) * </pre></code> * * @param self * @return self without duplicates */ /* public static Collection unique(Collection self){ if (self instanceof Set) return self; if (self.size() == new HashSet(self).size()) return self; Collection seen = new HashSet(self.size()); for (Iterator iter = self.iterator(); iter.hasNext();) { Object o = iter.next(); if (seen.contains(o)){ iter.remove(); } else { seen.add(o); } } return self; } */ public static Collection unique(Collection self) { if (self instanceof Set) return self; List answer = new ArrayList(); NumberComparator comparator = new NumberComparator(); for (Iterator it = self.iterator(); it.hasNext();) { Object o = it.next(); boolean duplicated = false; for (Iterator it2 = answer.iterator(); it2.hasNext();) { Object o2 = it2.next(); if (comparator.compare(o, o2) == 0) { duplicated = true; break; } } if (!duplicated) answer.add(o); } self.clear(); self.addAll(answer); return self; } /** * Remove all duplicates from a given Collection. * Works on the receiver object and returns it. * The order of members in the Collection are compared by the given Comparator. * For eachy duplicate, the first member which is returned * by the given Collection's iterator is retained, but all other ones are removed. * The given Collection's original order is retained. * * <code><pre> * class Person { * @Property fname, lname * public String toString() { * return fname + " " + lname * } * } * * class PersonComparator implements Comparator { * public int compare(Object o1, Object o2) { * Person p1 = (Person) o1 * Person p2 = (Person) o2 * if (p1.lname != p2.lname) * return p1.lname.compareTo(p2.lname) * else * return p1.fname.compareTo(p2.fname) * } * * public boolean equals(Object obj) { * return this.equals(obj) * } * } * * Person a = new Person(fname:"John", lname:"Taylor") * Person b = new Person(fname:"Clark", lname:"Taylor") * Person c = new Person(fname:"Tom", lname:"Cruz") * Person d = new Person(fname:"Clark", lname:"Taylor") * * def list = [a, b, c, d] * List list2 = list.unique(new PersonComparator()) * assert( list2 == list && list == [a, b, c] ) * * </pre></code> * * @param self a Collection * @param comparator a Comparator. * @return self without duplicates */ public static Collection unique(Collection self, Comparator comparator) { if (self instanceof Set) return self; List answer = new ArrayList(); for (Iterator it = self.iterator(); it.hasNext();) { Object o = it.next(); boolean duplicated = false; for (Iterator it2 = answer.iterator(); it2.hasNext();) { Object o2 = it2.next(); if (comparator.compare(o, o2) == 0) { duplicated = true; break; } } if (!duplicated) answer.add(o); } self.clear(); self.addAll(answer); return self; } /** * Allows objects to be iterated through using a closure * * @param self the object over which we iterate * @param closure the closure applied on each element found */ public static void each(Object self, Closure closure) { for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { closure.call(iter.next()); } } /** * Allows object to be iterated through a closure with a counter * * @param self an Object * @param closure a Closure */ public static void eachWithIndex(Object self, Closure closure) { int counter = 0; for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { closure.call(new Object[]{iter.next(), new Integer(counter++)}); } } /** * Allows objects to be iterated through using a closure * * @param self the collection over which we iterate * @param closure the closure applied on each element of the collection */ public static void each(Collection self, Closure closure) { for (Iterator iter = self.iterator(); iter.hasNext();) { closure.call(iter.next()); } } /** * Allows a Map to be iterated through using a closure. 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 the map over which we iterate * @param closure the closure applied on each entry of the map */ public static void each(Map self, Closure closure) { for (Iterator iter = self.entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); callClosureForMapEntry(closure, entry); } } /** * Iterates over every element of a collection, and check whether a predicate is valid for all elements. * * @param self the object over which we iterate * @param closure the closure predicate used for matching * @return true if every item in the collection matches the closure * predicate */ public static boolean every(Object self, Closure closure) { for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { if (!InvokerHelper.asBool(closure.call(iter.next()))) { return false; } } return true; } /** * Iterates over every element of a collection, and check whether a predicate is valid for at least one element * * @param self the object over which we iterate * @param closure the closure predicate used for matching * @return true if any item in the collection matches the closure predicate */ public static boolean any(Object self, Closure closure) { for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { if (InvokerHelper.asBool(closure.call(iter.next()))) { return true; } } return false; } /** * Iterates over every element of the collection and return each object that matches * the given filter - calling the isCase() method used by switch statements. * This method can be used with different kinds of filters like regular expresions, classes, ranges etc. * * @param self the object over which we iterate * @param filter the filter to perform on the collection (using the isCase(object) method) * @return a list of objects which match the filter */ public static List grep(Object self, Object filter) { List answer = new ArrayList(); MetaClass metaClass = InvokerHelper.getMetaClass(filter); for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { Object object = iter.next(); if (InvokerHelper.asBool(metaClass.invokeMethod(filter, "isCase", object))) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -