Example usage for java.util Iterator Iterator

List of usage examples for java.util Iterator Iterator

Introduction

In this page you can find the example usage for java.util Iterator Iterator.

Prototype

Iterator

Source Link

Usage

From source file:org.jbpm.formModeler.service.bb.mvc.taglib.formatter.ForFormatter.java

public void service(HttpServletRequest request, HttpServletResponse response) throws FormatterException {
    log.debug("Servicing ForFormatter.");
    Object array = getParameter("array");
    if (array == null) {
        Object componentName = getParameter("bean");
        Object propertyName = getParameter("property");
        if (componentName != null) {
            Object component = CDIBeanLocator.getBeanByNameOrType((String) componentName);
            array = component;//  w  ww .ja  v a  2  s . c om
            if (propertyName != null) {
                JXPathContext ctx = JXPathContext.newContext(component);
                try {
                    array = ctx.getValue((String) propertyName);
                } catch (Exception e) {
                    log.debug("Error:", e);
                }
            }
        }
    }
    String sortProperties = (String) getParameter("sortProperties");

    Iterator iterator = null;
    if (array == null) {
        renderFragment("empty");
        return;
    }

    if (array instanceof Collection) {
        iterator = ((Collection) array).iterator();
    } else if (array.getClass().isArray()) {
        final Object theArray = array;
        iterator = new Iterator() {
            int index = 0;

            public void remove() {
                throw new UnsupportedOperationException();
            }

            public boolean hasNext() {
                return Array.getLength(theArray) > index;
            }

            public Object next() {
                return Array.get(theArray, index++);
            }
        };
    } else if (array instanceof Iterator) {
        iterator = (Iterator) array;
    } else if (array instanceof Enumeration) {
        List l = new ArrayList();
        while (((Enumeration) array).hasMoreElements()) {
            l.add(((Enumeration) array).nextElement());
        }
        iterator = l.iterator();
    }

    if (sortProperties != null) {
        iterator = getSortedIterator(iterator, sortProperties);
    }

    if (iterator != null && iterator.hasNext()) {
        renderFragment("outputStart");
        int i = 0;
        while (iterator.hasNext()) {
            Object o = iterator.next();
            setAttribute("index", new Integer(i));
            setAttribute("count", new Integer(++i));
            if (o != null)
                setAttribute("element", o);
            else
                setAttribute("element", getParameter("nullValue"));
            renderFragment("output");
        }
        renderFragment("outputEnd");
    } else {
        renderFragment("empty");
    }
}

From source file:de.tudarmstadt.lt.nlkg.DT.java

public Entry get(String word, int max_dt_words) {
    try {//w  ww . j  av a 2 s  .c om
        String query = String.format(
                "SELECT word2,count FROM `dt` WHERE word1 LIKE '%s' ORDER BY count DESC LIMIT %d;", word,
                max_dt_words + 1);
        final Connection c = connect();
        final Statement s = c.createStatement();
        final ResultSet r = s.executeQuery(query);

        if (!r.next())
            return Entry.EMPTY;
        Entry result = new Entry() {
            {
                word = new Word() {
                    {
                        word = r.getString(1);
                        significance = r.getDouble(2);
                    }
                };
                dtwords = new Iterator<Word>() {
                    @Override
                    public boolean hasNext() {
                        try {
                            if (r.isClosed())
                                return false;
                            if (r.next())
                                return true;
                            r.close();
                            s.close();
                            return false;
                        } catch (SQLException e) {
                            e.printStackTrace();
                            return false;
                        }
                    }

                    @Override
                    public Word next() {
                        try {
                            return new Word() {
                                {
                                    word = r.getString(1);
                                    significance = r.getDouble(2);
                                }
                            };
                        } catch (SQLException e) {
                            e.printStackTrace();
                            return Word.EMPTY;
                        }
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    protected void finalize() throws Throwable {
                        r.close();
                        r.close();
                        super.finalize();
                    }
                };
            }
        };
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return Entry.EMPTY;
    }

}

From source file:com.googlecode.l10nmavenplugin.model.BundlePropertiesFamily.java

/**
 * {@inheritDoc}/* w  ww .j a va  2s. c  o m*/
 */
public Iterator<PropertyFamily> iterator() {
    return new Iterator<PropertyFamily>() {

        private Iterator<String> keysIterator = getKeys().iterator();

        public boolean hasNext() {
            return keysIterator.hasNext();
        }

        public PropertyFamily next() {
            String key = keysIterator.next();
            return getPropertyFamily(key);
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:net.sourceforge.jabm.gametheory.CompressedPayoffMatrix.java

public Iterator<FullEntry> fullEntryIterator() {
    final CompressedPayoffMatrix matrix = this;
    return new Iterator<FullEntry>() {

        BaseNIterator b = new BaseNIterator(numStrategies, numPlayers);

        public boolean hasNext() {
            return b.hasNext();
        }// www .j  a v a2  s .c om

        public FullEntry next() {
            return new FullEntry(b.next(), matrix);
        }

        public void remove() {
        }

    };
}

From source file:de.codesourcery.eve.skills.ui.utils.CalendarWidget.java

private Iterator<MyButton> buttonIterator() {

    return new Iterator<MyButton>() {

        private int x = 0;
        private int y = 0;

        @Override/*from  w ww  . j a  va  2 s . c o  m*/
        public boolean hasNext() {
            return y < 4;
        }

        @Override
        public MyButton next() {

            if (!hasNext()) {
                throw new NoSuchElementException();
            }

            final MyButton result = buttons[y][x];

            x++;
            if (x == 7) {
                x = 0;
                y++;
            }
            return result;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove()");
        }
    };
}

From source file:cc.aileron.accessor.PojoAccessorMixerImpl.java

@Override
public PojoAccessor<?> mixin(final List<PojoAccessor<?>> accessors, final List<Object> parents) {
    final InstanceManager i = accessors.get(0).instanceManager();

    final HashMap<String, PojoAccessor<?>> map = new HashMap<String, PojoAccessor<?>>();
    final EnumMap<PojoAccessorMethod, List<String>> keys = new EnumMap<PojoAccessorMethod, List<String>>(
            PojoAccessorMethod.class);
    keys.put(PojoAccessorMethod.GET, new SkipList<String>());
    keys.put(PojoAccessorMethod.SET, new SkipList<String>());

    final HashMap<String, PojoAccessor<?>> setmap = new HashMap<String, PojoAccessor<?>>();
    for (final PojoAccessor<?> accessor : accessors) {
        for (final PojoAccessorMethod method : PojoAccessorMethod.values()) {
            final List<String> methodKeys = accessor.keys(method);
            keys.get(method).addAll(methodKeys);
        }// w  w  w .  j a va2  s.c  om
        for (final String key : accessor.keys(PojoAccessorMethod.GET)) {
            map.put(key, accessor);
        }
        for (final String key : accessor.keys(PojoAccessorMethod.SET)) {
            setmap.put(key, accessor);
        }
    }
    final PojoAccessorManagerLocal manager = this.manager;
    return new PojoAccessor<Object>() {
        @Override
        public boolean exist(final String key) {
            for (final PojoAccessor<?> accessor : accessors) {
                if (accessor.exist(key)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public InstanceManager instanceManager() {
            return i;
        }

        @Override
        public List<String> keys(final PojoAccessorMethod method) {
            return keys.get(method);
        }

        @Override
        public PojoAccessor<?> mixin(final Object... objects) {
            final SkipList<PojoAccessor<?>> list = new SkipList<PojoAccessor<?>>();
            list.add(this);
            for (final Object object : objects) {
                list.add(manager.from(object, parents));
            }
            return PojoAccessorMixerImpl.this.mixin(list, parents);
        }

        @Override
        public Iterable<PojoAccessorValue> set(final PojoAccessorMethod method) {
            final HashMap<String, PojoAccessor<?>> m = method == PojoAccessorMethod.GET ? map : setmap;
            return new Iterable<PojoAccessorValue>() {

                @Override
                public Iterator<PojoAccessorValue> iterator() {
                    final Iterator<Entry<String, PojoAccessor<?>>> ite = m.entrySet().iterator();
                    return new Iterator<PojoAccessorValue>() {
                        @Override
                        public boolean hasNext() {
                            return ite.hasNext();
                        }

                        @Override
                        public PojoAccessorValue next() {
                            final Entry<String, PojoAccessor<?>> e = ite.next();
                            try {
                                return e.getValue().to(e.getKey());
                            } catch (final PojoAccessorValueNotFoundException e1) {
                                throw new Error(e1);
                            } catch (final PojoPropertiesNotFoundException e1) {
                                throw new Error(e1);
                            }
                        }

                        @Override
                        public void remove() {
                            throw new UnsupportedOperationException();
                        }
                    };
                }
            };
        }

        @Override
        public PojoAccessorValue to(final String key)
                throws PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
            {
                final PojoAccessor<?> a = map.get(key);
                if (a != null) {
                    return a.to(key);
                }
            }
            for (final PojoAccessor<?> a : accessors) {
                if (a.exist(key)) {
                    return a.to(key);
                }
            }
            for (final PojoAccessor<?> a : accessors) {
                try {
                    final PojoAccessorValue result = a.to(key);
                    if (result != null) {
                        return result;
                    }
                } catch (final PojoAccessorValueNotFoundException e) {
                } catch (final PojoPropertiesNotFoundException e) {
                }
            }
            throw new PojoAccessorValueNotFoundException(accessors, key);
        }

        @Override
        public String toString() {
            return ReflectionToStringBuilder.toString(this);
        }

        @Override
        public Object toTarget() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:mvm.rya.indexing.accumulo.freetext.query.ASTNodeUtils.java

public static Iterable<SimpleNode> getNodeIterator(final SimpleNode n) {
    return new Iterable<SimpleNode>() {

        @Override//ww w.  j av  a  2 s .  c  om
        public Iterator<SimpleNode> iterator() {
            return new Iterator<SimpleNode>() {
                int pointer = 0;

                @Override
                public boolean hasNext() {
                    return pointer < n.jjtGetNumChildren();
                }

                @Override
                public SimpleNode next() {
                    Node rtn = n.jjtGetChild(pointer);
                    pointer++;
                    return (SimpleNode) rtn;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}

From source file:org.paxml.table.excel.ExcelTable.java

@Override
protected Iterator<IRow> getAllRows() {

    if (compact) {
        Iterator it = sheet.rowIterator();

        return new AbstractIteratorDecorator(it) {

            @Override/*from  ww w  .  ja  v a2 s  .  com*/
            public Object next() {

                return new ExcelRow((Row) getIterator().next(), ExcelTable.this);

            }

        };
    } else {
        return new Iterator<IRow>() {

            private int index = 0;

            @Override
            public boolean hasNext() {
                return index <= sheet.getLastRowNum();
            }

            @Override
            public IRow next() {

                ExcelRow r = getRow(index);
                index++;
                return r;

            }

            @Override
            public void remove() {
                Row row = sheet.getRow(index);
                if (row != null) {
                    sheet.removeRow(row);
                }
            }

        };
    }
}

From source file:com.feilong.core.ValidatorTest.java

/**
 * Test method for {@link com.feilong.core.Validator#isNullOrEmpty(java.lang.Object)}.
 *//* w ww  .  j  a  v  a  2  s  .  c  om*/
@Test
public void testIsNullOrEmpty() {
    List<String> list = new ArrayList<String>();

    assertEquals(true, isNullOrEmpty(""));
    assertEquals(true, isNullOrEmpty("   "));
    assertEquals(true, isNullOrEmpty(null));
    assertEquals(true, isNullOrEmpty(list));
    assertEquals(true, isNullOrEmpty(new LinkedHashMap<String, String>()));

    // **********Array*********************************
    assertEquals(true, isNullOrEmpty(new String[] {}));
    assertEquals(false, isNullOrEmpty(new Integer[] { 2 }));
    assertEquals(true, isNullOrEmpty(new Integer[][] {}));

    assertEquals(false, isNullOrEmpty(new Long[] { 2L }));
    assertEquals(true, isNullOrEmpty(new User[] {}));

    assertEquals(true, isNullOrEmpty(new int[] {}));
    assertEquals(false, isNullOrEmpty(new int[] { 1, 2 }));

    assertEquals(true, isNullOrEmpty(new double[] {}));
    assertEquals(false, isNullOrEmpty(new double[] { 1.2d }));

    assertEquals(true, isNullOrEmpty(new long[] {}));
    assertEquals(false, isNullOrEmpty(new long[] { 200L }));

    assertEquals(true, isNullOrEmpty(new float[] {}));
    assertEquals(false, isNullOrEmpty(new float[] { 5.8f }));

    assertEquals(true, isNullOrEmpty(new boolean[] {}));
    assertEquals(false, isNullOrEmpty(new boolean[] { true }));

    assertEquals(true, isNullOrEmpty(new byte[] {}));

    assertEquals(true, isNullOrEmpty(new char[] {}));
    assertEquals(false, isNullOrEmpty(new char[] { 'a' }));

    assertEquals(true, isNullOrEmpty(new short[] {}));
    assertEquals(false, isNullOrEmpty(new short[] { 5 }));

    assertEquals(true, isNullOrEmpty(list.iterator()));
    assertEquals(true, isNullOrEmpty(ConvertUtil.toEnumeration(list)));
    assertEquals(true, isNullOrEmpty(new Iterator<User>() {

        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public User next() {
            return null;
        }

        @Override
        public void remove() {
        }
    }));
}

From source file:UnifyHash.java

public Iterator iterator() {
    ///#ifdef JDK12
    cleanUp();//from   www. j  a v  a 2  s  .  co  m
    ///#endif

    return new Iterator() {
        private int bucket = 0;
        private int known = modCount;
        private Bucket nextBucket;
        private Object nextVal;

        {
            internalNext();
        }

        private void internalNext() {
            while (true) {
                while (nextBucket == null) {
                    if (bucket == buckets.length)
                        return;
                    nextBucket = buckets[bucket++];
                }

                nextVal = nextBucket.get();
                if (nextVal != null)
                    return;

                nextBucket = nextBucket.next;
            }
        }

        public boolean hasNext() {
            return nextBucket != null;
        }

        public Object next() {
            if (known != modCount)
                throw new ConcurrentModificationException();
            if (nextBucket == null)
                throw new NoSuchElementException();
            Object result = nextVal;
            nextBucket = nextBucket.next;
            internalNext();
            return result;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}