Skip to content

eleventigers/rxeither

Repository files navigation

RxEither Build Status

Either is a value of one of two possible types.

Inspired by Jeffrey van Gogh's discussion with the Rx.Net users, RxEither is a port of Scala's Either for the RxJava world.

Usage

  • Progress reporting

    public class ProgressExample {
    
         public static void main(String[] args) {
                PublishSubject<Integer> progress = PublishSubject.create();
    
                Observable<String> results = Observable.fromCallable(() -> {
                    progress.onNext(0);
                    TimeUnit.SECONDS.sleep(1);
                    progress.onNext(100);
                    return "Hello, world!";
                }).subscribeOn(Schedulers.io());
    
                Observable<Either<Integer, String>> either = RxEither.from(progress, results).share();
    
                RxEither.filterLeft(either).subscribe(System.out::println);
                RxEither.filterRight(either).toBlocking().subscribe(System.out::println);
            }
    }

    Prints:

    0
    100
    Hello, world!
    
  • Switch

    public class SwitchActionExample {
    
        public static void main(String[] args) {
            System.out.println("Type Either a string or an Int: ");
    
            String in = "";
            Either<String, Integer> either;
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
                in = reader.readLine();
                either = Either.right(Integer.parseInt(in));
            } catch (NumberFormatException | IOException e) {
                either = Either.left(in);
            }
    
            either.fold(
                s -> {
                    System.out.println("You passed me the String: " + s);
                },
                x -> {
                    System.out.println(
                            "You passed me the Int: " + x
                                    + ", which I will increment. " + x + " + 1 = "
                                    + (x + 1));
                });
        }
    }

    Prints:

    Type Either a string or an Int:
    1
    You passed me the Int: 1, which I will increment. 1 + 1 = 2
    

Download

Maven:

<dependency>
  <groupId>net.jokubasdargis.rxeither</groupId>
  <artifactId>rxeither</artifactId>
  <version>1.2.0</version>
</dependency>

Gradle:

compile 'net.jokubasdargis.rxeither:rxeither:1.2.0'

Snapshots of the development version are available in Sonatype's snapshots repository.

License

Copyright 2016 Jokubas Dargis

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.