All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spotify.mobius.rx2.MobiusEffectRouter Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
/*
 * -\-\-
 * Mobius
 * --
 * Copyright (c) 2017-2018 Spotify AB
 * --
 * 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 com.spotify.mobius.rx2;

import io.reactivex.Observable;
import io.reactivex.ObservableTransformer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Transformer that routes each incoming Effect descriptor to a sub-transformer associated with the
 * Effect descriptor class.
 */
class MobiusEffectRouter implements ObservableTransformer {

  private final MergedTransformer mergedTransformer;

  MobiusEffectRouter(
      Set> handledEffectClasses,
      Collection> effectPerformers) {

    final Set> effectClasses = new HashSet<>(handledEffectClasses);
    final List> immutableEffectPerformers =
        Collections.unmodifiableList(new ArrayList<>(effectPerformers));

    ObservableTransformer unhandledEffectHandler =
        new ObservableTransformer() {
          @Override
          public Observable apply(Observable effects) {
            return effects
                .filter(
                    new Predicate() {
                      @Override
                      public boolean test(F e) {
                        for (Class effectClass : effectClasses) {
                          if (effectClass.isAssignableFrom(e.getClass())) {
                            return false;
                          }
                        }
                        return true;
                      }
                    })
                .map(
                    new Function() {
                      @Override
                      public E apply(F e) {
                        throw new UnknownEffectException(e);
                      }
                    });
          }
        };
    List> allHandlers = new ArrayList<>(immutableEffectPerformers);
    allHandlers.add(unhandledEffectHandler);

    mergedTransformer = new MergedTransformer<>(allHandlers);
  }

  @Override
  public Observable apply(Observable effects) {

    return effects.compose(mergedTransformer);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy