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.paxml.bean.excel.ReadExcelTag.java

/**
 * Do excel query.//from   w  w  w.jav  a 2s. co m
 * 
 * @param context
 * @return iterator if lazy, otherwise list.
 * @throws Exception
 */
private Iterator doQuery(Context context) throws Exception {

    return new Iterator() {
        private Connection con;
        private PreparedStatement s;
        private ResultSet rs;
        private String[] columns;

        @Override
        public void finalize() {
            end();
        }

        private void end() {
            closeQueryResource(con, s, rs);
        }

        private void start() {
            if (con != null) {
                return;
            }
            File f = file.getFile();
            con = getConnection(f);
            if (log.isDebugEnabled()) {
                log.debug("Opened excel file via odbc: " + f.getAbsolutePath());
                log.debug("Executing excel query: " + query);
            }
            try {
                s = getPreparedStatement(con);
                s.execute();
                rs = s.getResultSet();
                if (rs != null) {

                    ResultSetMetaData meta = rs.getMetaData();
                    columns = new String[meta.getColumnCount()];
                    for (int i = columns.length - 1; i >= 0; i--) {
                        columns[i] = meta.getColumnName(i);
                    }

                }
            } catch (Exception e) {
                end();
                throw new PaxmlRuntimeException("Cannot execute excel query: " + query);
            }
        }

        @Override
        public boolean hasNext() {
            start();
            try {
                boolean hasNext = rs != null && rs.next();
                if (!hasNext) {
                    end();
                }
                return hasNext;
            } catch (Exception e) {
                end();
                throw new PaxmlRuntimeException(e);
            }
        }

        @Override
        public Object next() {
            Map<String, Object> row = new LinkedHashMap<String, Object>(columns.length);
            try {

                for (int i = 1; i <= columns.length; i++) {
                    String column = columns[i];
                    Object value = rs.getObject(i);
                    row.put(column, value);
                }
            } catch (Exception e) {
                end();
                throw new PaxmlRuntimeException(e);
            }
            return row;
        }

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

    };

}

From source file:org.apache.cocoon.components.expression.jexl.JSIntrospector.java

public Iterator getIterator(Object obj, Info i) throws Exception {
    if (!(obj instanceof Scriptable)) {
        // support Enumeration
        /*// w ww.  j av  a  2 s .c  om
           Booth Enumeration and Iterator are supported in
           Uberspect. The only difference is that they emit a
           rather long warning message to commons logging, telling
           that Enumerations and Iterator not are resettable and
           cannot be reused.
        */
        if (obj instanceof Enumeration) {
            final Enumeration e = (Enumeration) obj;
            return new Iterator() {

                public boolean hasNext() {
                    return e.hasMoreElements();
                }

                public Object next() {
                    return e.nextElement();
                }

                public void remove() {
                    // no action
                }

            };
        }
        if (obj instanceof Iterator) {
            // support Iterator
            return (Iterator) obj;
        }
        return super.getIterator(obj, i);
    }
    if (obj instanceof NativeArray) {
        return new NativeArrayIterator((NativeArray) obj);
    }
    return new ScriptableIterator((Scriptable) obj);
}

From source file:com.skelril.aurora.jail.CSVJailCellDatabase.java

@Override
public Iterator<JailCell> iterator() {

    return new Iterator<JailCell>() {

        private final Iterator<JailCell> setIter = jailCells.iterator();
        private JailCell next;

        public boolean hasNext() {

            return setIter.hasNext();
        }//w  w  w. j a v  a  2  s. c o  m

        public JailCell next() {

            return next = setIter.next();
        }

        public void remove() {

            deleteJailCell(next.getPrisonName(), next.getCellName(), null);
        }
    };
}

From source file:com.norconex.collector.core.data.store.impl.mongo.MongoCrawlDataStore.java

@Override
public Iterator<ICrawlData> getCacheIterator() {
    final DBCursor cursor = collCached.find();
    return new Iterator<ICrawlData>() {
        @Override//w  w w . j a v a  2s .  c om
        public boolean hasNext() {
            return cursor.hasNext();
        }

        @Override
        public ICrawlData next() {
            return serializer.fromDBObject(cursor.next());
        }

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

From source file:com.github.fcannizzaro.prefs.Prefs.java

/**
 * Iterate a kind of values (String, Boolean , Integer ..)
 *//*from w  w w  . ja  v  a 2 s.  c  o m*/
public static <T> Iterator<Item<T>> iterator(Class c) {

    JSONObject obj = objects;

    if (c.equals(Integer.class))
        obj = integers;
    else if (c.equals(Float.class))
        obj = floats;
    else if (c.equals(Boolean.class))
        obj = booleans;
    else if (c.equals(String.class))
        obj = strings;
    else if (c.equals(Double.class))
        obj = doubles;

    final Iterator<String> keys = obj.keys();
    final JSONObject finalObj = obj;

    return new Iterator<Item<T>>() {

        @Override
        public boolean hasNext() {
            return keys.hasNext();
        }

        @Override
        public Item<T> next() {
            String key = keys.next();
            return new Item(key, finalObj.get(key));
        }

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

From source file:jetbrains.exodus.entitystore.FileSystemBlobVaultOld.java

@Override
public BackupStrategy getBackupStrategy() {
    return new BackupStrategy() {

        @Override/*  w  w  w .j  a  v  a 2 s.c o m*/
        public Iterable<FileDescriptor> listFiles() {
            return new Iterable<FileDescriptor>() {
                @Override
                public Iterator<FileDescriptor> iterator() {
                    final Deque<FileDescriptor> queue = new LinkedList<>();
                    queue.add(new FileDescriptor(location, blobsDirectory + File.separator));
                    return new Iterator<FileDescriptor>() {
                        int i = 0;
                        int n = 0;
                        File[] files;
                        FileDescriptor next;
                        String currentPrefix;

                        @Override
                        public boolean hasNext() {
                            if (next != null) {
                                return true;
                            }
                            while (i < n) {
                                final File file = files[i++];
                                final String name = file.getName();
                                if (file.isDirectory()) {
                                    queue.push(new FileDescriptor(file,
                                            currentPrefix + file.getName() + File.separator));
                                } else if (file.isFile()) {
                                    final long fileSize = file.length();
                                    if (fileSize == 0)
                                        continue;
                                    if (name.endsWith(blobExtension) || name.equalsIgnoreCase(VERSION_FILE)) {
                                        next = new FileDescriptor(file, currentPrefix, fileSize);
                                        return true;
                                    }
                                } else {
                                    // something strange with filesystem
                                    throw new EntityStoreException(
                                            "File or directory expected: " + file.toString());
                                }
                            }
                            if (queue.isEmpty()) {
                                return false;
                            }
                            final FileDescriptor fd = queue.pop();
                            files = IOUtil.listFiles(fd.getFile());
                            currentPrefix = fd.getPath();
                            i = 0;
                            n = files.length;
                            next = fd;
                            return true;
                        }

                        @Override
                        public FileDescriptor next() {
                            if (!hasNext()) {
                                throw new NoSuchElementException();
                            }
                            final FileDescriptor result = next;
                            next = null;
                            return result;
                        }

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

From source file:com.skelril.aurora.jail.CSVInmateDatabase.java

public Iterator<Inmate> iterator() {

    return new Iterator<Inmate>() {

        private final Iterator<Map.Entry<UUID, Inmate>> setIter = UUIDInmate.entrySet().iterator();
        private Inmate next;

        public boolean hasNext() {
            return setIter.hasNext();
        }/*  w  w  w. j av a  2 s .  c  o m*/

        public Inmate next() {
            next = setIter.next().getValue();
            return next;
        }

        public void remove() {
            unjail(next.getID(), null, "Removed by iterator");
        }
    };
}

From source file:org.apache.hadoop.yarn.server.api.records.impl.pb.NodeStatusPBImpl.java

private synchronized void addKeepAliveApplicationsToProto() {
    maybeInitBuilder();//w w  w  . j a v a 2s .  c  o m
    builder.clearKeepAliveApplications();
    if (keepAliveApplications == null)
        return;
    Iterable<ApplicationIdProto> iterable = new Iterable<ApplicationIdProto>() {
        @Override
        public Iterator<ApplicationIdProto> iterator() {
            return new Iterator<ApplicationIdProto>() {

                Iterator<ApplicationId> iter = keepAliveApplications.iterator();

                @Override
                public boolean hasNext() {
                    return iter.hasNext();
                }

                @Override
                public ApplicationIdProto next() {
                    return convertToProtoFormat(iter.next());
                }

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

                }
            };

        }
    };
    builder.addAllKeepAliveApplications(iterable);
}

From source file:act.installer.brenda.SQLConnection.java

/**
 * Iterate over all BRENDA organisms./*w  ww . ja  v a 2 s.  co m*/
 * @return An iterator over all BRENDA organisms.
 * @throws SQLException
 */
public Iterator<BrendaSupportingEntries.Organism> getOrganisms() throws SQLException {
    final PreparedStatement stmt = brendaConn.prepareStatement(BrendaSupportingEntries.Organism.QUERY);
    final ResultSet results = stmt.executeQuery();

    return new Iterator<BrendaSupportingEntries.Organism>() {
        @Override
        public boolean hasNext() {
            return hasNextHelper(results, stmt);
        }

        @Override
        public BrendaSupportingEntries.Organism next() {
            try {
                results.next();
                return BrendaSupportingEntries.Organism.fromResultSet(results);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

From source file:sample.client.Application.java

<T> Iterable<T> toIterable(final Enumeration<T> enumeration) {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return (enumeration == null ? Collections.<T>emptyIterator() : new Iterator<T>() {
                public boolean hasNext() {
                    return enumeration.hasMoreElements();
                }//  w ww  .j  ava  2 s  . co  m

                public T next() {
                    return enumeration.nextElement();
                }

                public void remove() {
                    throw new UnsupportedOperationException("Auto-generated method stub");
                }
            });
        }
    };
}