com.spotify.mobius.rx2.MobiusEffectRouter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mobius-rx2 Show documentation
Show all versions of mobius-rx2 Show documentation
RxJava2 utilities for use with Mobius
The newest version!
/*
* -\-\-
* Mobius
* --
* Copyright (c) 2017-2020 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 - 2025 Weber Informatics LLC | Privacy Policy