Iterators.java revision 2973:0e8fa3249327
177957Sbenno/*
2176770Sraj * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3176770Sraj * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4176770Sraj *
5176770Sraj * This code is free software; you can redistribute it and/or modify it
6176770Sraj * under the terms of the GNU General Public License version 2 only, as
7176770Sraj * published by the Free Software Foundation.  Oracle designates this
8176770Sraj * particular file as subject to the "Classpath" exception as provided
9176770Sraj * by Oracle in the LICENSE file that accompanied this code.
10176770Sraj *
11176770Sraj * This code is distributed in the hope that it will be useful, but WITHOUT
12176770Sraj * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13176770Sraj * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14176770Sraj * version 2 for more details (a copy is included in the LICENSE file that
15176770Sraj * accompanied this code).
16176770Sraj *
17176770Sraj * You should have received a copy of the GNU General Public License version
18176770Sraj * 2 along with this work; if not, write to the Free Software Foundation,
19176770Sraj * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20176770Sraj *
21176770Sraj * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22176770Sraj * or visit www.oracle.com if you need additional information or have any
23176770Sraj * questions.
24176770Sraj */
25176770Srajpackage com.sun.tools.javac.util;
26176770Sraj
27176770Srajimport java.util.Iterator;
28176770Srajimport java.util.NoSuchElementException;
29176770Srajimport java.util.function.Function;
30176770Sraj
31176770Sraj/** Utilities for Iterators.
3277957Sbenno *
3377957Sbenno *  <p><b>This is NOT part of any supported API.
3477957Sbenno *  If you write code that depends on this, you do so at your own risk.
3577957Sbenno *  This code and its internal interfaces are subject to change or
3677957Sbenno *  deletion without notice.</b>
3777957Sbenno */
3877957Sbennopublic class Iterators {
3977957Sbenno
4077957Sbenno    public static <I, O> Iterator<O> createCompoundIterator(Iterable<I> inputs, Function<I, Iterator<O>> convertor) {
4177957Sbenno        return new CompoundIterator<>(inputs, convertor);
4277957Sbenno    }
4377957Sbenno
4477957Sbenno    private static class CompoundIterator<I, O> implements Iterator<O> {
4577957Sbenno
4677957Sbenno        private final Iterator<I> inputs;
4777957Sbenno        private final Function<I, Iterator<O>> convertor;
4877957Sbenno        private Iterator<O> currentIterator;
4977957Sbenno
5077957Sbenno        public CompoundIterator(Iterable<I> inputs, Function<I, Iterator<O>> convertor) {
5177957Sbenno            this.inputs = inputs.iterator();
5277957Sbenno            this.convertor = convertor;
5377957Sbenno        }
5477957Sbenno
5577957Sbenno        public boolean hasNext() {
5677957Sbenno            while (inputs.hasNext() && (currentIterator == null || !currentIterator.hasNext())) {
5777957Sbenno                currentIterator = convertor.apply(inputs.next());
5877957Sbenno            }
5977957Sbenno            return currentIterator != null && currentIterator.hasNext();
6077957Sbenno        }
61176770Sraj
6277957Sbenno        public O next() {
6377957Sbenno            if (!hasNext())
6477957Sbenno                throw new NoSuchElementException();
6577957Sbenno
6677957Sbenno            return currentIterator.next();
67134329Salc        }
68209975Snwhitehorn
69222813Sattilio        public void remove() {
70134329Salc            throw new UnsupportedOperationException();
71134329Salc        }
7290643Sbenno    }
73128540Sgrehan}
74209975Snwhitehorn