defaultgroovymethods.java
来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· Java 代码 · 共 1,794 行 · 第 1/5 页
JAVA
1,794 行
* arguments.
* <p/>
* <p/>
* For examples, <pre>
* printf ( "Hello, %s!\n" , [ "world" ] as String[] )
* printf ( "Hello, %s!\n" , [ "Groovy" ])
* printf ( "%d + %d = %d\n" , [ 1 , 2 , 1+2 ] as Integer[] )
* printf ( "%d + %d = %d\n" , [ 3 , 3 , 3+3 ])
* <p/>
* ( 1..5 ).each { printf ( "-- %d\n" , [ it ] as Integer[] ) }
* ( 1..5 ).each { printf ( "-- %d\n" , [ it ] as int[] ) }
* ( 0x41..0x45 ).each { printf ( "-- %c\n" , [ it ] as char[] ) }
* ( 07..011 ).each { printf ( "-- %d\n" , [ it ] as byte[] ) }
* ( 7..11 ).each { printf ( "-- %d\n" , [ it ] as short[] ) }
* ( 7..11 ).each { printf ( "-- %d\n" , [ it ] as long[] ) }
* ( 7..11 ).each { printf ( "-- %5.2f\n" , [ it ] as float[] ) }
* ( 7..11 ).each { printf ( "-- %5.2g\n" , [ it ] as double[] ) }
* </pre>
* <p/>
*
* @param format A format string
* @param arg Argument which is referenced by the format specifiers in the format
* string. The type of <code>arg</code> should be one of Object[], List,
* int[], short[], byte[], char[], boolean[], long[], float[], or double[].
* @since JDK 1.5
*/
public static void printf(Object self, String format, Object arg) {
if (arg instanceof Object[]) {
printf(self, format, (Object[]) arg);
return;
} else if (arg instanceof List) {
printf(self, format, ((List) arg).toArray());
return;
} else if (!arg.getClass().isArray()) {
Object[] o = (Object[]) java.lang.reflect.Array.newInstance(arg.getClass(), 1);
o[0] = arg;
printf(self, format, o);
return;
}
Object[] ans = null;
String elemType = arg.getClass().getName();
if (elemType.equals("[I")) {
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;
}
}
// Collection based methods
//-------------------------------------------------------------------------
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;
}
/**
* A convenience method for making a collection unique using a closure as a comparator
* (by Michael Baehr)
*
* @param self a Collection
* @param closure a Closure used as a comparator
* @return self without any duplicates
*/
public static Collection unique(Collection self, Closure closure) {
if (self instanceof Set)
return self;
// use a comparator of one item or two
int params = closure.getMaximumNumberOfParameters();
if (params == 1) {
unique(self, new OrderBy(closure));
} else {
unique(self, new ClosureComparator(closure));
}
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 each 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 preserved.
* <p/>
* <code><pre>
* class Person {
* @Property fname, lname
* public String toString() {
* return fname + " " + lname
* }
* }
* <p/>
* 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)
* }
* <p/>
* public boolean equals(Object obj) {
* return this.equals(obj)
* }
* }
* <p/>
* 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")
* <p/>
* def list = [a, b, c, d]
* List list2 = list.unique(new PersonComparator())
* assert( list2 == list && list == [a, b, c] )
* <p/>
* </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 (!DefaultTypeTransformation.castToBoolean(closure.call(iter.next()))) {
return false;
}
}
return true;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?