/*
 * Copyright 2015-2017 OpenCB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.opencb.commons.io;

import java.util.List;

/**
 * @author Alejandro Aleman Ramos <aaleman@cipf.es>
 * @author Cristina Yenyxe Gonzalez Garcia <cyenyxe@ebi.ac.uk>
 */
@FunctionalInterface
public interface DataReader<T> {

    default boolean open() {
        return true;
    }

    default boolean close() {
        return true;
    }

    default boolean pre() {
        return true;
    }

    default boolean post() {
        return true;
    }

    default List<T> read() {
        return read(1);
    }

    List<T> read(int batchSize);
}

@@ -621,16 +621,95 @@
 ns.io;%0A%0A
+import com.google.common.base.Throwables;%0Aimport org.opencb.commons.run.Task;%0A%0A
 import j
@@ -1223,10 +1223,1982 @@
 hSize);%0A
+%0A%0A    default %3CO%3E DataReader%3CO%3E then(Task%3CT, O%3E task) %7B%0A        return new DataReader%3CO%3E() %7B%0A            @Override%0A            public boolean open() %7B%0A                return DataReader.this.open();%0A            %7D%0A%0A            @Override%0A            public boolean pre() %7B%0A                try %7B%0A                    task.pre();%0A                %7D catch (Exception e) %7B%0A                    throw Throwables.propagate(e);%0A                %7D%0A                return DataReader.this.pre();%0A            %7D%0A%0A            @Override%0A            public boolean close() %7B%0A                return DataReader.this.close();%0A            %7D%0A%0A            @Override%0A            public boolean post() %7B%0A                try %7B%0A                    task.post();%0A                %7D catch (Exception e) %7B%0A                    throw Throwables.propagate(e);%0A                %7D%0A                return DataReader.this.post();%0A            %7D%0A%0A            @Override%0A            public List%3CO%3E read(int batch) %7B%0A                try %7B%0A                    List%3CT%3E read = DataReader.this.read(batch);%0A                    // Iterate while reader has data%0A                    // Exit when reader is exhausted, or task produces data%0A                    while (read != null && !read.isEmpty()) %7B%0A                        List%3CO%3E apply = task.apply(read);%0A                        if (apply != null && !apply.isEmpty()) %7B%0A                            // Valid task apply.%0A                            return apply;%0A                        %7D else %7B%0A                            // Empty task apply. Read more data%0A                            read = DataReader.this.read(batch);%0A                        %7D%0A                    %7D%0A                    // Reader is exhausted. Drain the task.%0A                    return task.drain();%0A                %7D catch (Exception e) %7B%0A                    // TODO: Reader should throw any exception%0A                    throw Throwables.propagate(e);%0A                %7D%0A            %7D%0A        %7D;%0A    %7D%0A%0A
 %7D%0A

Add DataReader<T>.then(Task<T,O>) :> DataReader<O>