This is an automated email from the ASF dual-hosted git repository. heneveld pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git
commit 216b48558991a32c161cf8d2c43dd3250757cc3e Author: Alex Heneveld <[email protected]> AuthorDate: Thu May 25 14:25:25 2023 +0100 more workflow math for time add and subtract duration, subtract duration from instant, don't run unless it is the right operator --- .../steps/variables/SetVariableWorkflowStep.java | 29 ++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/SetVariableWorkflowStep.java b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/SetVariableWorkflowStep.java index 3e84563740..6531ba0cec 100644 --- a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/SetVariableWorkflowStep.java +++ b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/SetVariableWorkflowStep.java @@ -375,8 +375,33 @@ public class SetVariableWorkflowStep extends WorkflowStepDefinition { Object lhs = process(lhs0, null); Object rhs = process(rhs0, null); - if (lhs instanceof Instant) return TypeCoercions.coerce(rhs, Duration.class).addTo((Instant)lhs); - if (lhs instanceof Date) return new Timestamp((Instant) TypeCoercions.coerce(rhs, Duration.class).addTo( ((Date)lhs).toInstant() )); + if ("+".equals(op)) { + if (lhs instanceof Duration) { + if (rhs instanceof Instant || rhs instanceof Date) { + Object newRhs = lhs; + lhs = rhs; + rhs = newRhs; + // fall through to below + } else { + return TypeCoercions.coerce(rhs, Duration.class).add((Duration) lhs); + } + } + if (lhs instanceof Instant) return TypeCoercions.coerce(rhs, Duration.class).addTo((Instant) lhs); + if (lhs instanceof Date) + return new Timestamp((Instant) TypeCoercions.coerce(rhs, Duration.class).addTo(((Date) lhs).toInstant())); + } else if ("-".equals(op)) { + if (lhs instanceof Duration) { + return ((Duration)lhs).subtract(TypeCoercions.coerce(rhs, Duration.class)); + } + if (lhs instanceof Instant) { + if (rhs instanceof Instant) return Duration.between((Instant)rhs, (Instant)lhs); + return TypeCoercions.coerce(rhs, Duration.class).multiply(-1).addTo((Instant) lhs); + } + if (lhs instanceof Date) { + if (rhs instanceof Date) return Duration.between(((Date)rhs).toInstant(), ((Date)lhs).toInstant()); + return new Timestamp((Instant) TypeCoercions.coerce(rhs, Duration.class).multiply(-1).addTo(((Date) lhs).toInstant())); + } + } Maybe<Integer> lhsI = asInteger(lhs); Maybe<Integer> rhsI = asInteger(rhs);
