├── src ├── main │ ├── resources │ │ └── .gitignore │ └── java │ │ └── demo │ │ ├── social │ │ ├── TwitterService.java │ │ ├── FacebookService.java │ │ ├── LinkedInService.java │ │ ├── TwitterProfile.java │ │ ├── FacebookProfile.java │ │ ├── LinkedInProfile.java │ │ ├── UserConnectionInfo.java │ │ └── Demo.java │ │ ├── ErrorHandlingDemo.java │ │ ├── FirstReactorStreamDemo.java │ │ ├── RxJavaOperatorsDemo.java │ │ ├── ReactorStreamOperatorsDemo.java │ │ ├── OperatorsDemo.java │ │ ├── ThreadingDemo.java │ │ ├── FirstObservableDemo.java │ │ ├── share │ │ ├── ReactorDistributingSharedStreamDemo.java │ │ ├── RxMultiStreamDemo.java │ │ ├── ReactorMultiStreamDemo.java │ │ ├── ReactorSharedStreamDemo.java │ │ └── RxSharedStreamDemo.java │ │ ├── FirstPublisherDemo.java │ │ └── LogUtils.java └── test │ └── java │ └── demo │ ├── NaivePublisherVerification.java │ ├── PublisherFactoryVerification.java │ ├── ReactorStreamVerification.java │ └── RxObservableVerification.java ├── gradle.properties ├── .gradle └── 2.5 │ └── taskArtifacts │ ├── cache.properties │ ├── fileHashes.bin │ ├── fileSnapshots.bin │ ├── taskArtifacts.bin │ ├── outputFileStates.bin │ └── cache.properties.lock ├── README.md ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradlew.bat └── gradlew /src/main/resources/.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | version=1.0.0.BUILD-SNAPSHOT 2 | -------------------------------------------------------------------------------- /.gradle/2.5/taskArtifacts/cache.properties: -------------------------------------------------------------------------------- 1 | #Wed Aug 12 15:42:39 EDT 2015 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Demos for ["Intro to Reactive Programming"](http://www.slideshare.net/rstoya05/intro-to-reactive-programming) talk. 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstoyanchev/s2gx2015-intro-to-reactive-programming/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.gradle/2.5/taskArtifacts/fileHashes.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstoyanchev/s2gx2015-intro-to-reactive-programming/HEAD/.gradle/2.5/taskArtifacts/fileHashes.bin -------------------------------------------------------------------------------- /.gradle/2.5/taskArtifacts/fileSnapshots.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstoyanchev/s2gx2015-intro-to-reactive-programming/HEAD/.gradle/2.5/taskArtifacts/fileSnapshots.bin -------------------------------------------------------------------------------- /.gradle/2.5/taskArtifacts/taskArtifacts.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstoyanchev/s2gx2015-intro-to-reactive-programming/HEAD/.gradle/2.5/taskArtifacts/taskArtifacts.bin -------------------------------------------------------------------------------- /.gradle/2.5/taskArtifacts/outputFileStates.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstoyanchev/s2gx2015-intro-to-reactive-programming/HEAD/.gradle/2.5/taskArtifacts/outputFileStates.bin -------------------------------------------------------------------------------- /.gradle/2.5/taskArtifacts/cache.properties.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstoyanchev/s2gx2015-intro-to-reactive-programming/HEAD/.gradle/2.5/taskArtifacts/cache.properties.lock -------------------------------------------------------------------------------- /src/main/java/demo/social/TwitterService.java: -------------------------------------------------------------------------------- 1 | package demo.social; 2 | 3 | import rx.Observable; 4 | 5 | public interface TwitterService { 6 | 7 | Observable getUserProfile(String id); 8 | 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | 3 | # Eclipse artifacts, including WTP generated manifests 4 | .classpath 5 | .project 6 | .settings 7 | .springBeans 8 | 9 | # IDEA artifacts and output dirs 10 | *.iml 11 | *.ipr 12 | *.iws 13 | .idea 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/demo/social/FacebookService.java: -------------------------------------------------------------------------------- 1 | package demo.social; 2 | 3 | import rx.Observable; 4 | 5 | public interface FacebookService { 6 | 7 | Observable getUserProfile(String id); 8 | 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Aug 17 17:08:45 BST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip 7 | -------------------------------------------------------------------------------- /src/main/java/demo/social/LinkedInService.java: -------------------------------------------------------------------------------- 1 | package demo.social; 2 | 3 | import rx.Observable; 4 | 5 | public interface LinkedInService { 6 | 7 | Observable findUsers(String skill); 8 | 9 | Observable getConnections(String id); 10 | 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/demo/social/TwitterProfile.java: -------------------------------------------------------------------------------- 1 | package demo.social; 2 | 3 | public class TwitterProfile { 4 | 5 | private final String id; 6 | 7 | private final String location; 8 | 9 | 10 | public TwitterProfile(String id, String location) { 11 | this.id = id; 12 | this.location = location; 13 | } 14 | 15 | 16 | public String getId() { 17 | return id; 18 | } 19 | 20 | public String getLocation() { 21 | return location; 22 | } 23 | 24 | 25 | @Override 26 | public String toString() { 27 | return "Twitter: @" + getId() + ", " + getLocation(); 28 | } 29 | 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/demo/ErrorHandlingDemo.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import reactor.rx.Streams; 6 | 7 | 8 | public class ErrorHandlingDemo { 9 | 10 | private static final Logger logger = LoggerFactory.getLogger(ErrorHandlingDemo.class); 11 | 12 | 13 | public static void main(String[] args) { 14 | 15 | try { 16 | Streams.just(1, 2, 3, 4, 5) 17 | .map(i -> { 18 | throw new NullPointerException(); 19 | }) 20 | .consume( 21 | element -> {}, 22 | error -> logger.debug("Oooh error!") 23 | ); 24 | } 25 | catch (Throwable ex) { 26 | logger.error("Crickets..."); 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/demo/social/FacebookProfile.java: -------------------------------------------------------------------------------- 1 | package demo.social; 2 | 3 | public class FacebookProfile { 4 | 5 | private final String id; 6 | 7 | private final String name; 8 | 9 | private final String bio; 10 | 11 | 12 | public FacebookProfile(String id, String name, String bio) { 13 | this.id = id; 14 | this.name = name; 15 | this.bio = bio; 16 | } 17 | 18 | 19 | public String getId() { 20 | return id; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public String getBio() { 28 | return bio; 29 | } 30 | 31 | 32 | @Override 33 | public String toString() { 34 | return "Facebook: " + getName() + ", \"" + getBio() + "\""; 35 | } 36 | 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/main/java/demo/social/LinkedInProfile.java: -------------------------------------------------------------------------------- 1 | package demo.social; 2 | 3 | public class LinkedInProfile { 4 | 5 | private final String id; 6 | 7 | private final String twitterId; 8 | 9 | private final String facebookId; 10 | 11 | 12 | public LinkedInProfile(String id, String twitterId, String facebookId) { 13 | this.id = id; 14 | this.twitterId = twitterId; 15 | this.facebookId = facebookId; 16 | } 17 | 18 | 19 | public String getId() { 20 | return id; 21 | } 22 | 23 | public String getTwitterId() { 24 | return twitterId; 25 | } 26 | 27 | public String getFacebookId() { 28 | return facebookId; 29 | } 30 | 31 | 32 | @Override 33 | public String toString() { 34 | return "LinkedIn: " + getId(); 35 | } 36 | 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/main/java/demo/FirstReactorStreamDemo.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | 4 | import java.lang.reflect.Method; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import reactor.rx.Stream; 8 | import reactor.rx.Streams; 9 | 10 | public class FirstReactorStreamDemo { 11 | 12 | 13 | public static void main(String[] args) { 14 | 15 | List items = initIntSequence(10); 16 | 17 | // Stream, 10 items 18 | 19 | // Streams.from(items).consume(System.out::println); 20 | 21 | // log() 22 | 23 | // log() + capacity(4) + consume() 24 | 25 | // log() + consumeLater() .. control.requestMore() 26 | 27 | } 28 | 29 | 30 | private static List initIntSequence(long itemCount) { 31 | List items = new ArrayList<>(); 32 | for(int i = 1; i <= itemCount ; i++){ 33 | items.add(i); 34 | } 35 | return items; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/demo/RxJavaOperatorsDemo.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import java.lang.reflect.Method; 4 | 5 | import rx.Observable; 6 | 7 | public class RxJavaOperatorsDemo { 8 | 9 | public static void main(String[] args) { 10 | 11 | System.out.println("\n\nRxJava operators:"); 12 | 13 | Observable.from(Observable.class.getMethods()) 14 | .filter(m -> Observable.class.isAssignableFrom(m.getReturnType())) 15 | .map(Method::getName) 16 | .distinct() 17 | .toSortedList() 18 | .forEach(System.out::println); 19 | 20 | System.out.println("\n\nRxJava operators via Java 8 Stream:"); 21 | 22 | java.util.stream.Stream.of(Observable.class.getMethods()) 23 | .filter(m -> Observable.class.isAssignableFrom(m.getReturnType())) 24 | .map(Method::getName) 25 | .distinct() 26 | .sorted() 27 | .forEach(s -> System.out.print(s + " ")); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/demo/ReactorStreamOperatorsDemo.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import java.lang.reflect.Method; 4 | 5 | import reactor.rx.Stream; 6 | import reactor.rx.Streams; 7 | 8 | public class ReactorStreamOperatorsDemo { 9 | 10 | 11 | public static void main(String[] args) { 12 | 13 | System.out.println("Reactor Stream operators:"); 14 | 15 | Streams.from(Stream.class.getMethods()) 16 | .filter(m -> Stream.class.isAssignableFrom(m.getReturnType())) 17 | .map(Method::getName) 18 | .distinct() 19 | .sort() 20 | .consume(s -> System.out.print(s + " ")); 21 | 22 | System.out.println("\n\nReactor operators via Java 8 Stream:"); 23 | 24 | java.util.stream.Stream.of(Stream.class.getMethods()) 25 | .filter(m -> Stream.class.isAssignableFrom(m.getReturnType())) 26 | .map(Method::getName) 27 | .distinct() 28 | .sorted() 29 | .forEach(s -> System.out.print(s + " ")); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/demo/NaivePublisherVerification.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.reactivestreams.Publisher; 4 | import org.reactivestreams.tck.PublisherVerification; 5 | import org.reactivestreams.tck.TestEnvironment; 6 | import org.testng.SkipException; 7 | 8 | public class NaivePublisherVerification extends PublisherVerification { 9 | 10 | 11 | public NaivePublisherVerification() { 12 | super(new TestEnvironment(500, true)); 13 | } 14 | 15 | 16 | @Override 17 | public Publisher createPublisher(long elementCount) { 18 | 19 | if(elementCount > 100L) { 20 | throw new SkipException("Large Publisher Not implemented"); 21 | } 22 | 23 | return FirstPublisherDemo.createNaivePublisher(elementCount); 24 | } 25 | 26 | 27 | @Override 28 | public Publisher createFailedPublisher() { 29 | throw new SkipException("Not implemented"); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/demo/PublisherFactoryVerification.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.reactivestreams.Publisher; 4 | import org.reactivestreams.tck.PublisherVerification; 5 | import org.reactivestreams.tck.TestEnvironment; 6 | import org.testng.SkipException; 7 | 8 | public class PublisherFactoryVerification extends PublisherVerification { 9 | 10 | 11 | public PublisherFactoryVerification() { 12 | super(new TestEnvironment(500, true)); 13 | } 14 | 15 | 16 | @Override 17 | public Publisher createPublisher(long elementCount) { 18 | 19 | if(elementCount > 100L) { 20 | throw new SkipException("Large Publisher Not implemented"); 21 | } 22 | 23 | return FirstPublisherDemo.createWithPublisherFactory(elementCount); 24 | } 25 | 26 | @Override 27 | public Publisher createFailedPublisher() { 28 | throw new SkipException("Not implemented"); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/demo/social/UserConnectionInfo.java: -------------------------------------------------------------------------------- 1 | package demo.social; 2 | 3 | public class UserConnectionInfo { 4 | 5 | private final String name; 6 | 7 | private final LinkedInProfile linkedInProfile; 8 | 9 | private final TwitterProfile twitterProfile; 10 | 11 | private final FacebookProfile facebookProfile; 12 | 13 | 14 | public UserConnectionInfo(String n, LinkedInProfile lp, TwitterProfile tp, FacebookProfile fp) { 15 | name = n; 16 | linkedInProfile = lp; 17 | twitterProfile = tp; 18 | facebookProfile = fp; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public LinkedInProfile getLinkedInProfile() { 26 | return linkedInProfile; 27 | } 28 | 29 | public TwitterProfile getTwitterProfile() { 30 | return twitterProfile; 31 | } 32 | 33 | public FacebookProfile getFacebookProfile() { 34 | return facebookProfile; 35 | } 36 | 37 | 38 | public String toString() { 39 | return "UserConnectionInfo {\n" + 40 | "\t" + name + "\n" + 41 | "\t\t" + linkedInProfile + "\n" + 42 | "\t\t" + twitterProfile + "\n" + 43 | "\t\t" + facebookProfile + "\n" + 44 | "}\n"; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/demo/ReactorStreamVerification.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.reactivestreams.Publisher; 7 | import org.reactivestreams.tck.PublisherVerification; 8 | import org.reactivestreams.tck.TestEnvironment; 9 | import org.testng.SkipException; 10 | import reactor.rx.Streams; 11 | 12 | public class ReactorStreamVerification extends PublisherVerification { 13 | 14 | 15 | public ReactorStreamVerification() { 16 | super(new TestEnvironment(500, true)); 17 | } 18 | 19 | 20 | @Override 21 | public Publisher createPublisher(long elementCount) { 22 | 23 | if(elementCount > 100L) { 24 | throw new SkipException("Large Publisher Not implemented"); 25 | } 26 | 27 | List items = new ArrayList<>(); 28 | for(int i = 1; i <= elementCount ; i++){ 29 | items.add(i); 30 | } 31 | 32 | return Streams.from(items); 33 | } 34 | 35 | @Override 36 | public Publisher createFailedPublisher() { 37 | throw new SkipException("Not implemented"); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/demo/OperatorsDemo.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import java.lang.reflect.Method; 4 | import java.util.ArrayList; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import reactor.rx.Stream; 9 | import reactor.rx.Streams; 10 | import rx.Observable; 11 | 12 | public class OperatorsDemo { 13 | 14 | private static final Logger logger= LoggerFactory.getLogger(OperatorsDemo.class); 15 | 16 | 17 | public static void main(String[] args) { 18 | 19 | logger.debug("\n\nRxJava operators:"); 20 | 21 | Observable.from(Observable.class.getMethods()) 22 | .filter(m -> Observable.class.isAssignableFrom(m.getReturnType())) 23 | .map(Method::getName) 24 | .distinct() 25 | .toSortedList() 26 | .forEach(System.out::println); 27 | 28 | logger.debug("\n\nRxJava operators (using Java 8 Stream):"); 29 | 30 | java.util.stream.Stream.of(Observable.class.getMethods()) 31 | .filter(m -> Observable.class.isAssignableFrom(m.getReturnType())) 32 | .map(Method::getName) 33 | .distinct() 34 | .sorted() 35 | .forEach(s -> System.out.print(s + " ")); 36 | 37 | logger.debug("\n\nReactor Stream operators:"); 38 | 39 | Streams.from(Stream.class.getMethods()) 40 | .filter(m -> Stream.class.isAssignableFrom(m.getReturnType())) 41 | .map(Method::getName) 42 | .distinct() 43 | .sort() 44 | .consume(s -> System.out.print(s + " ")); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/demo/ThreadingDemo.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import java.io.IOException; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import rx.Observable; 8 | import rx.Subscriber; 9 | import rx.schedulers.Schedulers; 10 | 11 | import static demo.LogUtils.rxLog; 12 | 13 | public class ThreadingDemo { 14 | 15 | private static Logger logger = LoggerFactory.getLogger(ThreadingDemo.class); 16 | 17 | 18 | public static void main(String[] args) throws IOException { 19 | 20 | Observable.just(1, 2, 3) 21 | .compose(rxLog("just", "subscribeOn")) 22 | .observeOn(Schedulers.computation()) 23 | .compose(rxLog("subscribeOn", "subscribe")) 24 | .subscribe(createSubscriber()); 25 | 26 | System.in.read(); 27 | 28 | } 29 | 30 | 31 | @SuppressWarnings("unused") 32 | private static Subscriber createSubscriber() { 33 | 34 | return new Subscriber() { 35 | 36 | 37 | @Override 38 | public void onStart() { 39 | logger.debug("onStart"); 40 | request(1); 41 | } 42 | 43 | @Override 44 | public void onNext(Integer data) { 45 | logger.debug("onNext: " + String.valueOf(data)); 46 | request(1); 47 | } 48 | 49 | @Override 50 | public void onError(Throwable error) { 51 | logger.debug("onError: " + error); 52 | } 53 | 54 | @Override 55 | public void onCompleted() { 56 | logger.debug("onComplete"); 57 | } 58 | }; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/demo/FirstObservableDemo.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import rx.Observable; 8 | import rx.Subscriber; 9 | 10 | public class FirstObservableDemo { 11 | 12 | 13 | public static void main(String[] args) { 14 | 15 | List items = initIntSequence(10); 16 | 17 | // Observable, 10 items 18 | 19 | Observable.from(items).subscribe(System.out::println); 20 | 21 | // src/test/java -> RxObservableVerification 22 | 23 | // log demand with doOnRequest 24 | 25 | // back-pressure: experiment with request(n) in createSubscriber 26 | 27 | // Observable, 10000 items 28 | 29 | } 30 | 31 | private static List initIntSequence(long itemCount) { 32 | List items = new ArrayList<>(); 33 | for(int i = 1; i <= itemCount ; i++){ 34 | items.add(i); 35 | } 36 | return items; 37 | } 38 | 39 | @SuppressWarnings("unused") 40 | private static Subscriber createSubscriber() { 41 | 42 | return new Subscriber() { 43 | 44 | 45 | @Override 46 | public void onStart() { 47 | System.out.println("onStart"); 48 | request(1); 49 | } 50 | 51 | @Override 52 | public void onNext(Integer data) { 53 | System.out.println(data); 54 | request(1); 55 | } 56 | 57 | @Override 58 | public void onError(Throwable error) { 59 | System.out.println("onError: " + error); 60 | } 61 | 62 | @Override 63 | public void onCompleted() { 64 | System.out.println("onComplete"); 65 | } 66 | }; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/demo/share/ReactorDistributingSharedStreamDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package demo.share; 17 | 18 | import java.io.IOException; 19 | 20 | import org.slf4j.Logger; 21 | import org.slf4j.LoggerFactory; 22 | import reactor.Environment; 23 | import reactor.core.processor.RingBufferWorkProcessor; 24 | import reactor.rx.Stream; 25 | import reactor.rx.Streams; 26 | 27 | /** 28 | * Shared stream with point-to-point, i.e. each element to one subscriber only. 29 | */ 30 | public class ReactorDistributingSharedStreamDemo { 31 | 32 | private static Logger logger = LoggerFactory.getLogger(ReactorDistributingSharedStreamDemo.class); 33 | 34 | 35 | public static void main(String[] args) throws IOException { 36 | 37 | Environment.initialize(); 38 | 39 | Stream stream = Streams.period(1).process(RingBufferWorkProcessor.create());; 40 | 41 | stream.consume(n -> logger.debug("\t A[{}]", n)); 42 | stream.consume(n -> logger.debug("\t\t\t B[{}]", n)); 43 | 44 | System.in.read(); 45 | 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/demo/RxObservableVerification.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.reactivestreams.Publisher; 7 | import org.reactivestreams.tck.PublisherVerification; 8 | import org.reactivestreams.tck.TestEnvironment; 9 | import org.testng.SkipException; 10 | import rx.Observable; 11 | import rx.RxReactiveStreams; 12 | 13 | public class RxObservableVerification extends PublisherVerification { 14 | 15 | 16 | public RxObservableVerification() { 17 | super(new TestEnvironment(500, true)); 18 | } 19 | 20 | 21 | @Override 22 | public Publisher createPublisher(long elementCount) { 23 | 24 | if(elementCount > 100L) { 25 | throw new SkipException("Large Publisher Not implemented"); 26 | } 27 | 28 | List items = new ArrayList<>(); 29 | for(int i = 1; i <= elementCount ; i++){ 30 | items.add(i); 31 | } 32 | 33 | return RxReactiveStreams.toPublisher(Observable.from(items)); 34 | } 35 | 36 | @Override 37 | public Publisher createFailedPublisher() { 38 | 39 | // Null because we always successfully subscribe. 40 | // If the observable is in error state, it will subscribe and then emit the error as the first item 41 | // This is not an “error state” publisher as defined by RS 42 | 43 | // Source for above comment: 44 | // https://github.com/ReactiveX/RxJavaReactiveStreams/blob/d581cdf1768db20c8f81a6661c71bc68b860f51b/rxjava-reactive-streams/src/test/java/rx/reactivestreams/TckSynchronousPublisherTest.java#L41-L44 45 | 46 | return null; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/demo/share/RxMultiStreamDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package demo.share; 17 | 18 | import java.io.IOException; 19 | import java.util.concurrent.TimeUnit; 20 | 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | import rx.Observable; 24 | 25 | /** 26 | * Each subscriber with independent stream. 27 | */ 28 | public class RxMultiStreamDemo { 29 | 30 | private static Logger logger = LoggerFactory.getLogger(RxMultiStreamDemo.class); 31 | 32 | 33 | public static void main(String[] args) throws IOException { 34 | 35 | Observable observable = Observable.interval(1, TimeUnit.SECONDS); 36 | 37 | observable.subscribe(n -> logger.debug("\t A[{}]", n)); 38 | 39 | // 2nd subscriber starts 5 seconds later to emphasize independent streams 40 | sleep(5); 41 | 42 | observable.subscribe(n -> logger.debug("\t\t\t B[{}]", n)); 43 | 44 | System.in.read(); 45 | 46 | } 47 | 48 | private static void sleep(long seconds) { 49 | try { 50 | Thread.sleep(seconds * 1000); 51 | } 52 | catch (InterruptedException e) { 53 | logger.debug("Interrupted..."); 54 | } 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/demo/share/ReactorMultiStreamDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package demo.share; 17 | 18 | import java.io.IOException; 19 | 20 | import org.slf4j.Logger; 21 | import org.slf4j.LoggerFactory; 22 | import reactor.Environment; 23 | import reactor.rx.Stream; 24 | import reactor.rx.Streams; 25 | 26 | /** 27 | * Each subscriber with independent stream. 28 | */ 29 | public class ReactorMultiStreamDemo { 30 | 31 | private static Logger logger = LoggerFactory.getLogger(ReactorMultiStreamDemo.class); 32 | 33 | 34 | public static void main(String[] args) throws IOException { 35 | 36 | Environment.initialize(); 37 | 38 | Stream stream = Streams.period(1); 39 | 40 | stream.consume(n -> logger.debug("\t A[{}]", n)); 41 | 42 | // 2nd subscriber starts 5 seconds later to emphasize independent streams 43 | sleep(5); 44 | 45 | stream.consume(n -> logger.debug("\t\t\t B[{}]", n)); 46 | 47 | System.in.read(); 48 | 49 | } 50 | 51 | private static void sleep(long seconds) { 52 | try { 53 | Thread.sleep(seconds * 1000); 54 | } 55 | catch (InterruptedException e) { 56 | logger.debug("Interrupted..."); 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/demo/share/ReactorSharedStreamDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package demo.share; 17 | 18 | import java.io.IOException; 19 | 20 | import org.slf4j.Logger; 21 | import org.slf4j.LoggerFactory; 22 | import reactor.Environment; 23 | import reactor.core.processor.RingBufferProcessor; 24 | import reactor.rx.Stream; 25 | import reactor.rx.Streams; 26 | 27 | /** 28 | * Shared stream with fan-out, i.e. each element to each Subscriber. 29 | */ 30 | public class ReactorSharedStreamDemo { 31 | 32 | private static Logger logger = LoggerFactory.getLogger(ReactorSharedStreamDemo.class); 33 | 34 | 35 | public static void main(String[] args) throws IOException { 36 | 37 | Environment.initialize(); 38 | 39 | Stream stream = Streams.period(1).process(RingBufferProcessor.create());; 40 | 41 | stream.consume(n -> logger.debug("\t A[{}]", n)); 42 | 43 | // 2nd subscriber will miss a few events (try with and without this line...) 44 | sleep(5); 45 | 46 | stream.consume(n -> logger.debug("\t\t\t B[{}]", n)); 47 | 48 | System.in.read(); 49 | 50 | } 51 | 52 | private static void sleep(long seconds) { 53 | try { 54 | Thread.sleep(seconds * 1000); 55 | } 56 | catch (InterruptedException e) { 57 | logger.debug("Interrupted..."); 58 | } 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/demo/share/RxSharedStreamDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package demo.share; 17 | 18 | import java.io.IOException; 19 | import java.util.concurrent.TimeUnit; 20 | 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | import rx.Observable; 24 | 25 | /** 26 | * Shared stream with fan-out, i.e. each element to each Observer. 27 | */ 28 | public class RxSharedStreamDemo { 29 | 30 | private static Logger logger = LoggerFactory.getLogger(RxSharedStreamDemo.class); 31 | 32 | 33 | public static void main(String[] args) throws IOException { 34 | 35 | Observable observable = Observable.interval(1, TimeUnit.SECONDS).share(); 36 | 37 | observable.subscribe(n -> logger.debug("\t A[{}]", n)); 38 | 39 | // 2nd subscriber will miss a few events (try with and without this line...) 40 | sleep(5); 41 | 42 | observable.subscribe(n -> logger.debug("\t\t\t B[{}]", n)); 43 | 44 | System.in.read(); 45 | 46 | // Slow consumer: add sleep(5) in 2nd subscribe action 47 | 48 | // Decouple slow consumer: add observeOn(Schedulers.computation() before 2nd subscribe 49 | 50 | } 51 | 52 | private static void sleep(long seconds) { 53 | try { 54 | Thread.sleep(seconds * 1000); 55 | } 56 | catch (InterruptedException e) { 57 | logger.debug("Interrupted..."); 58 | } 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/main/java/demo/FirstPublisherDemo.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | import org.reactivestreams.Publisher; 8 | import org.reactivestreams.Subscriber; 9 | import org.reactivestreams.Subscription; 10 | import reactor.core.reactivestreams.PublisherFactory; 11 | 12 | 13 | public class FirstPublisherDemo { 14 | 15 | 16 | public static void main(String[] args) { 17 | 18 | // createNaivePublisher, 10 items (ok!) 19 | 20 | createNaivePublisher(10).subscribe(createSubscriber()); 21 | 22 | // src/test/java -> NaivePublisherVerification (fail!) 23 | 24 | // src/test/java -> PublisherFactoryVerification (ok!) 25 | 26 | // see createWithPublisherFactory 27 | 28 | // back-pressure: experiment w/ request(n) in createSubscriber... 29 | 30 | // createNaivePublisher, 10000 items (StackOverflow!) 31 | 32 | // createWithPublisherFactory, 10000 items (ok!) 33 | 34 | } 35 | 36 | 37 | static Publisher createNaivePublisher(long itemCount) { 38 | 39 | final List items = initIntSequence(itemCount); 40 | 41 | return new Publisher() { 42 | 43 | @Override 44 | public void subscribe(final Subscriber subscriber) { 45 | 46 | subscriber.onSubscribe(new Subscription() { 47 | 48 | private Iterator iterator = items.iterator(); 49 | 50 | @Override 51 | public void request(long n) { 52 | for (int i = 0; i < n; i++) { 53 | if (this.iterator.hasNext()) { 54 | subscriber.onNext(this.iterator.next()); 55 | } 56 | else { 57 | subscriber.onComplete(); 58 | } 59 | } 60 | } 61 | 62 | @Override 63 | public void cancel() { 64 | } 65 | }); 66 | } 67 | }; 68 | } 69 | 70 | static Publisher createWithPublisherFactory(long itemCount) { 71 | 72 | final List items = initIntSequence(itemCount); 73 | 74 | return PublisherFactory.forEach((subscriber) -> { 75 | Iterator iterator = subscriber.context(); 76 | if (iterator.hasNext()) { 77 | subscriber.onNext(iterator.next()); 78 | } 79 | else { 80 | subscriber.onComplete(); 81 | } 82 | }, subscriber -> items.iterator()); 83 | } 84 | 85 | private static List initIntSequence(long itemCount) { 86 | List items = new ArrayList<>(); 87 | for(int i = 1; i <= itemCount ; i++){ 88 | items.add(i); 89 | } 90 | return items; 91 | } 92 | 93 | private static Subscriber createSubscriber() { 94 | 95 | return new Subscriber() { 96 | 97 | private Subscription subscription; 98 | 99 | @Override 100 | public void onSubscribe(Subscription subscription) { 101 | System.out.println("onSubscribe"); 102 | this.subscription = subscription; 103 | this.subscription.request(1); 104 | } 105 | 106 | @Override 107 | public void onNext(Integer data) { 108 | System.out.println(data); 109 | this.subscription.request(1); 110 | } 111 | 112 | @Override 113 | public void onError(Throwable error) { 114 | System.out.println("onError: " + error); 115 | } 116 | 117 | @Override 118 | public void onComplete() { 119 | System.out.println("onComplete"); 120 | } 121 | }; 122 | } 123 | 124 | } 125 | -------------------------------------------------------------------------------- /src/main/java/demo/LogUtils.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import reactor.fn.Supplier; 6 | import reactor.rx.action.Action; 7 | import rx.Notification; 8 | import rx.Observable; 9 | 10 | public class LogUtils { 11 | 12 | private static Logger logger = LoggerFactory.getLogger(LogUtils.class); 13 | 14 | 15 | /** 16 | * Helps append signal logging to a Reactor processing chain, e.g.: 17 | *
 18 | 	 * Streams.just(1)
 19 | 	 * 	.lift(reactorLog("just", "consume")
 20 | 	 * 	.consume(...);
 21 | 	 * 
22 | * 23 | * @param up label for the upstream Publisher 24 | * @param down label of the downstream Subscriber 25 | */ 26 | public static Supplier> reactorLog(String up, String down) { 27 | return () -> new LoggerAction<>(up, down); 28 | } 29 | 30 | /** 31 | * Helps to append signal logging to an Rx Observable chain, e.g.: 32 | *
 33 | 	 * Observable.just(1)
 34 | 	 *	.compose(rxLog("just", "subscribe"))
 35 | 	 *	.subscribe(...);
 36 | 	 * 
37 | * 38 | * @param up label for the upstream Observable 39 | * @param down label of the downstream Observable 40 | */ 41 | public static LogTransformer rxLog(String up, String down) { 42 | return new LogTransformer<>(up, down); 43 | } 44 | 45 | public static String demandToString(long demand) { 46 | return (demand == Long.MAX_VALUE ? "request(unbounded)" : "request(" + String.valueOf(demand) + ")"); 47 | } 48 | 49 | 50 | private static class LogTransformer implements Observable.Transformer { 51 | 52 | private final String up; 53 | 54 | private final String down; 55 | 56 | 57 | public LogTransformer(String up, String down) { 58 | this.up = " [" + up + " <-- " + down + "] {}"; 59 | this.down = " [" + up + " --> " + down + "] {}"; 60 | } 61 | 62 | @Override 63 | public Observable call(Observable observable) { 64 | return observable 65 | .doOnRequest(n -> logger.debug(this.up, demandToString(n))) 66 | .doOnUnsubscribe(() -> logger.debug(this.up, "cancel")) 67 | .doOnEach((n) -> logger.debug(this.down, signalToString(n))); 68 | } 69 | 70 | protected static String signalToString(Notification n) { 71 | return n.getKind() + (n.hasValue() ? ": " + n.getValue() : ""); 72 | } 73 | } 74 | 75 | private static class LoggerAction extends Action { 76 | 77 | private final String up; 78 | 79 | private final String down; 80 | 81 | 82 | public LoggerAction(String up, String down) { 83 | this.up = " [" + up + " <-- " + down + "] {}"; 84 | this.down = " [" + up + " --> " + down + "] {}"; 85 | } 86 | 87 | @Override 88 | public void requestMore(long n) { 89 | logger.debug(this.up, demandToString(n)); 90 | super.requestMore(n); 91 | } 92 | 93 | @Override 94 | public void cancel() { 95 | logger.debug(this.up, "cancel"); 96 | super.cancel(); 97 | } 98 | 99 | @Override 100 | protected void doNext(T data) { 101 | logger.debug(this.down, "onNext: " + data); 102 | broadcastNext(data); 103 | } 104 | 105 | @Override 106 | protected void doError(Throwable error) { 107 | logger.debug(this.down, "onError: " + error); 108 | super.doError(error); 109 | } 110 | 111 | @Override 112 | protected void doComplete() { 113 | logger.debug(this.down, "onComplete"); 114 | super.doComplete(); 115 | } 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /src/main/java/demo/social/Demo.java: -------------------------------------------------------------------------------- 1 | package demo.social; 2 | 3 | import java.util.HashMap; 4 | import java.util.LinkedHashSet; 5 | import java.util.Map; 6 | import java.util.Set; 7 | 8 | import rx.Observable; 9 | 10 | 11 | public class Demo { 12 | 13 | private static final TestTwitterService twitterService = new TestTwitterService(); 14 | 15 | private static final TestFacebookService facebookService = new TestFacebookService(); 16 | 17 | private static final TestLinkedInService linkedInService = new TestLinkedInService(); 18 | 19 | 20 | static { 21 | twitterService.add("jdenham", "Mongtomery AL"); 22 | twitterService.add("josorio", "Huntington NY"); 23 | twitterService.add("cfeliciano", "San Diago CA"); 24 | twitterService.add("moden", "Detroit MI"); 25 | twitterService.add("lconrad", "Portland OR"); 26 | 27 | facebookService.add("jdenham", "Janelle Denham", "Pop culture lover. Wannabe coffee expert. Beer ninja. Passionate internet aficionado. Introvert."); 28 | facebookService.add("josorio", "Justina Osorio", "Freelance communicator. Web maven. Internet specialist. Bacon expert. Falls down a lot. Student. Twitter ninja. Avid food advocate."); 29 | facebookService.add("cfeliciano", "Clemmie Feliciano", "Music geek. Friendly writer. Prone to fits of apathy. Extreme travel enthusiast. Organizer."); 30 | facebookService.add("moden", "Major Oden", "Passionate web scholar. Prone to fits of apathy. Twitter fanatic. Proud troublemaker. General bacon aficionado."); 31 | facebookService.add("lconrad", "Lamonica Conrad", "Incurable webaholic. Hardcore organizer. Wannabe music maven. Friendly tv expert. Gamer. Unapologetic beer evangelist. Internet fan."); 32 | 33 | linkedInService.add("tina_nelson", new LinkedInProfile("jannel_denham", "jdenham", "jdenham")); 34 | linkedInService.add("tina_nelson", new LinkedInProfile("justina_osorio", "josorio", "josorio")); 35 | linkedInService.add("tina_nelson", new LinkedInProfile("clemmie_feliciano", "cfeliciano", "cfeliciano")); 36 | linkedInService.add("tina_nelson", new LinkedInProfile("major_oden", "moden", "moden")); 37 | linkedInService.add("tina_nelson", new LinkedInProfile("lamonica_conrad", "lconrad", "lconrad")); 38 | } 39 | 40 | 41 | public static void main(String[] args) { 42 | 43 | linkedInService.findUsers("reactive").take(5).flatMap(userName -> 44 | 45 | linkedInService.getConnections(userName).take(3).flatMap(connection -> { 46 | 47 | String id = connection.getTwitterId(); 48 | Observable twitterProfile = twitterService.getUserProfile(id); 49 | 50 | id = connection.getFacebookId(); 51 | Observable facebookProfile = facebookService.getUserProfile(id); 52 | 53 | return Observable.zip(twitterProfile, facebookProfile, (tp, fp) -> 54 | new UserConnectionInfo(userName, connection, tp, fp)); 55 | 56 | }) 57 | ) 58 | .subscribe(System.out::println); 59 | } 60 | 61 | 62 | /** 63 | * Test LinkedIn service. 64 | */ 65 | private static class TestLinkedInService implements LinkedInService { 66 | 67 | private final Map> map = new HashMap<>(); 68 | 69 | 70 | public TestLinkedInService add(String id, LinkedInProfile connection) { 71 | Set set = map.get(id); 72 | if (set == null) { 73 | set = new LinkedHashSet<>(); 74 | map.put(id, new LinkedHashSet<>()); 75 | } 76 | set.add(connection); 77 | return this; 78 | } 79 | 80 | @Override 81 | public Observable findUsers(String skill) { 82 | return Observable.from(map.keySet()); 83 | } 84 | 85 | @Override 86 | public Observable getConnections(String id) { 87 | return (map.containsKey(id) ? Observable.from(map.get(id)) : Observable.empty()); 88 | } 89 | } 90 | 91 | /** 92 | * Test Twitter service. 93 | */ 94 | private static class TestTwitterService implements TwitterService { 95 | 96 | private final Map map = new HashMap<>(); 97 | 98 | 99 | public TestTwitterService add(String id, String location) { 100 | map.put(id, new TwitterProfile(id, location)); 101 | return this; 102 | } 103 | 104 | @Override 105 | public Observable getUserProfile(String id) { 106 | return (map.containsKey(id) ? Observable.just(map.get(id)) : Observable.empty()); 107 | } 108 | } 109 | 110 | /** 111 | * Test Facebook service. 112 | */ 113 | private static class TestFacebookService implements FacebookService { 114 | 115 | private final Map map = new HashMap<>(); 116 | 117 | 118 | public TestFacebookService add(String id, String name, String bio) { 119 | map.put(id, new FacebookProfile(id, name, bio)); 120 | return this; 121 | } 122 | 123 | @Override 124 | public Observable getUserProfile(String id) { 125 | return (map.containsKey(id) ? Observable.just(map.get(id)) : Observable.empty()); 126 | } 127 | } 128 | 129 | } 130 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | --------------------------------------------------------------------------------