Search in sources :

Example 11 with Iterator

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;
}
Also used : Iterator(java.util.Iterator)

Example 12 with Iterator

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());
}
Also used : Iterator(java.util.Iterator) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 13 with Iterator

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;
            }
        }
    };
}
Also used : Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) GroovyObject(groovy.lang.GroovyObject) LinkedList(java.util.LinkedList)

Example 14 with Iterator

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;
            }
        }
    };
}
Also used : Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) Stack(java.util.Stack)

Example 15 with Iterator

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 = """
     * &lt;characterList&gt;
     *   &lt;character/&gt;
     *   &lt;character&gt;
     *     &lt;name>Gromit&lt;/name&gt;
     *   &lt;/character&gt;
     * &lt;/characterList&gt;"""
     *
     * 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);
}
Also used : Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

Iterator (java.util.Iterator)7939 ArrayList (java.util.ArrayList)2053 Set (java.util.Set)1744 HashMap (java.util.HashMap)1678 HashSet (java.util.HashSet)1526 Map (java.util.Map)1486 List (java.util.List)1463 Test (org.junit.Test)576 IOException (java.io.IOException)465 Collection (java.util.Collection)320 Region (org.apache.geode.cache.Region)240 SSOException (com.iplanet.sso.SSOException)227 LinkedList (java.util.LinkedList)196 File (java.io.File)187 TreeSet (java.util.TreeSet)187 SMSException (com.sun.identity.sm.SMSException)169 LinkedHashMap (java.util.LinkedHashMap)146 IdRepoException (com.sun.identity.idm.IdRepoException)133 NoSuchElementException (java.util.NoSuchElementException)130 Session (org.hibernate.Session)126