Search in sources :

Example 1 with IntRange

use of groovy.lang.IntRange in project groovy by apache.

the class StringGroovyMethods method getAt.

/**
     * Select 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(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;
}
Also used : ArrayList(java.util.ArrayList) IntRange(groovy.lang.IntRange) ArrayList(java.util.ArrayList) List(java.util.List) IntRange(groovy.lang.IntRange) EmptyRange(groovy.lang.EmptyRange) Range(groovy.lang.Range)

Example 2 with IntRange

use of groovy.lang.IntRange in project grails-core by grails.

the class RangeConstraintTests method testCreation.

public void testCreation() {
    RangeConstraint constraint = (RangeConstraint) getConstraint("testInteger", new IntRange(1, 5));
    assertEquals(ConstrainedProperty.RANGE_CONSTRAINT, constraint.getName());
    assertTrue(constraint.supports(Integer.class));
    assertTrue(constraint.supports(Long.class));
    assertTrue(constraint.supports(Double.class));
    assertFalse(constraint.supports(Object.class));
    assertFalse(constraint.supports(null));
    assertEquals(new IntRange(1, 5), constraint.getRange());
    try {
        getConstraint("testInteger", "wrong");
        fail("RangeConstraint must throw an exception for non-range parameters.");
    } catch (IllegalArgumentException iae) {
    // Great
    }
}
Also used : IntRange(groovy.lang.IntRange)

Example 3 with IntRange

use of groovy.lang.IntRange in project grails-core by grails.

the class SizeConstraintTests method testCreation.

public void testCreation() {
    SizeConstraint constraint = (SizeConstraint) getConstraint("testInteger", new IntRange(1, 5));
    assertEquals(ConstrainedProperty.SIZE_CONSTRAINT, constraint.getName());
    assertTrue(constraint.supports(List.class));
    assertTrue(constraint.supports(Collection.class));
    assertTrue(constraint.supports(Double[].class));
    assertFalse(constraint.supports(Object.class));
    assertFalse(constraint.supports(null));
    assertFalse(constraint.supports(Integer.class));
    assertFalse(constraint.supports(Number.class));
    assertEquals(new IntRange(1, 5), constraint.getRange());
    try {
        getConstraint("testInteger", "wrong");
        fail("SizeConstraint must throw an exception for non-range parameters.");
    } catch (IllegalArgumentException iae) {
    // Great
    }
}
Also used : IntRange(groovy.lang.IntRange) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with IntRange

use of groovy.lang.IntRange in project grails-core by grails.

the class SizeConstraintTests method testValidation.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void testValidation() {
    testConstraintMessageCodes(getConstraint("testString", new IntRange(2, 5)), "123456", new String[] { "testClass.testString.size.error", "testClass.testString.size.toobig" }, new Object[] { "testString", TestClass.class, "123456", 2, 5 });
    testConstraintMessageCodes(getConstraint("testString", new IntRange(2, 5)), "1", new String[] { "testClass.testString.size.error", "testClass.testString.size.toosmall" }, new Object[] { "testString", TestClass.class, "1", 2, 5 });
    testConstraintPassed(getConstraint("testArray", new IntRange(2, 5)), new String[] { "one", "two", "three" });
    List list = new ArrayList();
    list.add("one");
    testConstraintFailed(getConstraint("testArray", new IntRange(2, 5)), list);
    list.add("two");
    testConstraintPassed(getConstraint("testArray", new IntRange(2, 5)), list);
    // must always pass on null value
    testConstraintPassed(getConstraint("testArray", new IntRange(2, 5)), null);
    testConstraintDefaultMessage(getConstraint("testString", new IntRange(1, 5)), "123456", "Property [{0}] of class [{1}] with value [{2}] does not fall within the valid size range from [{3}] to [{4}]");
}
Also used : IntRange(groovy.lang.IntRange) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 5 with IntRange

use of groovy.lang.IntRange in project grails-core by grails.

the class ConstrainedPropertyTests method testGetMinSize.

public void testGetMinSize() {
    // validate that getMinSize returns null if the property has no minSize constraint and no size constraint
    ConstrainedProperty cp = new ConstrainedProperty(getClass(), "testURL", String.class);
    assertNull(cp.getMinSize());
    // validate that getMinSize returns the correct value when the minSize constraint is defined for the property (but no size constraint is defined)
    cp.applyConstraint(ConstrainedProperty.MIN_SIZE_CONSTRAINT, 5);
    assertEquals(5, cp.getMinSize().intValue());
    // validate that getMinSize returns the correct value when the size constraint is defined for the property (but no minSize constraint is defined)
    cp = new ConstrainedProperty(getClass(), "testURL", String.class);
    cp.applyConstraint(ConstrainedProperty.SIZE_CONSTRAINT, new IntRange(10, 20));
    assertEquals(10, cp.getMinSize().intValue());
    // validate that getMinSize returns the maximum of the minSize constraint and the lower bound of the size constraint
    //   1) validate where the lower bound of the size constraint is greater than the minSize constraint
    cp = new ConstrainedProperty(getClass(), "testURL", String.class);
    cp.applyConstraint(ConstrainedProperty.MIN_SIZE_CONSTRAINT, 6);
    cp.applyConstraint(ConstrainedProperty.SIZE_CONSTRAINT, new IntRange(11, 21));
    assertEquals(11, cp.getMinSize().intValue());
    //   2) validate where the minSize constraint is greater than the lower bound of the size constraint
    cp = new ConstrainedProperty(getClass(), "testURL", String.class);
    cp.applyConstraint(ConstrainedProperty.MIN_SIZE_CONSTRAINT, 12);
    cp.applyConstraint(ConstrainedProperty.SIZE_CONSTRAINT, new IntRange(9, 22));
    assertEquals(12, cp.getMinSize().intValue());
}
Also used : IntRange(groovy.lang.IntRange)

Aggregations

IntRange (groovy.lang.IntRange)6 ArrayList (java.util.ArrayList)3 List (java.util.List)3 EmptyRange (groovy.lang.EmptyRange)1 Range (groovy.lang.Range)1 Collection (java.util.Collection)1