Last active
August 29, 2015 13:59
-
-
Save joesteele/10605549 to your computer and use it in GitHub Desktop.
Example showing RxJava for synchronous operations with no subscribers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.