Java 类groovy.lang.IntRange 实例源码

项目:yajsw    文件:DefaultGroovyMethodsSupport.java   
protected static RangeInfo subListBorders(int size, Range range) {
    if (range instanceof IntRange) {
        return ((IntRange)range).subListBorders(size);
    }
    int from = normaliseIndex(DefaultTypeTransformation.intUnbox(range.getFrom()), size);
    int to = normaliseIndex(DefaultTypeTransformation.intUnbox(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);
}
项目:groovy    文件:ScriptBytecodeAdapter.java   
public static List createRange(Object from, Object to, boolean inclusive) throws Throwable {
    if (from instanceof Integer && to instanceof Integer) {
        int ifrom = (Integer) from;
        int ito = (Integer) to;
        if (inclusive || ifrom != ito) {
            return new IntRange(inclusive, ifrom, ito);
        } // else fall through for EmptyRange
    }
    if (!inclusive && compareEqual(from, to)) {
        return new EmptyRange((Comparable) from);
    }
    if (from instanceof Number && to instanceof Number) {
        return new NumberRange(comparableNumber((Number) from), comparableNumber((Number) to), inclusive);
    }
    if (!inclusive) {
        if (compareGreaterThan(from, to)) {
            to = invokeMethod0(ScriptBytecodeAdapter.class, to, "next");
        } else {
            to = invokeMethod0(ScriptBytecodeAdapter.class, to, "previous");
        }
    }

    return new ObjectRange((Comparable) from, (Comparable) to);
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Support retrieving a subset of a BitSet using a Range
 *
 * @param self  a BitSet
 * @param range a Range defining the desired subset
 * @return a new BitSet that represents the requested subset
 * @see java.util.BitSet
 * @see groovy.lang.IntRange
 * @since 1.5.0
 */
public static BitSet getAt(BitSet self, IntRange range) {
    RangeInfo info = subListBorders(self.length(), range);
    BitSet result = new BitSet();

    int numberOfBits = info.to - info.from;
    int adjuster = 1;
    int offset = info.from;

    if (info.reverse) {
        adjuster = -1;
        offset = info.to - 1;
    }

    for (int i = 0; i < numberOfBits; i++) {
        result.set(i, self.get(offset + (adjuster * i)));
    }

    return result;
}
项目:groovy    文件:DefaultGroovyMethodsSupport.java   
protected static RangeInfo subListBorders(int size, Range range) {
    if (range instanceof IntRange) {
        return ((IntRange)range).subListBorders(size);
    }
    int from = normaliseIndex(DefaultTypeTransformation.intUnbox(range.getFrom()), size);
    int to = normaliseIndex(DefaultTypeTransformation.intUnbox(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);
}
项目:groovy    文件:DOMCategory.java   
private static NodeList nodesGetAt(Object o, IntRange r) {
    if (o instanceof Element) {
        NodeList n = xgetAt((Element)o, r);
        if (n != null) return n;
    }
    if (o instanceof NodeList) {
        return xgetAt((NodeList)o, r);
    }
    return null;
}
项目:groovy    文件:DOMCategory.java   
private static NodeList xgetAt(Element element, IntRange r) {
    if (hasChildElements(element, "*")) {
        NodeList nodeList = getChildElements(element, "*");
        return xgetAt(nodeList, r);
    }
    return null;
}
项目:groovy    文件:DOMCategory.java   
private static NodeList xgetAt(NodeList nodeList, IntRange r) {
    int from = r.getFromInt();
    int to = r.getToInt();

    // If the range is of size 1, then we can use the existing
    // xgetAt() that takes an integer index.
    if (from == to) return new NodesHolder(Collections.singletonList(xgetAt(nodeList, from)));

    // Normalise negative indices.
    if (from < 0) from = from + nodeList.getLength();
    if (to < 0) to = to + nodeList.getLength();

    // After normalisation, 'from' may be greater than 'to'. In that
    // case, we need to reverse them and make sure the range's 'reverse'
    // property is correct.
    // TODO We should probably use DefaultGroovyMethodsSupport.subListBorders(),
    // but that's protected and unavailable to us.
    if (from > to) {
        r = r.isReverse() ? new IntRange(to, from) : new IntRange(from, to);
        from = r.getFromInt();
        to = r.getToInt();
    }

    // Copy the required nodes into a new list.
    List<Node> nodes = new ArrayList<Node>(to - from + 1);
    if (r.isReverse()) {
        for (int i = to; i >= from; i--) nodes.add(nodeList.item(i));
    }
    else {
        for (int i = from; i <= to; i++) nodes.add(nodeList.item(i));
    }
    return new NodesHolder(nodes);
}
项目:groovy    文件:StaticTypeCheckingVisitor.java   
@Override
public void visitRangeExpression(final RangeExpression expression) {
    super.visitRangeExpression(expression);
    ClassNode fromType = getWrapper(getType(expression.getFrom()));
    ClassNode toType = getWrapper(getType(expression.getTo()));
    if (Integer_TYPE.equals(fromType) && Integer_TYPE.equals(toType)) {
        storeType(expression, ClassHelper.make(IntRange.class));
    } else {
        ClassNode rangeType = ClassHelper.make(Range.class).getPlainNodeReference();
        rangeType.setGenericsTypes(new GenericsType[] { new GenericsType(WideningCategories.lowestUpperBound(fromType, toType))});
        storeType(expression,  rangeType);
    }
}
项目:groovy    文件:InvokeMethodTest.java   
public void testListGetWithRange() throws Throwable {
    List list = Arrays.asList(new Object[]{"a", "b", "c"});
    Object range = new IntRange(true, 0, 2);
    Object value = invoke(list, "getAt", range);
    assertTrue("Returned List: " + value, value instanceof List);
    List retList = (List) value;
    assertEquals("List size", 3, retList.size());
}
项目:eHMP    文件:NextPageLinkGeneratorTests.java   
@Test
public void testSupports() {
    Assert.assertTrue(generator.supports(JsonCCollection.create(createMockPage(0, 20, 42))));
    Assert.assertTrue(generator.supports(JsonCCollection.create(createMockPage(20, 20, 42))));
    Assert.assertFalse(generator.supports(JsonCCollection.create(createMockPage(40, 20, 42))));
    Assert.assertFalse(generator.supports(new IntRange(1, 10)));
}
项目:eHMP    文件:PreviousPageLinkGeneratorTests.java   
@Test
public void testSupports() {
    Assert.assertFalse(generator.supports(JsonCCollection.create(createMockPage(0, 20, 42))));
    Assert.assertTrue(generator.supports(JsonCCollection.create(createMockPage(20, 20, 42))));
    Assert.assertTrue(generator.supports(JsonCCollection.create(createMockPage(40, 20, 42))));
    Assert.assertFalse(generator.supports(new IntRange(1, 10)));
}
项目:groovy    文件:DOMCategory.java   
public static NodeList getAt(Node o, IntRange r) {
    return nodesGetAt(o, r);
}
项目:groovy    文件:DOMCategory.java   
public static NodeList getAt(NodeListsHolder o, IntRange r) {
    return nodesGetAt(o, r);
}
项目:groovy    文件:DOMCategory.java   
public static NodeList getAt(NodesHolder o, IntRange r) {
    return nodesGetAt(o, r);
}
项目:groovy    文件:DefaultGroovyMethods.java   
@Deprecated
public static CharSequence getAt(CharSequence text, IntRange range) {
    return StringGroovyMethods.getAt(text, range);
}
项目:groovy    文件:DefaultGroovyMethods.java   
@Deprecated
public static String getAt(String text, IntRange range) {
    return StringGroovyMethods.getAt(text, range);
}
项目:groovy    文件:DefaultGroovyMethods.java   
@Deprecated
public static void putAt(StringBuffer self, IntRange range, Object value) {
    StringGroovyMethods.putAt(self, range, value);
}
项目:eHMP    文件:NextPageLinkGeneratorTests.java   
private Page createMockPage(int startIndex, int itemsPerPage, int total) {
    return new PageImpl(new IntRange(startIndex, (startIndex + Math.min(itemsPerPage - 1, total - startIndex))), new PageRequest((int) (startIndex / itemsPerPage), itemsPerPage), total);
}
项目:eHMP    文件:PreviousPageLinkGeneratorTests.java   
private Page createMockPage(int startIndex, int itemsPerPage, int total) {
    return new PageImpl(new IntRange(startIndex, (startIndex + Math.min(itemsPerPage - 1, total - startIndex))), new PageRequest((int) (startIndex / itemsPerPage), itemsPerPage), total);
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Support the subscript operator with an IntRange for a byte array
 *
 * @param array a byte array
 * @param range an IntRange indicating the indices for the items to retrieve
 * @return list of the retrieved bytes
 * @since 1.0
 */
@SuppressWarnings("unchecked")
public static List<Byte> getAt(byte[] array, IntRange range) {
    RangeInfo info = subListBorders(array.length, range);
    List<Byte> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
    return info.reverse ? reverse(answer) : answer;
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Support the subscript operator with an IntRange for a char array
 *
 * @param array a char array
 * @param range an IntRange indicating the indices for the items to retrieve
 * @return list of the retrieved chars
 * @since 1.0
 */
@SuppressWarnings("unchecked")
public static List<Character> getAt(char[] array, IntRange range) {
    RangeInfo info = subListBorders(array.length, range);
    List<Character> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
    return info.reverse ? reverse(answer) : answer;
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Support the subscript operator with an IntRange for a short array
 *
 * @param array a short array
 * @param range an IntRange indicating the indices for the items to retrieve
 * @return list of the retrieved shorts
 * @since 1.0
 */
@SuppressWarnings("unchecked")
public static List<Short> getAt(short[] array, IntRange range) {
    RangeInfo info = subListBorders(array.length, range);
    List<Short> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
    return info.reverse ? reverse(answer) : answer;
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Support the subscript operator with an IntRange for an int array
 *
 * @param array an int array
 * @param range an IntRange indicating the indices for the items to retrieve
 * @return list of the retrieved ints
 * @since 1.0
 */
@SuppressWarnings("unchecked")
public static List<Integer> getAt(int[] array, IntRange range) {
    RangeInfo info = subListBorders(array.length, range);
    List<Integer> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
    return info.reverse ? reverse(answer) : answer;
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Support the subscript operator with an IntRange for a long array
 *
 * @param array a long array
 * @param range an IntRange indicating the indices for the items to retrieve
 * @return list of the retrieved longs
 * @since 1.0
 */
@SuppressWarnings("unchecked")
public static List<Long> getAt(long[] array, IntRange range) {
    RangeInfo info = subListBorders(array.length, range);
    List<Long> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
    return info.reverse ? reverse(answer) : answer;
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Support the subscript operator with an IntRange for a float array
 *
 * @param array a float array
 * @param range an IntRange indicating the indices for the items to retrieve
 * @return list of the retrieved floats
 * @since 1.0
 */
@SuppressWarnings("unchecked")
public static List<Float> getAt(float[] array, IntRange range) {
    RangeInfo info = subListBorders(array.length, range);
    List<Float> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
    return info.reverse ? reverse(answer) : answer;
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Support the subscript operator with an IntRange for a double array
 *
 * @param array a double array
 * @param range an IntRange indicating the indices for the items to retrieve
 * @return list of the retrieved doubles
 * @since 1.0
 */
@SuppressWarnings("unchecked")
public static List<Double> getAt(double[] array, IntRange range) {
    RangeInfo info = subListBorders(array.length, range);
    List<Double> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
    return info.reverse ? reverse(answer) : answer;
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Support the subscript operator with an IntRange for a boolean array
 *
 * @param array a boolean array
 * @param range an IntRange indicating the indices for the items to retrieve
 * @return list of the retrieved booleans
 * @since 1.0
 */
@SuppressWarnings("unchecked")
public static List<Boolean> getAt(boolean[] array, IntRange range) {
    RangeInfo info = subListBorders(array.length, range);
    List<Boolean> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
    return info.reverse ? reverse(answer) : answer;
}
项目:groovy    文件:GPathResult.java   
/**
 * Supports the range subscript operator for a GPathResult.
 * <pre class="groovyTestCase">
 * import groovy.util.slurpersupport.*
 * def text = """
 * &lt;characterList>
 *   &lt;character&gt;Wallace&lt;/character&gt;
 *   &lt;character&gt;Gromit&lt;/character&gt;
 *   &lt;character&gt;Shaun&lt;/character&gt;
 * &lt;/characterList&gt;"""
 *
 * GPathResult characterList = new XmlSlurper().parseText(text)
 *
 * assert characterList.character[1..2].join(',') == 'Gromit,Shaun'
 * </pre>
 * @param range a Range indicating the items to get
 * @return a new list based on range borders
 */
public Object getAt(final IntRange range) {
    return DefaultGroovyMethods.getAt(list(), range);
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Returns indices of the collection.
 * <p>
 * Example:
 * <pre class="groovyTestCase">
 * assert 0..2 == [5, 6, 7].indices
 * </pre>
 *
 * @param self a collection
 * @return an index range
 * @since 2.4.0
 */
public static IntRange getIndices(Collection self) {
    return new IntRange(false, 0, self.size());
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Returns indices of the array.
 * <p>
 * Example:
 * <pre class="groovyTestCase">
 * String[] letters = ['a', 'b', 'c', 'd']
 * assert 0..<4 == letters.indices
 * </pre>
 *
 * @param self an array
 * @return an index range
 * @since 2.4.0
 */
public static <T> IntRange getIndices(T[] self) {
    return new IntRange(false, 0, self.length);
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 *
 * @param array an Array of Objects
 * @param range an IntRange
 * @return a range of a list from the range's from index up to but not
 *         including the range's to value
 * @since 1.0
 */
public static <T> List<T> getAt(T[] array, IntRange range) {
    List<T> list = Arrays.asList(array);
    return getAt(list, range);
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * List subscript assignment operator when given a range as the index and
 * the assignment operand is a collection.
 * Example: <pre class="groovyTestCase">def myList = [4, 3, 5, 1, 2, 8, 10]
 * myList[3..5] = ["a", true]
 * assert myList == [4, 3, 5, "a", true, 10]</pre>
 *
 * Items in the given
 * range are replaced with items from the collection.
 *
 * @param self  a List
 * @param range the subset of the list to set
 * @param col   the collection of values to put at the given sublist
 * @since 1.5.0
 */
public static void putAt(List self, IntRange range, Collection col) {
    List sublist = resizeListWithRangeAndGetSublist(self, range);
    if (col.isEmpty()) return;
    sublist.addAll(col);
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * List subscript assignment operator when given a range as the index.
 * Example: <pre class="groovyTestCase">def myList = [4, 3, 5, 1, 2, 8, 10]
 * myList[3..5] = "b"
 * assert myList == [4, 3, 5, "b", 10]</pre>
 *
 * Items in the given
 * range are replaced with the operand.  The <code>value</code> operand is
 * always treated as a single value.
 *
 * @param self  a List
 * @param range the subset of the list to set
 * @param value the value to put at the given sublist
 * @since 1.0
 */
public static void putAt(List self, IntRange range, Object value) {
    List sublist = resizeListWithRangeAndGetSublist(self, range);
    sublist.add(value);
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Support assigning a range of values with a single assignment statement.
 *
 * @param self  a BitSet
 * @param range the range of values to set
 * @param value value
 * @see java.util.BitSet
 * @see groovy.lang.Range
 * @since 1.5.0
 */
public static void putAt(BitSet self, IntRange range, boolean value) {
    RangeInfo info = subListBorders(self.length(), range);
    self.set(info.from, info.to, value);
}
项目:groovy    文件:StringGroovyMethods.java   
/**
 * Support the range subscript operator for CharSequence with IntRange
 *
 * @param text  a CharSequence
 * @param range an IntRange
 * @return the subsequence CharSequence
 * @since 1.0
 */
public static CharSequence getAt(CharSequence text, IntRange range) {
    return getAt(text, (Range) range);
}
项目:groovy    文件:StringGroovyMethods.java   
/**
 * Support the range subscript operator for GString with IntRange
 *
 * @param text  a GString
 * @param range an IntRange
 * @return the String of characters corresponding to the provided range
 * @since 2.3.7
 */
public static String getAt(GString text, IntRange range) {
    return getAt(text, (Range) range);
}
项目:groovy    文件:StringGroovyMethods.java   
/**
 * Support the range subscript operator for String with IntRange
 *
 * @param text  a String
 * @param range an IntRange
 * @return the resulting String
 * @since 1.0
 */
public static String getAt(String text, IntRange range) {
    return getAt(text, (Range) range);
}
项目:groovy    文件:StringGroovyMethods.java   
/**
 * Support the range subscript operator for StringBuffer.  Index values are
 * treated as characters within the buffer.
 *
 * @param self  a StringBuffer
 * @param range a Range
 * @param value the object that's toString() will be inserted
 * @since 1.0
 */
public static void putAt(StringBuffer self, IntRange range, Object value) {
    RangeInfo info = subListBorders(self.length(), range);
    self.replace(info.from, info.to, value.toString());
}
项目:groovy    文件:PluginDefaultGroovyMethods.java   
/**
 * Support the range subscript operator for StringBuilder.
 * Index values are treated as characters within the builder.
 *
 * @param self  a StringBuilder
 * @param range a Range
 * @param value the object that's toString() will be inserted
 */
public static void putAt(StringBuilder self, IntRange range, Object value) {
    RangeInfo info = subListBorders(self.length(), range);
    self.replace(info.from, info.to, value.toString());
}