001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.util.stream.combine;
031
032import java.util.concurrent.Callable;
033import java.util.concurrent.Future;
034import java.util.concurrent.ThreadPoolExecutor;
035
036import org.openimaj.util.pair.IndependentPair;
037import org.openimaj.util.parallel.GlobalExecutorPool;
038import org.openimaj.util.stream.AbstractStream;
039import org.openimaj.util.stream.Stream;
040
041/**
042 * A stream combiner takes two streams and produces a new stream of synchronised
043 * pairs of the stream values. The two streams are consumed in two threads which
044 * the {@link #next()} method waits to complete before returning
045 * 
046 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
047 * 
048 * @param <A>
049 *            Type of payload in first stream
050 * @param <B>
051 *            Type of payload in second stream
052 */
053public class StreamCombiner<A, B> extends AbstractStream<IndependentPair<A, B>> {
054
055        private Stream<B> b;
056        private Stream<A> a;
057        private Starter<B> bstart;
058        private Starter<A> astart;
059        private ThreadPoolExecutor service;
060
061        class Starter<T> implements Callable<T> {
062
063                private Stream<T> stream;
064
065                public Starter(Stream<T> a) {
066                        stream = a;
067                }
068
069                @Override
070                public T call() throws Exception {
071                        return stream.next();
072                }
073        }
074
075        /**
076         * @param a
077         * @param b
078         */
079        public StreamCombiner(Stream<A> a, Stream<B> b) {
080                this.a = a;
081                this.b = b;
082                this.astart = new Starter<A>(this.a);
083                this.bstart = new Starter<B>(this.b);
084                this.service = GlobalExecutorPool.getPool();
085
086        }
087
088        @Override
089        public boolean hasNext() {
090                return a.hasNext() && b.hasNext();
091        }
092
093        @Override
094        public IndependentPair<A, B> next() {
095                final Future<A> futurea = this.service.submit(astart);
096                final Future<B> futureb = this.service.submit(bstart);
097                try {
098                        final A a = futurea.get();
099                        final B b = futureb.get();
100                        return IndependentPair.pair(a, b);
101                } catch (final Exception e) {
102                        e.printStackTrace();
103                }
104                return null;
105
106        }
107
108        /**
109         * Create a new {@link StreamCombiner} from the given streams
110         * 
111         * @param a
112         *            first stream
113         * @param b
114         *            second stream
115         * @return the combined stream
116         */
117        public static <A, B> StreamCombiner<A, B> combine(Stream<A> a, Stream<B> b) {
118                return new StreamCombiner<A, B>(a, b);
119        }
120
121}