package com.github.davidmoten.rx;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.github.davidmoten.rx.operators.OperatorFromTransformer;

import rx.Observable;
import rx.Observable.Operator;
import rx.Observable.Transformer;
import rx.functions.Func1;

public final class Transformers {

    public static <T, R> Operator<R, T> toOperator(Transformer<T, R> transformer) {
        return OperatorFromTransformer.toOperator(transformer);
    }

    public static <T extends Number> Transformer<T, Statistics> collectStats() {
        return new Transformer<T, Statistics>() {

            @Override
            public Observable<Statistics> call(Observable<T> o) {
                return o.scan(Statistics.create(), Functions.collectStats());
            }
        };
    }

    public static <T extends Comparable<T>> Transformer<T, T> sort() {
        return new Transformer<T, T>() {

            @Override
            public Observable<T> call(Observable<T> o) {
                return o.toSortedList().flatMapIterable(Functions.<List<T>> identity());
            }
        };
    }

    public static <T extends Comparable<T>> Transformer<T, T> sort(final Comparator<T> comparator) {
        return new Transformer<T, T>() {

            @Override
            public Observable<T> call(Observable<T> o) {
                return o.toSortedList(Functions.toFunc2(comparator)).flatMapIterable(
                        Functions.<List<T>> identity());
            }
        };
    }

    public static <T> Transformer<T, Set<T>> toSet() {
        return new Transformer<T, Set<T>>() {

            @Override
            public Observable<Set<T>> call(Observable<T> o) {
                return o.toList().map(new Func1<List<T>, Set<T>>() {

                    @Override
                    public Set<T> call(List<T> list) {
                        return Collections.unmodifiableSet(new HashSet<T>(list));
                    }
                });
            }
        };
    }

}

@@ -163,76 +163,8 @@
 t;%0A%0A
-import com.github.davidmoten.rx.operators.OperatorFromTransformer;%0A%0A
 impo
@@ -274,16 +274,84 @@
 Func1;%0A%0A
+import com.github.davidmoten.rx.operators.OperatorFromTransformer;%0A%0A
 public f
@@ -432,37 +432,52 @@
 tor(
-Transformer
+Func1%3CObservable
 %3CT
+%3E
 , 
-R%3E transformer
+Observable%3CR%3E%3E function
 ) %7B%0A
@@ -530,19 +530,16 @@
 tor(
-transformer
+function
 );%0A 

change signature