📄 defaultgroovymethods.java
字号:
/** * Concatenates all of the elements of the array together with the given String as a separator * * @param self an array of Object * @param separator a String separator * @return the joined String */ public static String join(Object[] self, String separator) { StringBuffer buffer = new StringBuffer(); boolean first = true; if (separator == null) separator = ""; for (int i = 0; i < self.length; i++) { String value = InvokerHelper.toString(self[i]); if (first) { first = false; } else { buffer.append(separator); } buffer.append(value); } return buffer.toString(); } /** * Selects the maximum value found in the collection * * @param self a Collection * @return the maximum value */ public static Object max(Collection self) { Object answer = null; for (Iterator iter = self.iterator(); iter.hasNext();) { Object value = iter.next(); if (value != null) { if (answer == null || InvokerHelper.compareGreaterThan(value, answer)) { answer = value; } } } return answer; } /** * Selects the maximum value found in the collection using the given comparator * * @param self a Collection * @param comparator a Comparator * @return the maximum value */ public static Object max(Collection self, Comparator comparator) { Object answer = null; for (Iterator iter = self.iterator(); iter.hasNext();) { Object value = iter.next(); if (answer == null || comparator.compare(value, answer) > 0) { answer = value; } } return answer; } /** * Selects the minimum value found in the collection * * @param self a Collection * @return the minimum value */ public static Object min(Collection self) { Object answer = null; for (Iterator iter = self.iterator(); iter.hasNext();) { Object value = iter.next(); if (value != null) { if (answer == null || InvokerHelper.compareLessThan(value, answer)) { answer = value; } } } return answer; } /** * Selects the minimum value found in the collection using the given comparator * * @param self a Collection * @param comparator a Comparator * @return the minimum value */ public static Object min(Collection self, Comparator comparator) { Object answer = null; for (Iterator iter = self.iterator(); iter.hasNext();) { Object value = iter.next(); if (answer == null || comparator.compare(value, answer) < 0) { answer = value; } } return answer; } /** * Selects the minimum value found in the collection using the given closure as a comparator * * @param self a Collection * @param closure a closure used as a comparator * @return the minimum value */ public static Object min(Collection self, Closure closure) { int params = closure.getMaximumNumberOfParameters(); if (params == 1) { Object answer = null; Object answer_value = null; for (Iterator iter = self.iterator(); iter.hasNext();) { Object item = iter.next(); Object value = closure.call(item); if (answer == null || InvokerHelper.compareLessThan(value, answer_value)) { answer = item; answer_value = value; } } return answer; } else { return min(self, new ClosureComparator(closure)); } } /** * Selects the maximum value found in the collection using the given closure as a comparator * * @param self a Collection * @param closure a closure used as a comparator * @return the maximum value */ public static Object max(Collection self, Closure closure) { int params = closure.getMaximumNumberOfParameters(); if (params == 1) { Object answer = null; Object answer_value = null; for (Iterator iter = self.iterator(); iter.hasNext();) { Object item = iter.next(); Object value = closure.call(item); if (answer == null || InvokerHelper.compareLessThan(answer_value, value)) { answer = item; answer_value = value; } } return answer; } else { return max(self, new ClosureComparator(closure)); } } /** * Makes a String look like a Collection by adding support for the size() method * * @param text a String * @return the length of the String */ public static int size(String text) { return text.length(); } /** * Provide standard Groovy size() method for StringBuffers * * @param buffer a StringBuffer * @return the length of the StringBuffer */ public static int size(StringBuffer buffer) { return buffer.length(); } /** * Provide the standard Groovy size method */ public static long size(File file){ return file.length(); } /** * Provide the standard Groovy size method */ public static long size(Matcher matcher){ return getCount(matcher); } /** * Makes an Array look like a Collection by adding support for the size() method * * @param self an Array of Object * @return the size of the Array */ public static int size(Object[] self) { return self.length; } /** * Support the subscript operator for String. * * @param text a String * @param index the index of the Character to get * @return the Character at the given index */ public static CharSequence getAt(CharSequence text, int index) { index = normaliseIndex(index, text.length()); return text.subSequence(index, index + 1); } /** * Support the subscript operator for String * * @param text a String * @return the Character object at the given index */ public static String getAt(String text, int index) { index = normaliseIndex(index, text.length()); return text.substring(index, index + 1); } /** * Support the range subscript operator for CharSequence * * @param text a CharSequence * @param range a Range * @return the subsequence CharSequence */ public static CharSequence getAt(CharSequence text, Range range) { int from = normaliseIndex(InvokerHelper.asInt(range.getFrom()), text.length()); int to = normaliseIndex(InvokerHelper.asInt(range.getTo()), text.length()); // If this is a backwards range, reverse the arguments to substring. if (from > to) { int tmp = from; from = to; to = tmp; } return text.subSequence(from, to + 1); } /** * Support the range subscript operator for CharSequence or StringBuffer with IntRange * * @param text a CharSequence * @param range an IntRange * @return the subsequence CharSequence */ public static CharSequence getAt(CharSequence text, IntRange range) { return getAt(text, (Range) range); } /** * Support the range subscript operator for String with IntRange * * @param text a String * @param range an IntRange * @return the resulting String */ public static String getAt(String text, IntRange range) { return getAt(text,(Range)range); } /** * Support the range subscript operator for String * * @param text a String * @param range a Range * @return a substring corresponding to the Range */ public static String getAt(String text, Range range) { int from = normaliseIndex(InvokerHelper.asInt(range.getFrom()), text.length()); int to = normaliseIndex(InvokerHelper.asInt(range.getTo()), text.length()); // If this is a backwards range, reverse the arguments to substring. boolean reverse = range.isReverse(); if (from > to) { int tmp = to; to = from; from = tmp; reverse = !reverse; } String answer = text.substring(from, to + 1); if (reverse) { answer = reverse(answer); } return answer; } /** * Creates a new string which is the reverse (backwards) of this string * * @param self a String * @return a new string with all the characters reversed. */ public static String reverse(String self) { int size = self.length(); StringBuffer buffer = new StringBuffer(size); for (int i = size - 1; i >= 0; i--) { buffer.append(self.charAt(i)); } return buffer.toString(); } /** * Transforms a String representing a URL into a URL object. * * @param self the String representing a URL * @return a URL * @throws MalformedURLException is thrown if the URL is not well formed. */ public static URL toURL(String self) throws MalformedURLException { return new URL(self); } /** * Transforms a String representing a URI into a URI object. * * @param self the String representing a URI * @return a URI * @throws URISyntaxException * @throws URISyntaxException is thrown if the URI is not well formed. */ public static URI toURI(String self) throws URISyntaxException { return new URI(self); } /** * Turns a String into a regular expression pattern * * @param self a String to convert into a regular expression * @return the regular expression pattern */ public static Pattern negate(String self) { return InvokerHelper.regexPattern(self); } /** * Replaces all occurrencies of a captured group by the result of a closure on that text. * * <p> For examples, * <pre> * assert "FOOBAR-FOOBAR-" == "foobar-FooBar-".replaceAll("(([fF][oO]{2})[bB]ar)", { Object[] it -> it[0].toUpperCase() }) * * Here, * it[0] is the global string of the matched group * it[1] is the first string in the matched group * it[2] is the second string in the matched group * * * assert "FOO-FOO-" == "foobar-FooBar-".replaceAll("(([fF][oO]{2})[bB]ar)", { x, y, z -> z.toUpperCase() }) * * Here, * x is the global string of the matched group * y is the first string in the matched group * z is the second string in the matched group * </pre> * * @param self a String * @param regex the capturing regex * @param closure the closure to apply on each captured group * @return a String with replaced content */ public static String replaceAll(String self, String regex, Closure closure) { Matcher matcher = Pattern.compile(regex).matcher(self); if (matcher.find()) { matcher.reset(); StringBuffer sb = new StringBuffer(); while (matcher.find()) { int count = matcher.groupCount(); ArrayList groups = new ArrayList(); for (int i = 0; i <= count; i++) { groups.add(matcher.group(i)); } matcher.appendReplacement(sb, String.valueOf(closure.call((Object[]) groups.toArray() )));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -