use of java.util.Iterator in project groovy by apache.
the class SwingGroovyMethods method buildRowData.
private static Object[] buildRowData(DefaultTableModel delegate, Object row) {
int cols = delegate.getColumnCount();
Object[] rowData = new Object[cols];
int i = 0;
for (Iterator it = DefaultGroovyMethods.iterator(row); it.hasNext() && i < cols; ) {
rowData[i++] = it.next();
}
return rowData;
}
use of java.util.Iterator in project groovy by apache.
the class ManagedConcurrentLinkedQueueStressTest method testQueueRemoveCalledByMultipleThreadsOnSameElement.
@Test
public void testQueueRemoveCalledByMultipleThreadsOnSameElement() throws Exception {
final Object value1 = new Object();
final Object value2 = new Object();
queue.add(value1);
queue.add(value2);
final int threadCount = 8;
final CyclicBarrier barrier = new CyclicBarrier(threadCount + 1);
for (int i = 0; i < threadCount; i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Iterator<Object> itr = queue.iterator();
Object o = itr.next();
assertEquals(value1, o);
ThreadUtils.await(barrier);
itr.remove();
ThreadUtils.await(barrier);
}
});
t.setDaemon(true);
t.start();
}
// start
ThreadUtils.await(barrier);
barrier.await(1L, TimeUnit.MINUTES);
Iterator<Object> itr = queue.iterator();
assertTrue(itr.hasNext());
assertEquals(value2, itr.next());
assertFalse(itr.hasNext());
}
use of java.util.Iterator in project groovy by apache.
the class GPathResult method breadthFirst.
/**
* Provides an Iterator over all the nodes of this GPathResult using a breadth-first traversal.
*
* @return the <code>Iterator</code> of (breadth-first) ordered GPathResults
*/
public Iterator breadthFirst() {
return new Iterator() {
private final List list = new LinkedList();
private Iterator iter = iterator();
private GPathResult next = getNextByBreadth();
public boolean hasNext() {
return this.next != null;
}
public Object next() {
try {
return this.next;
} finally {
this.next = getNextByBreadth();
}
}
public void remove() {
throw new UnsupportedOperationException();
}
private GPathResult getNextByBreadth() {
List children = new ArrayList();
while (this.iter.hasNext() || !children.isEmpty()) {
if (this.iter.hasNext()) {
final GPathResult node = (GPathResult) this.iter.next();
this.list.add(node);
this.list.add(this.iter);
children.add(node.children());
} else {
List nextLevel = new ArrayList();
for (Object child : children) {
GPathResult next = (GPathResult) child;
Iterator iterator = next.iterator();
while (iterator.hasNext()) {
nextLevel.add(iterator.next());
}
}
this.iter = nextLevel.iterator();
children = new ArrayList();
}
}
if (this.list.isEmpty()) {
return null;
} else {
GPathResult result = (GPathResult) this.list.get(0);
this.list.remove(0);
this.iter = (Iterator) this.list.get(0);
this.list.remove(0);
return result;
}
}
};
}
use of java.util.Iterator in project groovy by apache.
the class GPathResult method depthFirst.
/**
* Provides an Iterator over all the nodes of this GPathResult using a depth-first traversal.
*
* @return the <code>Iterator</code> of (depth-first) ordered GPathResults
*/
public Iterator depthFirst() {
return new Iterator() {
private final List list = new LinkedList();
private final Stack stack = new Stack();
private Iterator iter = iterator();
private GPathResult next = getNextByDepth();
public boolean hasNext() {
return this.next != null;
}
public Object next() {
try {
return this.next;
} finally {
this.next = getNextByDepth();
}
}
public void remove() {
throw new UnsupportedOperationException();
}
private GPathResult getNextByDepth() {
while (this.iter.hasNext()) {
final GPathResult node = (GPathResult) this.iter.next();
this.list.add(node);
this.stack.push(this.iter);
this.iter = node.children().iterator();
}
if (this.list.isEmpty()) {
return null;
} else {
GPathResult result = (GPathResult) this.list.get(0);
this.list.remove(0);
this.iter = (Iterator) this.stack.pop();
return result;
}
}
};
}
use of java.util.Iterator in project groovy by apache.
the class GPathResult method getAt.
/**
* Supports the subscript operator for a GPathResult.
* <pre class="groovyTestCase">
* import groovy.util.slurpersupport.*
* def text = """
* <characterList>
* <character/>
* <character>
* <name>Gromit</name>
* </character>
* </characterList>"""
*
* GPathResult characterList = new XmlSlurper().parseText(text)
*
* assert characterList.character[1].name == 'Gromit'
* </pre>
* @param index an index
* @return the value at the given index
*/
public Object getAt(final int index) {
if (index < 0) {
// calculate whole list in this case
// recommend avoiding -ve's as this is obviously not as efficient
List list = list();
int adjustedIndex = index + list.size();
if (adjustedIndex >= 0 && adjustedIndex < list.size())
return list.get(adjustedIndex);
} else {
final Iterator iter = iterator();
int count = 0;
while (iter.hasNext()) {
if (count++ == index) {
return iter.next();
} else {
iter.next();
}
}
}
return new NoChildren(this, this.name, this.namespaceTagHints);
}
Aggregations