Skip to content

Instantly share code, notes, and snippets.

@joesteele
Last active August 29, 2015 13:59
Show Gist options
  • Save joesteele/10605549 to your computer and use it in GitHub Desktop.
Save joesteele/10605549 to your computer and use it in GitHub Desktop.
Example showing RxJava for synchronous operations with no subscribers.
// Example of calculating effective character level from a MUD.
// Single-classed character of level 20 would be level 20.
// Multi-classed character would gain an additional level for
// every 3 levels in their non-primary classes (primary class
// is highest level class).
// e.g. Mage: 30, Ranger: 5:, Thief: 9 # Effective Level: 34
public int effectiveLevel() {
if (classes.size() == 1) {
return classes.get(0).level;
}
List<Integer> levels = Observable.from(classes).map((charClass) -> charClass.level)
.toList().toBlockingObservable().last();
int highestLevel = Collections.max(levels);
int effectiveLevels = Observable.from(levels)
.filter((level) -> level != highestLevel)
.map((level) -> level / 3)
.reduce((seed, level) -> seed + level)
.toBlockingObservable().last();
return highestLevel + effectiveLevels;
}
@joesteele
Copy link
Author

Normally you'd use RxJava in conjunction with subscribers. Also, technically since I'm using Java 8 here (lambdas etc.), I could have used the new stream API to achieve a similar result, but I am kind of liking the RxJava API better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment