fj.data.optic.Iso Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionaljava Show documentation
Show all versions of functionaljava Show documentation
Functional Java is an open source library that supports closures for the Java programming language
package fj.data.optic;
import fj.F;
import fj.P2;
/** {@link PIso} when S = T and A = B */
public final class Iso extends PIso {
final PIso pIso;
public Iso(final PIso pIso) {
this.pIso = pIso;
}
@Override
public A get(final S s) {
return pIso.get(s);
}
@Override
public S reverseGet(final A a) {
return pIso.reverseGet(a);
}
@Override
public Iso reverse() {
return new Iso<>(pIso.reverse());
}
/** pair two disjoint {@link Iso} */
public Iso, P2> product(final Iso other) {
return new Iso<>(pIso.product(other.pIso));
}
@Override
public Iso, P2> first() {
return new Iso<>(pIso.first());
}
@Override
public Iso, P2> second() {
return new Iso<>(pIso.second());
}
/**********************************************************/
/** Compose methods between an {@link Iso} and another Optics */
/**********************************************************/
/** compose an {@link Iso} with a {@link Setter} */
public Setter composeSetter(final Setter other) {
return new Setter<>(pIso.composeSetter(other.pSetter));
}
/** compose an {@link Iso} with a {@link Traversal} */
public Traversal composeTraversal(final Traversal other) {
return new Traversal<>(pIso.composeTraversal(other.pTraversal));
}
/** compose an {@link Iso} with a {@link Optional} */
public Optional composeOptional(final Optional other) {
return new Optional<>(pIso.composeOptional(other.pOptional));
}
/** compose an {@link Iso} with a {@link Prism} */
public Prism composePrism(final Prism other) {
return new Prism<>(pIso.composePrism(other.pPrism));
}
/** compose an {@link Iso} with a {@link Lens} */
public Lens composeLens(final Lens other) {
return asLens().composeLens(other);
}
/** compose an {@link Iso} with an {@link Iso} */
public Iso composeIso(final Iso other) {
return new Iso<>(pIso.composeIso(other.pIso));
}
/****************************************************************/
/** Transformation methods to view an {@link Iso} as another Optics */
/****************************************************************/
/** view an {@link Iso} as a {@link Setter} */
@Override
public Setter asSetter() {
return new Setter<>(pIso.asSetter());
}
/** view an {@link Iso} as a {@link Traversal} */
@Override
public Traversal asTraversal() {
return new Traversal<>(pIso.asTraversal());
}
/** view an {@link Iso} as a {@link Optional} */
@Override
public Optional asOptional() {
return new Optional<>(pIso.asOptional());
}
/** view an {@link Iso} as a {@link Prism} */
@Override
public Prism asPrism() {
return new Prism<>(pIso.asPrism());
}
/** view an {@link Iso} as a {@link Lens} */
@Override
public Lens asLens() {
return new Lens<>(pIso.asLens());
}
/** create an {@link Iso} using a pair of functions: one to get the target and one to get the source. */
public static Iso iso(final F get, final F reverseGet) {
return new Iso<>(PIso.pIso(get, reverseGet));
}
/**
* create an {@link Iso} between any type and itself. id is the zero element of optics composition, for all optics o of type O
* (e.g. Lens, Iso, Prism, ...):
*
*
* o composeIso Iso.id == o
* Iso.id composeO o == o
*
*
* (replace composeO by composeLens, composeIso, composePrism, ...)
*/
public static Iso id() {
return new Iso<>(PIso.pId());
}
}