/**
 * Copyright 2016 Netflix, Inc.
 *
 * 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 rx.javafx.sources;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import rx.Observable;
import rx.observables.JavaFxObservable;

/**
 * A CompositeObservable can merge multiple Observables that can be added/removed at any time,
 * affecting all Subscribers regardless of when they subscribed. This is especially helpful for merging
 * multiple UI event sources.
 *
 * @param <T>
 */
public final class CompositeObservable<T> {

    private final ObservableList<Observable<T>> sources;
    private final int initialCapacity;

    public CompositeObservable() {
        this(-1);
    }

    public CompositeObservable(int initialCapacity) {
        this.initialCapacity = initialCapacity;
        sources = FXCollections.synchronizedObservableList(FXCollections.observableArrayList());
    }

    public Observable<T> toObservable() {
        Observable<T> updatingSource = Observable.merge(
                Observable.from(sources).flatMap(obs -> obs.takeWhile(v -> sources.contains(obs))),
                JavaFxObservable.fromObservableListAdds(sources).flatMap(obs -> obs.takeWhile(v -> sources.contains(obs)))
        );

        if (initialCapacity > 0) {
            return updatingSource.cacheWithInitialCapacity(initialCapacity);
        } else {
            return updatingSource;
        }
    }

    public void add(Observable<T> observable) {
        sources.add(observable);
    }
    public void remove(Observable<T> observable) {
        sources.remove(observable);
    }
}

@@ -692,19 +692,18 @@
 servable
-Lis
+Se
 t;%0Aimpor
@@ -758,16 +758,67 @@
 ervable;
+%0Aimport java.util.HashSet;%0Aimport java.util.Arrays;
 %0A%0A/**%0A *
@@ -1138,19 +1138,18 @@
 servable
-Lis
+Se
 t%3CObserv
@@ -1422,19 +1422,18 @@
 servable
-Lis
+Se
 t(FXColl
@@ -1454,18 +1454,27 @@
 able
-ArrayList(
+Set(new HashSet%3C%3E()
 ));%0A
@@ -1730,11 +1730,10 @@
 able
-Lis
+Se
 tAdd
@@ -2101,83 +2101,379 @@
 oid 
-remove(Observable%3CT%3E observable) %7B%0A        sources.remove(observable);%0A    %7D
+addAll(Observable%3CT%3E... observables) %7B%0A        Arrays.stream(observables).forEach(this::add);%0A    %7D%0A    public void remove(Observable%3CT%3E observable) %7B%0A        sources.remove(observable);%0A    %7D%0A    public void removeAll(Observable%3CT%3E... observables) %7B%0A        Arrays.stream(observables).forEach(this::remove);%0A    %7D%0A    public void clear() %7B%0A        sources.clear();%0A    %7D%0A
 %0A%7D%0A

add clear(), addAll(), and removeAll() to CompositeObservable