package com.github.davidmoten.rx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.regex.Pattern;

import com.github.davidmoten.rx.internal.operators.OnSubscribeReader;
import com.github.davidmoten.rx.internal.operators.StringSplitOperator;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;

public final class Strings {

    private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    /**
     * Returns null if input is null otherwise returns input.toString().trim().
     */
    private static Func1<Object, String> TRIM = new Func1<Object, String>() {

        @Override
        public String call(Object input) {
            if (input == null)
                return null;
            else
                return input.toString().trim();
        }
    };

    @SuppressWarnings("unchecked")
    public static <T> Func1<T, String> trim() {
        return (Func1<T, String>) TRIM;
    }

    public static Observable<String> from(Reader reader, int bufferSize) {
        return Observable.create(new OnSubscribeReader(reader, bufferSize));
    }

    public static Observable<String> from(Reader reader) {
        return from(reader, 8192);
    }

    public static Observable<String> from(InputStream is) {
        return from(new InputStreamReader(is));
    }

    public static Observable<String> from(InputStream is, Charset charset) {
        return from(new InputStreamReader(is, charset));
    }

    public static Observable<String> from(InputStream is, Charset charset, int bufferSize) {
        return from(new InputStreamReader(is, charset), bufferSize);
    }

    public static Observable<String> split(Observable<String> source, String pattern) {
        return source.lift(new StringSplitOperator(Pattern.compile(pattern)))
                .compose(Transformers.<String> bufferEmissions());
    }

    public static Observable<String> from(File file) {
        return from(file, DEFAULT_CHARSET);
    }

    public static Observable<String> from(final File file, final Charset charset) {
        Func0<Reader> resourceFactory = new Func0<Reader>() {
            @Override
            public Reader call() {
                try {
                    return new InputStreamReader(new FileInputStream(file), charset);
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        Func1<Reader, Observable<String>> observableFactory = new Func1<Reader, Observable<String>>() {
            @Override
            public Observable<String> call(Reader is) {
                return from(is);
            }
        };
        Action1<Reader> disposeAction = new Action1<Reader>() {
            @Override
            public void call(Reader is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        return Observable.using(resourceFactory, observableFactory, disposeAction, true);
    }

}

@@ -545,16 +545,43 @@
 s.Func1;
+%0Aimport rx.functions.Func2;
 %0A%0Apublic
@@ -2108,32 +2108,690 @@
 ions());%0A    %7D%0A%0A
+    public static Observable%3CString%3E concat(Observable%3CString%3E src) %7B%0A        return strings(%0A                src.reduce(new StringBuilder(), new Func2%3CStringBuilder, String, StringBuilder%3E() %7B%0A                    @Override%0A                    public StringBuilder call(StringBuilder a, String b) %7B%0A                        return a.append(b);%0A                    %7D%0A                %7D));%0A    %7D%0A%0A    public static Observable%3CString%3E strings(Observable%3C?%3E source) %7B%0A        return source.map(new Func1%3CObject, String%3E() %7B%0A            @Override%0A            public String call(Object t) %7B%0A                return String.valueOf(t);%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     public stati

add concat