1
0
mirror of https://github.com/aNNiMON/HotaruFX.git synced 2024-09-19 14:14:21 +03:00

Replace Optional with custom holder

This commit is contained in:
Victor 2017-08-25 11:07:06 +03:00
parent 41550b683f
commit a34a23372b
2 changed files with 59 additions and 17 deletions

View File

@ -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<T> {
public static <T> PropertyTimelineHolder<T> empty() {
return new PropertyTimelineHolder<>(null);
}
private PropertyTimeline<T> value;
private PropertyTimelineHolder(PropertyTimeline<T> value) {
this.value = value;
}
public boolean isPresent() {
return value != null;
}
public boolean isEmpty() {
return value == null;
}
public void ifPresent(Consumer<PropertyTimeline<T>> consumer) {
if (isPresent()) {
consumer.accept(value);
}
}
public PropertyTimelineHolder<T> setIfEmpty(Supplier<PropertyTimeline<T>> supplier) {
if (isEmpty()) {
value = supplier.get();
}
return this;
}
public PropertyTimeline<T> setIfEmptyThenGet(Supplier<WritableValue<T>> supplier) {
return setIfEmpty(wrap(supplier)).get();
}
public PropertyTimeline<T> get() {
return value;
}
private Supplier<PropertyTimeline<T>> wrap(Supplier<WritableValue<T>> supplier) {
return () -> new PropertyTimeline<>(supplier.get());
}
}

View File

@ -1,43 +1,34 @@
package com.annimon.hotarufx.visual.objects; package com.annimon.hotarufx.visual.objects;
import com.annimon.hotarufx.visual.PropertyTimelineHolder;
import com.annimon.hotarufx.visual.PropertyTimeline; import com.annimon.hotarufx.visual.PropertyTimeline;
import com.annimon.hotarufx.visual.TimeLine; import com.annimon.hotarufx.visual.TimeLine;
import com.annimon.hotarufx.visual.visitors.NodeVisitor; import com.annimon.hotarufx.visual.visitors.NodeVisitor;
import java.util.Optional;
import javafx.scene.shape.Circle; import javafx.scene.shape.Circle;
public class CircleNode extends ObjectNode { public class CircleNode extends ObjectNode {
public final Circle circle; public final Circle circle;
private Optional<PropertyTimeline<Number>> centerX, centerY, radius; private PropertyTimelineHolder<Number> centerX, centerY, radius;
public CircleNode() { public CircleNode() {
circle = new Circle(); circle = new Circle();
centerX = Optional.empty(); centerX = PropertyTimelineHolder.empty();
centerY = Optional.empty(); centerY = PropertyTimelineHolder.empty();
radius = Optional.empty(); radius = PropertyTimelineHolder.empty();
} }
public PropertyTimeline<Number> centerXProperty() { public PropertyTimeline<Number> centerXProperty() {
if (!centerX.isPresent()) { return centerX.setIfEmptyThenGet(circle::centerXProperty);
centerX = Optional.of(new PropertyTimeline<>(circle.centerXProperty()));
}
return centerX.get();
} }
public PropertyTimeline<Number> centerYProperty() { public PropertyTimeline<Number> centerYProperty() {
if (!centerY.isPresent()) { return centerY.setIfEmptyThenGet(circle::centerYProperty);
centerY = Optional.of(new PropertyTimeline<>(circle.centerYProperty()));
}
return centerY.get();
} }
public PropertyTimeline<Number> radiusProperty() { public PropertyTimeline<Number> radiusProperty() {
if (!radius.isPresent()) { return radius.setIfEmptyThenGet(circle::radiusProperty);
radius = Optional.of(new PropertyTimeline<>(circle.radiusProperty()));
}
return radius.get();
} }
public void buildTimeline(TimeLine timeline) { public void buildTimeline(TimeLine timeline) {