I'm very new with RXJava. I'm defining multiple Subjects that need to do pretty much the same thing in initialization, but their generic types are different. Currently my solution looks like this:
BehaviorSubject<PepPoint> mUpdateSubject = BehaviorSubject.create();
BehaviorSubject<String> mMessageSubject = BehaviorSubject.create();
mMessageSubject.onNext(mMessage);//type string
mMessageSubject.distinctUntilChanged().skip(1).debounce(5, TimeUnit.SECONDS).sample(1, TimeUnit.MINUTES).subscribe(this::onChanged);
BehaviorSubject<Double> mTriggerRadiusSubject = BehaviorSubject.create();
mTriggerRadiusSubject.onNext(mTriggerRadius);//type double
mMessageSubject.distinctUntilChanged().skip(1).debounce(5, TimeUnit.SECONDS).sample(1, TimeUnit.MINUTES).subscribe(this::onChanged);
BehaviorSubject<SimpleLocation> observableLocation = BehaviorSubject.create();
observableLocation.onNext(mLocation);//type Location
observableLocation.distinctUntilChanged().skip(1).debounce(5, TimeUnit.SECONDS).sample(1, TimeUnit.MINUTES).subscribe(this::onChanged);
/*And same for many several other variables*/
You can see there is a lot of repetition. So what I hope to atchieve would preferably look something like this:
{
BehaviorSubject<PepPoint> mUpdateSubject = BehaviorSubject.create();
Map<Object, BehaviorSubject> subjectMap = new TreeMap<>();
subjectMap.put(mMessage, BehaviorSubject.<String>create());
subjectMap.put(mTriggerRadius, BehaviorSubject.<Double>create());
subjectMap.put(mLocation, BehaviorSubject.<Location>create());
subjectMap.put(mPreconditions, BehaviorSubject.<List<String>>create());
subjectMap.put(mLanguage, BehaviorSubject.<String>create());
for(Map.Entry<Object, BehaviorSubject> entry:subjectMap.entrySet()){
entry.getValue().onNext(entry.getKey());
mMessageSubject
.distinctUntilChanged()
.skip(1)
.debounce(5,TimeUnit.SECONDS)
.subscribe(this::onChange);
}
}
I don't understand why this does not work. I also tried
Map<? super Object, BehaviorSubject<? super Object>> subjectMap = new TreeMap<>();
Suggestions?
Thanks