package cn.song.kotlind.view.threetap

import rx.Subscriber
import java.util.*

/**
 * @author hzsongzhengwang
 * @date 2015/8/11
 * Copyright 2015 Six. All rights reserved.
 */

public class BoxSubscriber<T>(val child : Subscriber<in List<T>>, val size : Int) : Subscriber<T>(child){
    val buffer = ArrayList<T>(size)

    override fun onNext(t: T) {
        buffer.add(t)
        if(buffer.size() == size){
            child.onNext(ArrayList(buffer))
            buffer.remove(0)
        }
    }

    override fun onCompleted() {
        println("BoxSubscriber onCompleted()")
    }

    override fun onError(e: Throwable?) {
        println("BoxSubscriber onError()")
    }
}
