Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Damian committed Jun 12, 2024
1 parent a281227 commit 2a90cfa
Showing 1 changed file with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.regnosys.rosetta.interpreternew.visitors;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterDateValue;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnvironment;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterError;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterErrorValue;
Expand Down Expand Up @@ -34,10 +40,13 @@ public RosettaInterpreterValue interp(ArithmeticOperation expr,
RosettaInterpreterValue leftInterpreted = left.accept(visitor, env);
RosettaInterpreterValue rightInterpreted = right.accept(visitor, env);

//Check that the types are correct for the operations
if (!(leftInterpreted instanceof RosettaInterpreterNumberValue
|| leftInterpreted instanceof RosettaInterpreterStringValue)
|| !(rightInterpreted instanceof RosettaInterpreterNumberValue
|| rightInterpreted instanceof RosettaInterpreterStringValue)) {
|| leftInterpreted instanceof RosettaInterpreterStringValue
|| rightInterpreted instanceof RosettaInterpreterDateValue)
|| !(rightInterpreted instanceof RosettaInterpreterNumberValue
|| leftInterpreted instanceof RosettaInterpreterStringValue
|| rightInterpreted instanceof RosettaInterpreterDateValue)) {

// Check for errors in the left or right side of the binary operation
RosettaInterpreterErrorValue leftErrors =
Expand All @@ -47,6 +56,7 @@ public RosettaInterpreterValue interp(ArithmeticOperation expr,
return RosettaInterpreterErrorValue.merge(List.of(leftErrors, rightErrors));
}

//Interpret string concatenation
if (leftInterpreted instanceof RosettaInterpreterStringValue
&& rightInterpreted instanceof RosettaInterpreterStringValue) {
String leftString = ((RosettaInterpreterStringValue) leftInterpreted)
Expand All @@ -62,6 +72,7 @@ public RosettaInterpreterValue interp(ArithmeticOperation expr,
"The terms are strings but the operation "
+ "is not concatenation: not implemented"));
}
//Interpret number operations
} else if (leftInterpreted instanceof RosettaInterpreterNumberValue
&& rightInterpreted instanceof RosettaInterpreterNumberValue) {
RosettaNumber leftNumber = ((RosettaInterpreterNumberValue) leftInterpreted).getValue();
Expand All @@ -86,10 +97,23 @@ public RosettaInterpreterValue interp(ArithmeticOperation expr,
return new RosettaInterpreterNumberValue((leftNumber
.divide(rightNumber)).bigDecimalValue());
}
} else if (leftInterpreted instanceof RosettaInterpreterDateValue
&& rightInterpreted instanceof RosettaInterpreterDateValue) {
RosettaInterpreterDateValue l = (RosettaInterpreterDateValue) leftInterpreted;
RosettaInterpreterDateValue r = (RosettaInterpreterDateValue) rightInterpreted;
if (expr.getOperator().equals("-")) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MM yyyy");
String inputString1 = "23 01 1997";
String inputString2 = "27 04 1997";

LocalDateTime date1 = LocalDate.parse(inputString1, dtf);
LocalDateTime date2 = LocalDate.parse(inputString2, dtf);
long daysBetween = Duration.between(date1, date2).toDays();
System.out.println ("Days: " + daysBetween);
} else {
return new RosettaInterpreterErrorValue(
new RosettaInterpreterError(
"The terms of the operation are neither both strings nor both numbers"));
"The terms of the operation are not both strings or both numbers or both dates"));
}
}

Expand All @@ -108,7 +132,8 @@ public RosettaInterpreterValue interp(ArithmeticOperation expr,
private RosettaInterpreterErrorValue checkForErrors(
RosettaInterpreterValue interpretedValue, String side) {
if (interpretedValue instanceof RosettaInterpreterNumberValue
|| interpretedValue instanceof RosettaInterpreterStringValue) {
|| interpretedValue instanceof RosettaInterpreterStringValue
|| interpretedValue instanceof RosettaInterpreterDateValue) {
// If the value satisfies the type conditions, we return an empty
// error value so that the merger has two error values to merge
return new RosettaInterpreterErrorValue();
Expand All @@ -123,7 +148,7 @@ else if (RosettaInterpreterErrorValue.errorsExist(interpretedValue)) {
return new RosettaInterpreterErrorValue(
new RosettaInterpreterError(
"Arithmetic Operation: " + side
+ " is not of type Number/String"));
+ " is not of type Number/String/Date"));
}
}
}
Expand Down

0 comments on commit 2a90cfa

Please sign in to comment.