Last active
May 15, 2016 13:11
-
-
Save jbgi/393f621decdeb1374b1f3356b2d140b6 to your computer and use it in GitHub Desktop.
Elements, generated by derive4j at compile time from the Element#match method definition.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mfusco.fromgoftolambda.examples.visitor; | |
import java.lang.Object; | |
import java.lang.Override; | |
import java.util.Optional; | |
import java.util.function.Function; | |
import java.util.function.Supplier; | |
final class Elements { | |
private static final TotalMatchBuilderSquareElement totalMatchBuilderSquareElement = new TotalMatchBuilderSquareElement(); | |
private Elements() { | |
} | |
public static VisitorLambda.Element squareElement(VisitorLambda.Square squareElement) { | |
return new SquareElement(squareElement); | |
} | |
public static VisitorLambda.Element circleElement(VisitorLambda.Circle circleElement) { | |
return new CircleElement(circleElement); | |
} | |
public static VisitorLambda.Element rectangleElement(VisitorLambda.Rectangle rectangleElement) { | |
return new RectangleElement(rectangleElement); | |
} | |
public static VisitorLambda.Element lazy(Supplier<VisitorLambda.Element> element) { | |
return new Lazy(element); | |
} | |
public static TotalMatchBuilderSquareElement cases() { | |
return totalMatchBuilderSquareElement; | |
} | |
public static Optional<VisitorLambda.Square> getSquareElement(VisitorLambda.Element element) { | |
return element.match((squareElement) -> Optional.of(squareElement), | |
(circleElement) -> Optional.empty(), | |
(rectangleElement) -> Optional.empty());} | |
public static Optional<VisitorLambda.Circle> getCircleElement(VisitorLambda.Element element) { | |
return element.match((squareElement) -> Optional.empty(), | |
(circleElement) -> Optional.of(circleElement), | |
(rectangleElement) -> Optional.empty());} | |
public static Optional<VisitorLambda.Rectangle> getRectangleElement(VisitorLambda.Element element) { | |
return element.match((squareElement) -> Optional.empty(), | |
(circleElement) -> Optional.empty(), | |
(rectangleElement) -> Optional.of(rectangleElement));} | |
public static Function<VisitorLambda.Element, VisitorLambda.Element> setSquareElement(VisitorLambda.Square newSquareElement) { | |
return modSquareElement(__ -> newSquareElement); | |
} | |
public static Function<VisitorLambda.Element, VisitorLambda.Element> modSquareElement(Function<VisitorLambda.Square, VisitorLambda.Square> squareElementMod) { | |
return element -> element.match((squareElement) -> squareElement(squareElementMod.apply(squareElement)), | |
(circleElement) -> circleElement(circleElement), | |
(rectangleElement) -> rectangleElement(rectangleElement)); | |
} | |
public static Function<VisitorLambda.Element, VisitorLambda.Element> setCircleElement(VisitorLambda.Circle newCircleElement) { | |
return modCircleElement(__ -> newCircleElement); | |
} | |
public static Function<VisitorLambda.Element, VisitorLambda.Element> modCircleElement(Function<VisitorLambda.Circle, VisitorLambda.Circle> circleElementMod) { | |
return element -> element.match((squareElement) -> squareElement(squareElement), | |
(circleElement) -> circleElement(circleElementMod.apply(circleElement)), | |
(rectangleElement) -> rectangleElement(rectangleElement)); | |
} | |
public static Function<VisitorLambda.Element, VisitorLambda.Element> setRectangleElement(VisitorLambda.Rectangle newRectangleElement) { | |
return modRectangleElement(__ -> newRectangleElement); | |
} | |
public static Function<VisitorLambda.Element, VisitorLambda.Element> modRectangleElement(Function<VisitorLambda.Rectangle, VisitorLambda.Rectangle> rectangleElementMod) { | |
return element -> element.match((squareElement) -> squareElement(squareElement), | |
(circleElement) -> circleElement(circleElement), | |
(rectangleElement) -> rectangleElement(rectangleElementMod.apply(rectangleElement))); | |
} | |
private static final class SquareElement implements VisitorLambda.Element { | |
private final VisitorLambda.Square squareElement; | |
SquareElement(VisitorLambda.Square squareElement) { | |
this.squareElement = squareElement; | |
} | |
@Override | |
public <T> T match(Function<VisitorLambda.Square, T> squareElement, Function<VisitorLambda.Circle, T> circleElement, Function<VisitorLambda.Rectangle, T> rectangleElement) { | |
return squareElement.apply(this.squareElement); | |
} | |
} | |
private static final class CircleElement implements VisitorLambda.Element { | |
private final VisitorLambda.Circle circleElement; | |
CircleElement(VisitorLambda.Circle circleElement) { | |
this.circleElement = circleElement; | |
} | |
@Override | |
public <T> T match(Function<VisitorLambda.Square, T> squareElement, Function<VisitorLambda.Circle, T> circleElement, Function<VisitorLambda.Rectangle, T> rectangleElement) { | |
return circleElement.apply(this.circleElement); | |
} | |
} | |
private static final class RectangleElement implements VisitorLambda.Element { | |
private final VisitorLambda.Rectangle rectangleElement; | |
RectangleElement(VisitorLambda.Rectangle rectangleElement) { | |
this.rectangleElement = rectangleElement; | |
} | |
@Override | |
public <T> T match(Function<VisitorLambda.Square, T> squareElement, Function<VisitorLambda.Circle, T> circleElement, Function<VisitorLambda.Rectangle, T> rectangleElement) { | |
return rectangleElement.apply(this.rectangleElement); | |
} | |
} | |
private static final class Lazy implements VisitorLambda.Element { | |
private final Object lock = new Object(); | |
private Supplier<VisitorLambda.Element> expression; | |
private volatile VisitorLambda.Element evaluation; | |
Lazy(Supplier<VisitorLambda.Element> element) { | |
this.expression = element; | |
} | |
private VisitorLambda.Element eval() { | |
VisitorLambda.Element _evaluation = this.evaluation; | |
if (_evaluation == null) { | |
synchronized (this.lock) { | |
_evaluation = this.evaluation; | |
if (_evaluation == null) { | |
this.evaluation = _evaluation = expression.get(); | |
this.expression = null; | |
} | |
} | |
} | |
return _evaluation; | |
} | |
@Override | |
public <T> T match(Function<VisitorLambda.Square, T> squareElement, Function<VisitorLambda.Circle, T> circleElement, Function<VisitorLambda.Rectangle, T> rectangleElement) { | |
return eval().match(squareElement, circleElement, rectangleElement); | |
} | |
} | |
public static final class TotalMatchBuilderSquareElement { | |
private TotalMatchBuilderSquareElement() { | |
} | |
public final <T> TotalMatchBuilderCircleElement<T> squareElement(Function<VisitorLambda.Square, T> squareElement) { | |
return new TotalMatchBuilderCircleElement<>(squareElement); | |
} | |
public final <T> TotalMatchBuilderCircleElement<T> squareElement(T t) { | |
return this.squareElement((squareElement) -> t); | |
} | |
public final <T> PartialMatchBuilderRectangleElement<T> circleElement(Function<VisitorLambda.Circle, T> circleElement) { | |
return new PartialMatchBuilderRectangleElement<>(null, circleElement); | |
} | |
public final <T> PartialMatchBuilderRectangleElement<T> circleElement(T t) { | |
return this.circleElement((circleElement) -> t); | |
} | |
public final <T> PartialMatchBuilder<T> rectangleElement(Function<VisitorLambda.Rectangle, T> rectangleElement) { | |
return new PartialMatchBuilder<>(null, null, rectangleElement); | |
} | |
public final <T> PartialMatchBuilder<T> rectangleElement(T t) { | |
return this.rectangleElement((rectangleElement) -> t); | |
} | |
} | |
public static final class TotalMatchBuilderCircleElement<T> extends PartialMatchBuilder<T> { | |
private TotalMatchBuilderCircleElement(Function<VisitorLambda.Square, T> squareElement) { | |
super(squareElement, null, null); | |
} | |
public final TotalMatchBuilderRectangleElement<T> circleElement(Function<VisitorLambda.Circle, T> circleElement) { | |
return new TotalMatchBuilderRectangleElement<>(super.squareElement, circleElement); | |
} | |
public final TotalMatchBuilderRectangleElement<T> circleElement(T t) { | |
return this.circleElement((circleElement) -> t); | |
} | |
public final PartialMatchBuilder<T> rectangleElement(Function<VisitorLambda.Rectangle, T> rectangleElement) { | |
return new PartialMatchBuilder<>(super.squareElement, null, rectangleElement); | |
} | |
public final PartialMatchBuilder<T> rectangleElement(T t) { | |
return this.rectangleElement((rectangleElement) -> t); | |
} | |
} | |
public static final class TotalMatchBuilderRectangleElement<T> extends PartialMatchBuilder<T> { | |
private TotalMatchBuilderRectangleElement(Function<VisitorLambda.Square, T> squareElement, Function<VisitorLambda.Circle, T> circleElement) { | |
super(squareElement, circleElement, null); | |
} | |
public final Function<VisitorLambda.Element, T> rectangleElement(Function<VisitorLambda.Rectangle, T> rectangleElement) { | |
Function<VisitorLambda.Square, T> squareElement = super.squareElement; | |
Function<VisitorLambda.Circle, T> circleElement = super.circleElement; | |
return element -> element.match(squareElement, circleElement, rectangleElement); | |
} | |
public final Function<VisitorLambda.Element, T> rectangleElement(T t) { | |
return this.rectangleElement((rectangleElement) -> t); | |
} | |
} | |
public static class PartialMatchBuilderCircleElement<T> extends PartialMatchBuilder<T> { | |
private PartialMatchBuilderCircleElement(Function<VisitorLambda.Square, T> squareElement) { | |
super(squareElement, null, null); | |
} | |
public final PartialMatchBuilderRectangleElement<T> circleElement(Function<VisitorLambda.Circle, T> circleElement) { | |
return new PartialMatchBuilderRectangleElement<>(super.squareElement, circleElement); | |
} | |
public final PartialMatchBuilderRectangleElement<T> circleElement(T t) { | |
return this.circleElement((circleElement) -> t); | |
} | |
public final PartialMatchBuilder<T> rectangleElement(Function<VisitorLambda.Rectangle, T> rectangleElement) { | |
return new PartialMatchBuilder<>(super.squareElement, null, rectangleElement); | |
} | |
public final PartialMatchBuilder<T> rectangleElement(T t) { | |
return this.rectangleElement((rectangleElement) -> t); | |
} | |
} | |
public static class PartialMatchBuilderRectangleElement<T> extends PartialMatchBuilder<T> { | |
private PartialMatchBuilderRectangleElement(Function<VisitorLambda.Square, T> squareElement, Function<VisitorLambda.Circle, T> circleElement) { | |
super(squareElement, circleElement, null); | |
} | |
public final PartialMatchBuilder<T> rectangleElement(Function<VisitorLambda.Rectangle, T> rectangleElement) { | |
return new PartialMatchBuilder<>(super.squareElement, super.circleElement, rectangleElement); | |
} | |
public final PartialMatchBuilder<T> rectangleElement(T t) { | |
return this.rectangleElement((rectangleElement) -> t); | |
} | |
} | |
public static class PartialMatchBuilder<T> { | |
private final Function<VisitorLambda.Square, T> squareElement; | |
private final Function<VisitorLambda.Circle, T> circleElement; | |
private final Function<VisitorLambda.Rectangle, T> rectangleElement; | |
private PartialMatchBuilder(Function<VisitorLambda.Square, T> squareElement, Function<VisitorLambda.Circle, T> circleElement, Function<VisitorLambda.Rectangle, T> rectangleElement) { | |
this.squareElement = squareElement; | |
this.circleElement = circleElement; | |
this.rectangleElement = rectangleElement; | |
} | |
public final Function<VisitorLambda.Element, T> otherwise(Supplier<T> otherwise) { | |
Function<VisitorLambda.Square, T> squareElement = (this.squareElement != null) ? this.squareElement : (squareElement_) -> otherwise.get(); | |
Function<VisitorLambda.Circle, T> circleElement = (this.circleElement != null) ? this.circleElement : (circleElement_) -> otherwise.get(); | |
Function<VisitorLambda.Rectangle, T> rectangleElement = (this.rectangleElement != null) ? this.rectangleElement : (rectangleElement_) -> otherwise.get(); | |
return element -> element.match(squareElement, circleElement, rectangleElement); | |
} | |
public final Function<VisitorLambda.Element, T> otherwise(T t) { | |
return this.otherwise(() -> t); | |
} | |
public final Function<VisitorLambda.Element, Optional<T>> otherwiseEmpty() { | |
Function<VisitorLambda.Square, Optional<T>> squareElement = (this.squareElement != null) ? (squareElement_) -> Optional.of(this.squareElement.apply(squareElement_)) | |
: (squareElement_) -> Optional.empty(); | |
Function<VisitorLambda.Circle, Optional<T>> circleElement = (this.circleElement != null) ? (circleElement_) -> Optional.of(this.circleElement.apply(circleElement_)) | |
: (circleElement_) -> Optional.empty(); | |
Function<VisitorLambda.Rectangle, Optional<T>> rectangleElement = (this.rectangleElement != null) ? (rectangleElement_) -> Optional.of(this.rectangleElement.apply(rectangleElement_)) | |
: (rectangleElement_) -> Optional.empty(); | |
return element -> element.match(squareElement, circleElement, rectangleElement); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment