use of java.util.Iterator in project groovy-core by groovy.
the class Sequence method checkCollectionType.
// Implementation methods
//-------------------------------------------------------------------------
/**
* Checks that each member of the given collection are of the correct
* type
*/
protected void checkCollectionType(Collection c) {
if (type != null) {
for (Iterator iter = c.iterator(); iter.hasNext(); ) {
Object element = iter.next();
checkType(element);
}
}
}
use of java.util.Iterator in project groovy-core by groovy.
the class IOGroovyMethods method iterator.
/**
* Creates an iterator which will traverse through the reader a line at a time.
*
* @param self a Reader object
* @return an Iterator for the Reader
* @see java.io.BufferedReader#readLine()
* @since 1.5.0
*/
public static Iterator<String> iterator(Reader self) {
final BufferedReader bufferedReader;
if (self instanceof BufferedReader)
bufferedReader = (BufferedReader) self;
else
bufferedReader = new BufferedReader(self);
return new Iterator<String>() {
String nextVal;
boolean nextMustRead = true;
boolean hasNext = true;
public boolean hasNext() {
if (nextMustRead && hasNext) {
try {
nextVal = readNext();
nextMustRead = false;
} catch (IOException e) {
hasNext = false;
}
}
return hasNext;
}
public String next() {
String retval = null;
if (nextMustRead) {
try {
retval = readNext();
} catch (IOException e) {
hasNext = false;
}
} else
retval = nextVal;
nextMustRead = true;
return retval;
}
private String readNext() throws IOException {
String nv = bufferedReader.readLine();
if (nv == null)
hasNext = false;
return nv;
}
public void remove() {
throw new UnsupportedOperationException("Cannot remove() from a Reader Iterator");
}
};
}
use of java.util.Iterator in project groovy-core by groovy.
the class InvokerHelper method createMap.
public static Map createMap(Object[] values) {
Map answer = new LinkedHashMap(values.length / 2);
int i = 0;
while (i < values.length - 1) {
if ((values[i] instanceof SpreadMap) && (values[i + 1] instanceof Map)) {
Map smap = (Map) values[i + 1];
Iterator iter = smap.keySet().iterator();
for (; iter.hasNext(); ) {
Object key = iter.next();
answer.put(key, smap.get(key));
}
i += 2;
} else {
answer.put(values[i++], values[i++]);
}
}
return answer;
}
use of java.util.Iterator in project groovy-core by groovy.
the class InvokerHelper method spreadMap.
public static SpreadMap spreadMap(Object value) {
if (value instanceof Map) {
Object[] values = new Object[((Map) value).keySet().size() * 2];
int index = 0;
Iterator it = ((Map) value).keySet().iterator();
for (; it.hasNext(); ) {
Object key = it.next();
values[index++] = key;
values[index++] = ((Map) value).get(key);
}
return new SpreadMap(values);
}
throw new SpreadMapEvaluatingException("Cannot spread the map " + value.getClass().getName() + ", value " + value);
}
use of java.util.Iterator in project groovy-core by groovy.
the class FlatNodeListTraversal method process.
public AST process(AST t) {
GroovySourceAST node = (GroovySourceAST) t;
// fetch all the nodes in this AST into a List
NodeCollector collector = new NodeCollector();
AntlrASTProcessor internalTraversal = new PreOrderTraversal(collector);
internalTraversal.process(t);
List listOfAllNodesInThisAST = collector.getNodes();
// process each node in turn
setUp(node);
Iterator itr = listOfAllNodesInThisAST.iterator();
while (itr.hasNext()) {
GroovySourceAST currentNode = (GroovySourceAST) itr.next();
accept(currentNode);
}
tearDown(node);
return null;
}
Aggregations