⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultgroovymethods.java

📁 大名鼎鼎的java动态脚本语言。已经通过了sun的认证
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            }            matcher.appendTail(sb);            return sb.toString();        } else {            return self;        }    }    private static String getPadding(String padding, int length) {        if (padding.length() < length) {            return multiply(padding, new Integer(length / padding.length() + 1)).substring(0, length);        } else {            return padding.substring(0, length);        }    }    /**     * Pad a String with the characters appended to the left     *     * @param numberOfChars the total number of characters     * @param padding       the charaters used for padding     * @return the String padded to the left     */    public static String padLeft(String self, Number numberOfChars, String padding) {        int numChars = numberOfChars.intValue();        if (numChars <= self.length()) {            return self;        } else {            return getPadding(padding, numChars - self.length()) + self;        }    }    /**     * Pad a String with the spaces appended to the left     *     * @param numberOfChars the total number of characters     * @return the String padded to the left     */    public static String padLeft(String self, Number numberOfChars) {        return padLeft(self, numberOfChars, " ");    }    /**     * Pad a String with the characters appended to the right     *     * @param numberOfChars the total number of characters     * @param padding       the charaters used for padding     * @return the String padded to the right     */    public static String padRight(String self, Number numberOfChars, String padding) {        int numChars = numberOfChars.intValue();        if (numChars <= self.length()) {            return self;        } else {            return self + getPadding(padding, numChars - self.length());        }    }    /**     * Pad a String with the spaces appended to the right     *     * @param numberOfChars the total number of characters     * @return the String padded to the right     */    public static String padRight(String self, Number numberOfChars) {        return padRight(self, numberOfChars, " ");    }    /**     * Center a String and padd it with the characters appended around it     *     * @param numberOfChars the total number of characters     * @param padding       the charaters used for padding     * @return the String centered with padded character around     */    public static String center(String self, Number numberOfChars, String padding) {        int numChars = numberOfChars.intValue();        if (numChars <= self.length()) {            return self;        } else {            int charsToAdd = numChars - self.length();            String semiPad = charsToAdd % 2 == 1 ?                    getPadding(padding, charsToAdd / 2 + 1) :                    getPadding(padding, charsToAdd / 2);            if (charsToAdd % 2 == 0)                return semiPad + self + semiPad;            else                return semiPad.substring(0, charsToAdd / 2) + self + semiPad;        }    }    /**     * Center a String and padd it with spaces appended around it     *     * @param numberOfChars the total number of characters     * @return the String centered with padded character around     */    public static String center(String self, Number numberOfChars) {        return center(self, numberOfChars, " ");    }    /**     * Support the subscript operator, e.g. matcher[index], for a regex Matcher.     *     * For an example using no group match, <code><pre>     *    def p = /ab[d|f]/     *    def m = "abcabdabeabf" =~ p     *    for (i in 0..<m.count) {     *        println( "m.groupCount() = " + m.groupCount())     *        println( "  " + i + ": " + m[i] )   // m[i] is a String     *    }     * </pre></code>     *     * For an example using group matches, <code><pre>     *    def p = /(?:ab([c|d|e|f]))/     *    def m = "abcabdabeabf" =~ p     *    for (i in 0..<m.count) {     *        println( "m.groupCount() = " + m.groupCount())     *        println( "  " + i + ": " + m[i] )   // m[i] is a List     *    }     * </pre></code>     *     * For another example using group matches, <code><pre>     *    def m = "abcabdabeabfabxyzabx" =~ /(?:ab([d|x-z]+))/     *    m.count.times {     *        println( "m.groupCount() = " + m.groupCount())     *        println( "  " + it + ": " + m[it] )   // m[it] is a List     *    }     * </pre></code>     *     * @param matcher a Matcher     * @param idx     an index     * @return object a matched String if no groups matched, list of matched groups otherwise.     */    public static Object getAt(Matcher matcher, int idx) {        try {            int count = getCount(matcher);            if (idx < -count || idx >= count) {                throw new IndexOutOfBoundsException("index is out of range " + (-count) + ".." + (count - 1) + " (index = " + idx + ")");            }            idx = normaliseIndex(idx, count);            matcher.reset();            for (int i = 0; i <= idx; i++) {                matcher.find();            }            if (hasGroup(matcher)) {                // are we using groups?                // yes, so return the specified group as list                ArrayList list = new ArrayList(matcher.groupCount());                for (int i = 0; i <= matcher.groupCount(); i++) {                    list.add(matcher.group(i));                }                return list;            } else {                // not using groups, so return the nth                // occurrence of the pattern                return matcher.group();            }        }        catch (IllegalStateException ex) {            return null;        }    }    /**     * Set the position of the given Matcher to the given index.     *     * @param matcher a Matcher     * @param idx the index number     */    public static void setIndex(Matcher matcher, int idx) {        int count = getCount(matcher);        if (idx < -count || idx >= count) {            throw new IndexOutOfBoundsException("index is out of range " + (-count) + ".." + (count - 1) + " (index = " + idx + ")");        }        if (idx == 0) {            matcher.reset();        }        else if (idx > 0) {            matcher.reset();            for (int i = 0; i < idx; i++) {                matcher.find();            }        }        else if (idx < 0) {            matcher.reset();            idx += getCount(matcher);            for (int i = 0; i < idx; i++) {                matcher.find();            }        }    }    /**     * Find the number of Strings matched to the given Matcher.     *     * @param matcher a Matcher     * @return int  the number of Strings matched to the given matcher.     */    public static int getCount(Matcher matcher) {        int counter = 0;        matcher.reset();        while (matcher.find()) {            counter++;        }        matcher.reset();        return counter;    }    /**     * Check whether a Matcher contains a group or not.     *     * @param matcher a Matcher     * @return boolean  <code>true</code> if matcher contains at least one group.     */    public static boolean hasGroup(Matcher matcher) {        return matcher.groupCount() > 0;    }    /**     * Support the range subscript operator for a List     *     * @param self  a List     * @param range a Range     * @return a sublist based on range borders or a new list if range is reversed     * @see java.util.List#subList(int, int)     */    public static List getAt(List self, IntRange range) {        RangeInfo info = subListBorders(self.size(), range);        List answer = self.subList(info.from, info.to);  // sublist is always exclusive, but Ranges are not        if (info.reverse) {            answer = reverse(answer);        }        return answer;    }    // helper method for getAt and putAt    protected static RangeInfo subListBorders(int size, IntRange range){        int from = normaliseIndex(InvokerHelper.asInt(range.getFrom()), size);        int to = normaliseIndex(InvokerHelper.asInt(range.getTo()), size);        boolean reverse = range.isReverse();        if (from > to) {                        // support list[1..-1]            int tmp = to;            to = from;            from = tmp;            reverse = !reverse;        }        return new RangeInfo(from, to+1, reverse);    }    // helper method for getAt and putAt    protected static RangeInfo subListBorders(int size, EmptyRange range){        int from = normaliseIndex(InvokerHelper.asInt(range.getFrom()), size);        return new RangeInfo(from, from, false);    }    /**     * Allows a List to be used as the indices to be used on a List     *     * @param self    a List     * @param indices a Collection of indices     * @return a new list of the values at the given indices     */    public static List getAt(List self, Collection indices) {        List answer = new ArrayList(indices.size());        for (Iterator iter = indices.iterator(); iter.hasNext();) {            Object value = iter.next();            if (value instanceof Range) {                answer.addAll(getAt(self, (Range) value));            } else if (value instanceof List) {                answer.addAll(getAt(self, (List) value));            } else {                int idx = InvokerHelper.asInt(value);                answer.add(getAt(self, idx));            }        }        return answer;    }    /**     * Allows a List to be used as the indices to be used on a List     *     * @param self    an Array of Objects     * @param indices a Collection of indices     * @return a new list of the values at the given indices     */    public static List getAt(Object[] self, Collection indices) {        List answer = new ArrayList(indices.size());        for (Iterator iter = indices.iterator(); iter.hasNext();) {            Object value = iter.next();            if (value instanceof Range) {                answer.addAll(getAt(self, (Range) value));            } else if (value instanceof Collection) {                answer.addAll(getAt(self, (Collection) value));            } else {                int idx = InvokerHelper.asInt(value);                answer.add(getAt(self, idx));            }        }        return answer;    }    /**     * Allows a List to be used as the indices to be used on a CharSequence     *     * @param self    a CharSequence     * @param indices a Collection of indices     * @return a String of the values at the given indices     */    public static CharSequence getAt(CharSequence self, Collection indices) {        StringBuffer answer = new StringBuffer();        for (Iterator iter = indices.iterator(); iter.hasNext();) {            Object value = iter.next();            if (value instanceof Range) {                answer.append(getAt(self, (Range) value));            } else if (value instanceof Collection) {                answer.append(getAt(self, (Collection) value));            } else {                int idx = InvokerHelper.asInt(value);                answer.append(getAt(self, idx));            }        }        return answer.toString();    }    /**     * Allows a List to be used as the indices to be used on a String     *     * @param self    a String     * @param indices a Collection of indices     * @return a String of the values at the given indices     */    public static String getAt(String self, Collection indices) {        return (String) getAt((CharSequence) self, indices);    }    /**     * Allows a List to be used as the indices to be used on a Matcher     *     * @param self    a Matcher     * @param indices a Collection of indices     * @return a String of the values at the given indices     */    public static String getAt(Matcher self, Collection indices) {        StringBuffer answer = new StringBuffer();        for (Iterator iter = indices.iterator(); iter.hasNext();) {            Object value = iter.next();            if (value instanceof Range) {                answer.append(getAt(self, (Range) value));            } else if (value instanceof Collection) {                answer.append(getAt(self, (Collection) value));            } else {                int idx = InvokerHelper.asInt(value);                answer.append(getAt(self, idx));            }        }        return answer.toString();    }    /**     * Creates a sub-Map containing the given keys. This method is similar to     * List.subList() but uses keys rather than index ranges.     *     * @param map  a Map     * @param keys a Collection of keys     * @return a new Map containing the given keys     */    public static Map subMap(Map map, Collection keys) {        Map answer = new HashM

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -