0

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

Nabuska
  • 443
  • 9
  • 17

1 Answers1

0

Because I really didn't need the subjects to be in a collection, I went around the problem simply by creating a method that handles all that same thing that the for loop does.

Solution:

public void initSubjects() {
    mUpdateSubject = BehaviorSubject.create();

    initSingleSubject(mMessage, mMessageSubject = BehaviorSubject.<String>create());
    initSingleSubject(mTriggerRadius, mTriggerRadiusSubject = BehaviorSubject.<Double>create());
    initSingleSubject(mLanguage, mLanguageSubject = BehaviorSubject.<String>create());
    initSingleSubject(mPreconditions, mPreconditionsSubject = BehaviorSubject.<List<String>>create());
    initSingleSubject(mLocation, mLocationSubject = BehaviorSubject.<SimpleLocation>create());
}

private void initSingleSubject(Object value, BehaviorSubject subject){
    subject.onNext(value);
    mUpdateSubject
            .distinctUntilChanged()
            .skip(1)
            .debounce(5,TimeUnit.SECONDS)
            .subscribe(this::onUpdate);
}
Nabuska
  • 443
  • 9
  • 17