Skip to content

s-frick/jcontrols

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JControls

A library that provide useful functional control types.

  • Either
  • Try
  • Maybe

Add Maven Dependency

<dependency>
  <groupId>io.github.s-frick</groupId>
  <artifactId>controls</artifactId>
  <version>0.4.0</version>
</dependency>

Example Usage Either

  interface BusinessError {}
  interface BusinessObject {}
  interface Account extends BusinessObject {}
  interface User extends BusinessObject {}

  Either<BusinessError, BusinessObject> methodThatCanFail(boolean fail) {
    return fail ? Either.failure(new BusinessError() {}) : Either.success(new BusinessObject() {});
  }

  Either<BusinessError, User> getUser(Account acc) {
    return Either.success(new User() {});
  }

  void doStuff() {
    Either<BusinessError, BusinessObject> aBusinessObject = methodThatCanFail(true);

    aBusinessObject
      .map(this::toAccount)
      .flatMap(this::getUser)
      .ifPresent(this::doOtherStuff);
  }

From Try<T> to Either<BusinessError, T>

  void doStuff(){
    Try<BusinessObject> tryStuff = methodThatCanThrow();
    Either<Throwable, BusinessObject> aBusinessObject = tryStuff.toEither();

    Either<BusinessError, BusinessObject> transformed =
        aBusinessObject.mapLeft(FailureTransformer::transformFailure);
  }

If you using Java < 21

  BusinessError transformFailure(Throwable t) {
    if (t instanceof RuntimeException e) {
      return new SomeUsefulError("anError");
    }
    if (t instanceof Exception e) {
      return new OtherUsefulError("otherError");
    }
    return new BusinessError() {};
  }

If you using Java > 21

  BusinessError transformFailure2(Throwable t) {
    return switch(t) {
      case RuntimeException e -> new SomeUsefulError("anError");
      case Exception e        -> new OtherUsefulError("anError");
      default                 -> new BusinessError() {};
    };
  }

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages