use of java.util.Iterator in project groovy-core by groovy.
the class StringGroovyMethods method getAt.
/**
* Support the subscript operator, e.g. matcher[index], for a regex Matcher.
* <p>
* For an example using no group match,
* <pre>
* def p = /ab[d|f]/
* def m = "abcabdabeabf" =~ p
* assert 2 == m.count
* assert 2 == m.size() // synonym for m.getCount()
* assert ! m.hasGroup()
* assert 0 == m.groupCount()
* def matches = ["abd", "abf"]
* for (i in 0..<m.count) {
*   assert m[i] == matches[i]
* }
* </pre>
* <p>
* For an example using group matches,
* <pre>
* def p = /(?:ab([c|d|e|f]))/
* def m = "abcabdabeabf" =~ p
* assert 4 == m.count
* assert m.hasGroup()
* assert 1 == m.groupCount()
* def matches = [["abc", "c"], ["abd", "d"], ["abe", "e"], ["abf", "f"]]
* for (i in 0..<m.count) {
*   assert m[i] == matches[i]
* }
* </pre>
* <p>
* For another example using group matches,
* <pre>
* def m = "abcabdabeabfabxyzabx" =~ /(?:ab([d|x-z]+))/
* assert 3 == m.count
* assert m.hasGroup()
* assert 1 == m.groupCount()
* def matches = [["abd", "d"], ["abxyz", "xyz"], ["abx", "x"]]
* for (i in 0..<m.count) {
*   assert m[i] == matches[i]
* }
* </pre>
*
* @param matcher a Matcher
* @param idx an index
* @return object a matched String if no groups matched, list of matched groups otherwise.
* @since 1.0
*/
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);
Iterator iter = iterator(matcher);
Object result = null;
for (int i = 0; i <= idx; i++) {
result = iter.next();
}
return result;
} catch (IllegalStateException ex) {
return null;
}
}
use of java.util.Iterator in project groovy-core by groovy.
the class StringGroovyMethods method findAll.
/**
* Returns a (possibly empty) list of all occurrences of a regular expression (in Pattern format) found within a CharSequence.
* <p>
* For example, if the pattern doesn't match, it returns an empty list:
* <pre>
* assert [] == "foo".findAll(~/(\w*) Fish/)
* </pre>
* Any regular expression matches are returned in a list, and all regex capture groupings are ignored, only the full match is returned:
* <pre>
* def expected = ["One Fish", "Two Fish", "Red Fish", "Blue Fish"]
* assert expected == "One Fish, Two Fish, Red Fish, Blue Fish".findAll(~/(\w*) Fish/)
* </pre>
*
* @param self a CharSequence
* @param pattern the compiled regex Pattern
* @return a List containing all full matches of the Pattern within the CharSequence, an empty list will be returned if there are no matches
* @see #findAll(String, java.util.regex.Pattern)
* @since 1.8.2
*/
public static List<String> findAll(CharSequence self, Pattern pattern) {
Matcher matcher = pattern.matcher(self.toString());
boolean hasGroup = hasGroup(matcher);
List<String> list = new ArrayList<String>();
for (Iterator iter = iterator(matcher); iter.hasNext(); ) {
if (hasGroup) {
list.add((String) ((List) iter.next()).get(0));
} else {
list.add((String) iter.next());
}
}
return new ArrayList<String>(list);
}
use of java.util.Iterator in project gradle by gradle.
the class DOM4JSettingsNode method convertNodes.
private List<SettingsNode> convertNodes(List elements) {
List<SettingsNode> children = new ArrayList<SettingsNode>();
Iterator iterator = elements.iterator();
while (iterator.hasNext()) {
Element childElement = (Element) iterator.next();
children.add(new DOM4JSettingsNode(childElement));
}
return children;
}
use of java.util.Iterator in project grails-core by grails.
the class ConstrainedPropertyTests method testConstraintBuilder.
@SuppressWarnings("rawtypes")
public void testConstraintBuilder() throws Exception {
GroovyClassLoader gcl = new GroovyClassLoader();
Class groovyClass = gcl.parseClass("class TestClass {\n" + "Long id\n" + "Long version\n" + "String login\n" + "String other\n" + "String email\n" + "static constraints = {\n" + "login(size:5..15,nullable:false,blank:false)\n" + "other(blank:false,size:5..15,nullable:false)\n" + "email(email:true)\n" + "}\n" + "}");
GrailsApplication ga = new DefaultGrailsApplication(groovyClass);
ga.initialise();
new MappingContextBuilder(ga).build(groovyClass);
GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "TestClass");
Map constrainedProperties = domainClass.getConstrainedProperties();
assert constrainedProperties.size() == 3;
grails.gorm.validation.ConstrainedProperty loginConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("login");
Collection appliedConstraints = loginConstraint.getAppliedConstraints();
assertTrue(appliedConstraints.size() == 3);
// Check the order of the constraints for the 'login' property...
int index = 0;
String[] constraintNames = new String[] { "size", "nullable", "blank" };
for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
Constraint c = (Constraint) iter.next();
assertEquals(constraintNames[index], c.getName());
index++;
}
// ...and for the 'other' property.
appliedConstraints = ((grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("other")).getAppliedConstraints();
index = 0;
constraintNames = new String[] { "blank", "size", "nullable" };
for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
Constraint c = (Constraint) iter.next();
assertEquals(constraintNames[index], c.getName());
index++;
}
grails.gorm.validation.ConstrainedProperty emailConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("email");
assertEquals(2, emailConstraint.getAppliedConstraints().size());
GroovyObject go = (GroovyObject) groovyClass.newInstance();
go.setProperty("email", "rubbish_email");
Errors errors = new BindException(go, "TestClass");
emailConstraint.validate(go, go.getProperty("email"), errors);
assertTrue(errors.hasErrors());
go.setProperty("email", "valid@email.com");
errors = new BindException(go, "TestClass");
emailConstraint.validate(go, go.getProperty("email"), errors);
assertFalse(errors.hasErrors());
}
use of java.util.Iterator in project groovy-core by groovy.
the class ModuleNode method handleMainMethodIfPresent.
/*
* If a main method is provided by user, account for it under run() as scripts generate their own 'main' so they can run.
*/
private void handleMainMethodIfPresent(List methods) {
boolean found = false;
for (Iterator iter = methods.iterator(); iter.hasNext(); ) {
MethodNode node = (MethodNode) iter.next();
if (node.getName().equals("main")) {
if (node.isStatic() && node.getParameters().length == 1) {
boolean retTypeMatches, argTypeMatches;
ClassNode argType = node.getParameters()[0].getType();
ClassNode retType = node.getReturnType();
argTypeMatches = (argType.equals(ClassHelper.OBJECT_TYPE) || argType.getName().contains("String[]"));
retTypeMatches = (retType == ClassHelper.VOID_TYPE || retType == ClassHelper.OBJECT_TYPE);
if (retTypeMatches && argTypeMatches) {
if (found) {
throw new RuntimeException("Repetitive main method found.");
} else {
found = true;
}
// if script has both loose statements as well as main(), then main() is ignored
if (statementBlock.isEmpty()) {
addStatement(node.getCode());
}
iter.remove();
}
}
}
}
}
Aggregations