diff --git a/app/src/main/java/com/annimon/hotarufx/visual/PropertyTimelineHolder.java b/app/src/main/java/com/annimon/hotarufx/visual/PropertyTimelineHolder.java new file mode 100644 index 0000000..d52ad03 --- /dev/null +++ b/app/src/main/java/com/annimon/hotarufx/visual/PropertyTimelineHolder.java @@ -0,0 +1,51 @@ +package com.annimon.hotarufx.visual; + +import java.util.function.Consumer; +import java.util.function.Supplier; +import javafx.beans.value.WritableValue; + +public final class PropertyTimelineHolder { + + public static PropertyTimelineHolder empty() { + return new PropertyTimelineHolder<>(null); + } + + private PropertyTimeline value; + + private PropertyTimelineHolder(PropertyTimeline value) { + this.value = value; + } + + public boolean isPresent() { + return value != null; + } + + public boolean isEmpty() { + return value == null; + } + + public void ifPresent(Consumer> consumer) { + if (isPresent()) { + consumer.accept(value); + } + } + + public PropertyTimelineHolder setIfEmpty(Supplier> supplier) { + if (isEmpty()) { + value = supplier.get(); + } + return this; + } + + public PropertyTimeline setIfEmptyThenGet(Supplier> supplier) { + return setIfEmpty(wrap(supplier)).get(); + } + + public PropertyTimeline get() { + return value; + } + + private Supplier> wrap(Supplier> supplier) { + return () -> new PropertyTimeline<>(supplier.get()); + } +} diff --git a/app/src/main/java/com/annimon/hotarufx/visual/objects/CircleNode.java b/app/src/main/java/com/annimon/hotarufx/visual/objects/CircleNode.java index 8e9ad38..a4ccc2f 100644 --- a/app/src/main/java/com/annimon/hotarufx/visual/objects/CircleNode.java +++ b/app/src/main/java/com/annimon/hotarufx/visual/objects/CircleNode.java @@ -1,43 +1,34 @@ package com.annimon.hotarufx.visual.objects; +import com.annimon.hotarufx.visual.PropertyTimelineHolder; import com.annimon.hotarufx.visual.PropertyTimeline; import com.annimon.hotarufx.visual.TimeLine; import com.annimon.hotarufx.visual.visitors.NodeVisitor; -import java.util.Optional; import javafx.scene.shape.Circle; public class CircleNode extends ObjectNode { public final Circle circle; - private Optional> centerX, centerY, radius; + private PropertyTimelineHolder centerX, centerY, radius; public CircleNode() { circle = new Circle(); - centerX = Optional.empty(); - centerY = Optional.empty(); - radius = Optional.empty(); + centerX = PropertyTimelineHolder.empty(); + centerY = PropertyTimelineHolder.empty(); + radius = PropertyTimelineHolder.empty(); } public PropertyTimeline centerXProperty() { - if (!centerX.isPresent()) { - centerX = Optional.of(new PropertyTimeline<>(circle.centerXProperty())); - } - return centerX.get(); + return centerX.setIfEmptyThenGet(circle::centerXProperty); } public PropertyTimeline centerYProperty() { - if (!centerY.isPresent()) { - centerY = Optional.of(new PropertyTimeline<>(circle.centerYProperty())); - } - return centerY.get(); + return centerY.setIfEmptyThenGet(circle::centerYProperty); } public PropertyTimeline radiusProperty() { - if (!radius.isPresent()) { - radius = Optional.of(new PropertyTimeline<>(circle.radiusProperty())); - } - return radius.get(); + return radius.setIfEmptyThenGet(circle::radiusProperty); } public void buildTimeline(TimeLine timeline) {