com.github.tonivade.purefun.optics.Optional Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2019, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun.optics;
import static com.github.tonivade.purefun.Producer.cons;
import static java.util.Objects.requireNonNull;
import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Function2;
import com.github.tonivade.purefun.Operator1;
import com.github.tonivade.purefun.type.Either;
import com.github.tonivade.purefun.type.Option;
public final class Optional {
private final POptional delegate;
protected Optional(POptional delegate) {
this.delegate = requireNonNull(delegate);
}
public static Optional of(Function2 set, Function1> getOption) {
return new Optional<>(POptional.of(set, target -> getOption.apply(target).fold(cons(Either.left(target)), Either::right)));
}
public Function1 set(S target) {
return delegate.set(target);
}
public S set(S target, A value) {
return delegate.set(target, value);
}
public Either getOrModify(S target) {
return delegate.getOrModify(target);
}
public Option getOption(S target) {
return delegate.getOption(target);
}
public Operator1 lift(Operator1 mapper) {
return delegate.lift(mapper)::apply;
}
public S modify(S target, Operator1 mapper) {
return delegate.modify(target, mapper);
}
public Option modifyOption(S target, Operator1 mapper) {
return delegate.modifyOption(target, mapper);
}
public Optional compose(Optional other) {
return new Optional<>(delegate.compose(other.delegate));
}
public Optional compose(Iso other) {
return compose(other.asOptional());
}
public Optional compose(Prism other) {
return compose(other.asOptional());
}
public Optional compose(Lens other) {
return compose(other.asOptional());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy