groovy.lang.Range Java Examples

The following examples show how to use groovy.lang.Range. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TypeInferenceVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private ClassNode deriveExpressonType(Expression expression) {
    ClassNode derivedExpressionType = null;
    if (expression instanceof ConstantExpression
            && !expression.getText().equals("null")) { // NOI18N
        derivedExpressionType = ((ConstantExpression) expression).getType();
    } else if (expression instanceof ConstructorCallExpression) {
        derivedExpressionType = ((ConstructorCallExpression) expression).getType();
    } else if (expression instanceof MethodCallExpression) {
        int newOffset = ASTUtils.getOffset(doc, expression.getLineNumber(), expression.getColumnNumber());
        AstPath newPath = new AstPath(path.root(), newOffset, doc);
        derivedExpressionType = MethodInference.findCallerType(expression, newPath, doc, newOffset);
    } else if (expression instanceof StaticMethodCallExpression) {
        derivedExpressionType = MethodInference.findCallerType(expression, path, doc, cursorOffset);
    } else if (expression instanceof ListExpression) {
        derivedExpressionType = ((ListExpression) expression).getType();
    } else if (expression instanceof MapExpression) {
        derivedExpressionType = ((MapExpression) expression).getType();
    } else if (expression instanceof RangeExpression) {
        // this should work, but the type is Object - nut sure why
        // guessedType = ((RangeExpression)initialExpression).getType();
        derivedExpressionType = ClassHelper.makeWithoutCaching(Range.class, true);                
    } 
    return derivedExpressionType;
}
 
Example #2
Source File: DefaultGroovyMethodsSupport.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected static RangeInfo subListBorders(int size, Range range) {
    if (range instanceof IntRange) {
        return ((IntRange) range).subListBorders(size);
    }
    if (range instanceof NumberRange) {
        return ((NumberRange) 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);
}
 
Example #3
Source File: StringGroovyMethods.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Selects a List of values from a Matcher using a Collection
 * to identify the indices to be selected.
 *
 * @param self    a Matcher
 * @param indices a Collection of indices
 * @return a String of the values at the given indices
 *
 * @since 1.6.0
 */
public static List getAt(final Matcher self, Collection indices) {
    List result = new ArrayList();
    if (indices instanceof IntRange) {
        int size = (int) size(self);
        RangeInfo info = subListBorders(size, (Range) indices);
        indices = new IntRange(((IntRange)indices).getInclusive(), info.from, info.to - 1);
    }
    for (Object value : indices) {
        if (value instanceof Range) {
            result.addAll(getAt(self, (Range) value));
        } else {
            int idx = DefaultTypeTransformation.intUnbox(value);
            result.add(getAt(self, idx));
        }
    }
    return result;
}
 
Example #4
Source File: TypeInferenceVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void visitVariableExpression(VariableExpression expression) {
        if (expression.isSuperExpression()) {
            guessedType = expression.getType().getSuperClass();
        }
        if (null != expression.getAccessedVariable()) {
            Variable accessedVariable = expression.getAccessedVariable();

            if (accessedVariable.hasInitialExpression()) {
                Expression initialExpression = expression.getAccessedVariable().getInitialExpression();
                if (initialExpression instanceof ConstantExpression
                        && !initialExpression.getText().equals("null")) { // NOI18N
                    guessedType = ((ConstantExpression) initialExpression).getType();
                } else if (initialExpression instanceof ConstructorCallExpression) {
                    guessedType = ClassHelper.make(((ConstructorCallExpression) initialExpression).getType().getName());
                } else if (initialExpression instanceof MethodCallExpression) {
                    int newOffset = ASTUtils.getOffset(doc, initialExpression.getLineNumber(), initialExpression.getColumnNumber());
                    AstPath newPath = new AstPath(path.root(), newOffset, doc);
                    guessedType = MethodInference.findCallerType(initialExpression, newPath, doc, newOffset);
                } else if (initialExpression instanceof ListExpression) {
                    guessedType = ((ListExpression) initialExpression).getType();
                } else if (initialExpression instanceof MapExpression) {
                    guessedType = ((MapExpression) initialExpression).getType();
                } else if (initialExpression instanceof RangeExpression) {
                    // this should work, but the type is Object - nut sure why
                    // guessedType = ((RangeExpression)initialExpression).getType();
                    guessedType = ClassHelper.makeWithoutCaching(Range.class, true);                
                }
            } else if (accessedVariable instanceof Parameter) {
                Parameter param = (Parameter) accessedVariable;
                guessedType = param.getType();
            }
        } else if (!expression.getType().getName().equals("java.lang.Object")) {
            guessedType = expression.getType();

        }
    super.visitVariableExpression(expression);
}
 
Example #5
Source File: StringGroovyMethods.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Selects a List of characters from a CharSequence using a Collection
 * to identify the indices to be selected.
 *
 * @param self    a CharSequence
 * @param indices a Collection of indices
 * @return a String consisting of the characters at the given indices
 *
 * @since 1.0
 */
public static String getAt(final CharSequence self, final Collection indices) {
    StringBuilder answer = new StringBuilder();
    for (Object value : indices) {
        if (value instanceof Range) {
            answer.append(getAt(self, (Range) value));
        } else if (value instanceof Collection) {
            answer.append(getAt(self, (Collection) value));
        } else {
            int idx = DefaultTypeTransformation.intUnbox(value);
            answer.append(getAt(self, idx));
        }
    }
    return answer.toString();
}
 
Example #6
Source File: StringGroovyMethods.java    From groovy with Apache License 2.0 3 votes vote down vote up
/**
 * Supports the range subscript operator for String.
 *
 * @param self  a String
 * @param range a Range
 * @return a substring corresponding to the Range
 *
 * @since 1.0
 */
public static String getAt(final String self, final Range range) {
    RangeInfo info = subListBorders(self.length(), range);
    String answer = self.substring(info.from, info.to);
    if (info.reverse) {
        answer = reverse(answer);
    }
    return answer;
}
 
Example #7
Source File: StringGroovyMethods.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Supports the range subscript operator for CharSequence with IntRange.
 *
 * @param self  a CharSequence
 * @param range an IntRange
 * @return the subsequence CharSequence
 *
 * @since 1.0
 */
public static CharSequence getAt(final CharSequence self, final IntRange range) {
    return getAt(self, (Range) range);
}
 
Example #8
Source File: StringGroovyMethods.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Supports the range subscript operator for GString with IntRange.
 *
 * @param self  a GString
 * @param range an IntRange
 * @return the String of characters corresponding to the provided range
 *
 * @since 2.3.7
 */
public static String getAt(final GString self, final IntRange range) {
    return getAt(self, (Range) range);
}
 
Example #9
Source File: StringGroovyMethods.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Supports the range subscript operator for CharSequence.
 *
 * @param self  a CharSequence
 * @param range a Range
 * @return the subsequence CharSequence
 *
 * @since 1.0
 */
public static CharSequence getAt(final CharSequence self, final Range range) {
    RangeInfo info = subListBorders(self.length(), range);
    CharSequence sequence = self.subSequence(info.from, info.to);
    return info.reverse ? reverse(sequence) : sequence;
}
 
Example #10
Source File: StringGroovyMethods.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Supports the range subscript operator for GString.
 *
 * @param self  a GString
 * @param range a Range
 * @return the String of characters corresponding to the provided range
 *
 * @since 2.3.7
 */
public static String getAt(final GString self, final Range range) {
    return getAt(self.toString(), range);
}
 
Example #11
Source File: StringGroovyMethods.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Supports the range subscript operator for String with IntRange.
 *
 * @param self  a String
 * @param range an IntRange
 * @return the resulting String
 *
 * @since 1.0
 */
public static String getAt(final String self, final IntRange range) {
    return getAt(self, (Range) range);
}