class Operations { public int add(int a, int b) { return a + b; } public int multiply(int a, int b) { return a * b; } } Operations o = new Operations(); assert o.add(10, 10) == 20 assert o.multiply(2, 10) == 20
def items = [[price: 12.90, color: 'blue'], [price: 10, color: 'red'], [price: 8, color: 'blue'], [price: 30, color: 'gray']] println FluentIterable.from(items) .filter(new Predicate<Map<String, Object>>() { @Override boolean apply(Map<String, Object> input) { return input.get("color").equalsIgnoreCase("blue") } }) .transform(new Function<Map<String, Object>, BigDecimal>() { @Override BigDecimal apply(Map<String, Object> input) { return input.get("price") } }) .toList()
Observable.from('Welcome', 'Java', 'Geeks').subscribe { print "$it " }
def items = [[price: 12.90, color: 'blue'], [price: 10, color: 'red'], [price: 8, color: 'blue'], [price: 30, color: 'gray']] def obs = Observable.from(items) .filter({ it.color == 'blue' }) .filter({ it.price < 10 }) .map({ it.price as Double }) MathObservable.sumDouble(obs).subscribe { println it }
def obs = Observable.create { subscriber -> ['Welcome', 'Java', 'Geeks'].each { if (!subscriber.unsubscribed) { subscriber.onNext it } } if (!subscriber.unsubscribed) { subscriber.onCompleted() } } obs.subscribe { print "$it " }
def obs = Observable.create({ subscriber -> Thread.start { ['Welcome', 'Java', 'Geeks'].each { if (!subscriber.unsubscribed) { subscriber.onNext it } } if (!subscriber.unsubscribed) { subscriber.onCompleted() } } } as Observable.OnSubscribe) obs.subscribe { print "$it " }
def executor = Executors.newFixedThreadPool(3) def obs = Observable.from('Welcome', 'Java', 'Geeks').flatMap { text -> Observable.from executor.submit({ text } as Callable) } obs.toBlocking().forEach { println "$it " }
def obs = Observable.from('Welcome', 'Java', 'Geeks').flatMap { text -> Async.start { text } } obs.toBlocking().forEach { print "$it " }
def obs = Observable.from('Welcome', 'Java', 'Geeks').flatMap { text -> new Command({ text }).observe() } obs.toBlocking().forEach { print "$it " }
def obs = Observable .from({ 'Welcome' }, { throw new Exception() }, { 'Geeks' }) .map { it() } def fallback = Observable .from({ 'to' }, { 'Functional' }, { 'Reactive' }, { 'Programming' }) .map { it() } obs.onErrorResumeNext(fallback).forEach { print "$it " }
interface Marvel { @RequestLine('GET /v1/public/characters?name={name}') Map<String, Object> findByName(@Named('name') String name) @RequestLine('GET /v1/public/characters/{characterId}/comics?issueNumber=1') Map<String, Object> comics(@Named('characterId') Integer characterId) } def api = MarvelFeignBuilder.forClass(Marvel) ['Wolverine', 'Hulk', 'Iron Man', 'Beast'].each { name -> def id = api.findByName(name).data.results.first().id api.comics id }
def api = MarvelFeignBuilder.forClass(Marvel) Observable.from('Wolverine', 'Hulk', 'Iron Man', 'Beast') .flatMap { name -> new Command({ api.findByName name }).observe().map { character -> def c = character.data.results.first() [id: c.id, name: c.name, description: c.description, thumbnail: "$c.thumbnail.path.$c.thumbnail.extension"] } } .flatMap { character -> new Command({ api.comics character.id }).observe().map { commic -> def c = commic.data.results.first() character << [commic: [title: c.title, price: c.prices.first().price, thumbnail: "$c.thumbnail.path.$c.thumbnail.extension"]] } } .toBlocking().forEach { println it.name }