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;
import rx.functions.Func2;

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> concat(Observable<String> src) {
        return strings(
                src.reduce(new StringBuilder(), new Func2<StringBuilder, String, StringBuilder>() {
                    @Override
                    public StringBuilder call(StringBuilder a, String b) {
                        return a.append(b);
                    }
                }));
    }

    public static Observable<String> strings(Observable<?> source) {
        return source.map(new Func1<Object, String>() {
            @Override
            public String call(Object t) {
                return String.valueOf(t);
            }
        });
    }

    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);
    }

}

@@ -2180,18 +2180,160 @@
 tring%3E s
+ource) %7B%0A        return concat(source, %22%22);%0A    %7D%0A%0A    public static Observable%3CString%3E concat(Observable%3CString%3E source, final String delimite
 r
-c
 ) %7B%0A    
@@ -2369,18 +2369,21 @@
        s
+ou
 rc
+e
 .reduce(
@@ -2556,24 +2556,117 @@
 String b) %7B%0A
+                        if (a.length() %3E 0)%0A                            a.append(delimiter);%0A
             

add concat with delimiter