Skip to content

Instantly share code, notes, and snippets.

@vaslabs
Created February 11, 2020 16:42
Show Gist options
  • Save vaslabs/4d6240a893b0f97e24cd4523cb3f344a to your computer and use it in GitHub Desktop.
Save vaslabs/4d6240a893b0f97e24cd4523cb3f344a to your computer and use it in GitHub Desktop.
import io.vavr.control.Either;
import io.vavr.control.Try;
import java.time.Clock;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
class Snippets {
public static Either<String, Integer> toInt(String text) {
return Try.of(() -> Integer.parseInt(text)) //Try<Integer>
.toEither() //Either<Throwable, Integer>
.mapLeft(t -> "Expected an integer for days but found: " + text); //Either<Throwable, String>
}
public static Either<String, ZonedDateTime> addDaysToNow(int days, ZonedDateTime now) {
return Try.of(() -> now.plusDays(days))
.toEither()
.mapLeft(t -> "Given " + days + " exceed the date range");
}
static class UserRequest {
final String days;
final ZonedDateTime now;
UserRequest(String days, ZonedDateTime now) {
this.days = days;
this.now = now;
}
}
static class ValidatedUserInput {
final int days;
final ZonedDateTime now;
ValidatedUserInput(int days, ZonedDateTime now) {
this.days = days;
this.now = now;
}
}
public static Either<String, ZonedDateTime> serveUser(UserRequest userRequest) {
return toInt(userRequest.days) //Either<String, Integer>
.map(
days -> new ValidatedUserInput(days, userRequest.now)
) //Either<String, ValidatedUserInput>
.flatMap(
validatedInput -> addDaysToNow(validatedInput.days, validatedInput.now)
); //Either<String, ZonedDateTime>
}
public static Function<String, Function<Clock, Either<String, ZonedDateTime>>> serveUserFunction() {
return days -> //String =>
clock -> // Clock =>
serveUser(
new UserRequest(days, ZonedDateTime.now(clock))
); //Either<String, ZonedDateTime>
}
public static void main(String[] args) {
Clock clock = Clock.systemUTC();
String days = args[0];
serveUserFunction().apply(days).apply(clock);
}
}
@vaslabs
Copy link
Author

vaslabs commented Feb 11, 2020

handle null values in pojos

public class GetterLens {

   public static <T, G> Optional<G> safeGetter(T obj, Function<T, G> extractor) {
       return Optional.ofNullable(extractor.apply(obj));
   }

   public static <T, G> Optional<G> safeGetter(Optional<T> opt, Function<T, G> extractor) {
       return opt.flatMap(obj -> safeGetter(obj, extractor));
   }

   public static <A, B, C, O> Optional<O> safeGetter(
           Optional<A> opt,
           Function<A, B> fa,
           Function<B, C> fb,
           Function<C, O> fc) {
       Optional<B> b = safeGetter(opt, fa);
       Optional<C> c = safeGetter(b, fb);
       Optional<O> o = safeGetter(c, fc);
       return o;
   }

   public static <A, B, C, O> Optional<O> safeGetter(
           A a,
           Function<A, B> fa,
           Function<B, C> fb,
           Function<C, O> fc) {
       Optional<B> b = safeGetter(a, fa);
       Optional<C> c = safeGetter(b, fb);
       Optional<O> o = safeGetter(c, fc);
       return o;
   }

}

Example

Optional<O> o = safeGetter(a, A::getB, B::getC, C::getO)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment