/** * Map the right most value and return a new Either reflecting the new types. * * @param rFunc Function that maps the right value if present. * @param <T> New type of right value. * @return New Either bound to the same left type and the new right type. */ public <T> Either<L, T> mapRight(Function<? super R, ? extends T> rFunc) { return new Either<>(left, right.map(rFunc)); }
private SdkEventLoopGroup eventLoopGroup(DefaultBuilder builder) { Validate.isTrue(builder.eventLoopGroup == null || builder.eventLoopGroupBuilder == null, "The eventLoopGroup and the eventLoopGroupFactory can't both be configured."); return Either.fromNullable(builder.eventLoopGroup, builder.eventLoopGroupBuilder) .map(e -> e.map(this::nonManagedEventLoopGroup, SdkEventLoopGroup.Builder::build)) .orElseGet(SharedSdkEventLoopGroup::get); }
/** * Create a new {@code Optional<Either>} from two possibly null values. * * If both values are null, {@link Optional#empty()} is returned. Only one of the left or right values * is allowed to be non-null, otherwise an {@link IllegalArgumentException} is thrown. * @param left The left value (possibly null) * @param right The right value (possibly null) * @param <L> Left type * @param <R> Right type * @return an Optional Either representing one of the two values or empty if both are null */ public static <L, R> Optional<Either<L, R>> fromNullable(L left, R right) { if (left != null && right == null) { return Optional.of(left(left)); } if (left == null && right != null) { return Optional.of(right(right)); } if (left == null && right == null) { return Optional.empty(); } throw new IllegalArgumentException(String.format("Only one of either left or right should be non-null. " + "Got (left: %s, right: %s)", left, right)); }
private SdkEventLoopGroup eventLoopGroup(DefaultBuilder builder) { Validate.isTrue(builder.eventLoopGroup == null || builder.eventLoopGroupBuilder == null, "The eventLoopGroup and the eventLoopGroupFactory can't both be configured."); return Either.fromNullable(builder.eventLoopGroup, builder.eventLoopGroupBuilder) .map(e -> e.map(this::nonManagedEventLoopGroup, SdkEventLoopGroup.Builder::build)) .orElseGet(SharedSdkEventLoopGroup::get); }
/** * Create a new {@code Optional<Either>} from two possibly null values. * * If both values are null, {@link Optional#empty()} is returned. Only one of the left or right values * is allowed to be non-null, otherwise an {@link IllegalArgumentException} is thrown. * @param left The left value (possibly null) * @param right The right value (possibly null) * @param <L> Left type * @param <R> Right type * @return an Optional Either representing one of the two values or empty if both are null */ public static <L, R> Optional<Either<L, R>> fromNullable(L left, R right) { if (left != null && right == null) { return Optional.of(left(left)); } if (left == null && right != null) { return Optional.of(right(right)); } if (left == null && right == null) { return Optional.empty(); } throw new IllegalArgumentException(String.format("Only one of either left or right should be non-null. " + "Got (left: %s, right: %s)", left, right)); }
/** * Finalize which sync HTTP client will be used for the created client. */ private SdkHttpClient resolveSyncHttpClient(SdkClientConfiguration config) { Validate.isTrue(config.option(SdkClientOption.SYNC_HTTP_CLIENT) == null || httpClientBuilder == null, "The httpClient and the httpClientBuilder can't both be configured."); return Either.fromNullable(config.option(SdkClientOption.SYNC_HTTP_CLIENT), httpClientBuilder) .map(e -> e.map(NonManagedSdkHttpClient::new, b -> b.buildWithDefaults(childHttpConfig()))) .orElseGet(() -> defaultHttpClientBuilder.buildWithDefaults(childHttpConfig())); }
/** * Map the left most value and return a new Either reflecting the new types. * * @param lFunc Function that maps the left value if present. * @param <T> New type of left value. * @return New Either bound to the new left type and the same right type. */ public <T> Either<T, R> mapLeft(Function<? super L, ? extends T> lFunc) { return new Either<>(left.map(lFunc), right); }
/** * Finalize which async HTTP client will be used for the created client. */ private SdkAsyncHttpClient resolveAsyncHttpClient(SdkClientConfiguration config) { Validate.isTrue(config.option(ASYNC_HTTP_CLIENT) == null || asyncHttpClientBuilder == null, "The asyncHttpClient and the asyncHttpClientBuilder can't both be configured."); return Either.fromNullable(config.option(ASYNC_HTTP_CLIENT), asyncHttpClientBuilder) .map(e -> e.map(NonManagedSdkAsyncHttpClient::new, b -> b.buildWithDefaults(childHttpConfig()))) .orElseGet(() -> defaultAsyncHttpClientBuilder.buildWithDefaults(childHttpConfig())); }
/** * Map the right most value and return a new Either reflecting the new types. * * @param rFunc Function that maps the right value if present. * @param <T> New type of right value. * @return New Either bound to the same left type and the new right type. */ public <T> Either<L, T> mapRight(Function<? super R, ? extends T> rFunc) { return new Either<>(left, right.map(rFunc)); }
/** * Map the left most value and return a new Either reflecting the new types. * * @param lFunc Function that maps the left value if present. * @param <T> New type of left value. * @return New Either bound to the new left type and the same right type. */ public <T> Either<T, R> mapLeft(Function<? super L, ? extends T> lFunc) { return new Either<>(left.map(lFunc), right); }
/** * Create a new Either with the left type. * * @param value Left value * @param <L> Left type * @param <R> Right type */ public static <L, R> Either<L, R> left(L value) { return new Either<>(Optional.of(value), Optional.empty()); }
/** * Create a new Either with the right type. * * @param value Right value * @param <L> Left type * @param <R> Right type */ public static <L, R> Either<L, R> right(R value) { return new Either<>(Optional.empty(), Optional.of(value)); }
/** * Create a new Either with the left type. * * @param value Left value * @param <L> Left type * @param <R> Right type */ public static <L, R> Either<L, R> left(L value) { return new Either<>(Optional.of(value), Optional.empty()); }
/** * Create a new Either with the right type. * * @param value Right value * @param <L> Left type * @param <R> Right type */ public static <L, R> Either<L, R> right(R value) { return new Either<>(Optional.empty(), Optional.of(value)); }