Skip to content

Commit

Permalink
Removed Integer Value and changed some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioLupu committed Jun 5, 2024
1 parent 6107ebd commit f3aab4e
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 367 deletions.
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
package com.regnosys.rosetta.interpreternew.values;

import java.math.BigInteger;
import java.util.Objects;
import java.util.stream.Stream;

import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue;

public class RosettaInterpreterIntegerValue extends RosettaInterpreterBaseValue
implements Comparable<RosettaInterpreterIntegerValue> {

private BigInteger value;

@Override
public int hashCode() {
return Objects.hash(value);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
RosettaInterpreterIntegerValue other = (RosettaInterpreterIntegerValue) obj;
return Objects.equals(value, other.value);
}

public RosettaInterpreterIntegerValue(BigInteger value) {
super();
this.value = value;
}

public RosettaInterpreterIntegerValue(int value) {
super();
this.value = BigInteger.valueOf(value);
}

public BigInteger getValue() { return value; }

@Override
public int compareTo(RosettaInterpreterIntegerValue o) {
return this.value.compareTo(o.value);
}

@Override
public Stream<Object> toElementStream() {
return Stream.of(value);
}

@Override
public Stream<RosettaInterpreterValue> toValueStream() {
return Stream.of(this);
}
}
//package com.regnosys.rosetta.interpreternew.values;
//
//import java.math.BigInteger;
//import java.util.Objects;
//import java.util.stream.Stream;
//
//import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue;
//
//public class RosettaInterpreterIntegerValue extends RosettaInterpreterBaseValue
// implements Comparable<RosettaInterpreterIntegerValue> {
//
// private BigInteger value;
//
// @Override
// public int hashCode() {
// return Objects.hash(value);
// }
//
// @Override
// public boolean equals(Object obj) {
// if (this == obj) {
// return true;
// }
// if (obj == null) {
// return false;
// }
// if (getClass() != obj.getClass()) {
// return false;
// }
// RosettaInterpreterIntegerValue other = (RosettaInterpreterIntegerValue) obj;
// return Objects.equals(value, other.value);
// }
//
// public RosettaInterpreterIntegerValue(BigInteger value) {
// super();
// this.value = value;
// }
//
// public RosettaInterpreterIntegerValue(int value) {
// super();
// this.value = BigInteger.valueOf(value);
// }
//
// public BigInteger getValue() { return value; }
//
// @Override
// public int compareTo(RosettaInterpreterIntegerValue o) {
// return this.value.compareTo(o.value);
// }
//
// @Override
// public Stream<Object> toElementStream() {
// return Stream.of(value);
// }
//
// @Override
// public Stream<RosettaInterpreterValue> toValueStream() {
// return Stream.of(this);
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public RosettaInterpreterNumberValue(RosettaNumber value) {
this.value = value;
}

public RosettaInterpreterNumberValue(double value) {
super();
this.value = RosettaNumber.valueOf(value);
}

public RosettaNumber getValue() { return value; }

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.regnosys.rosetta.interpreternew.visitors;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -11,7 +10,6 @@
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnvironment;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterError;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterErrorValue;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterIntegerValue;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterListValue;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterNumberValue;
import com.regnosys.rosetta.rosetta.RosettaInterpreterBaseEnvironment;
Expand Down Expand Up @@ -92,7 +90,7 @@ public RosettaInterpreterValue interp(RosettaAbsentExpression exp, RosettaInterp
* Return the number of elements in a list
*
* @param exp Expression to perform 'count' on
* @return Integer indicating how many elements there are in the list
* @return Number indicating how many elements there are in the list
*/
public RosettaInterpreterValue interp(RosettaCountOperation exp, RosettaInterpreterBaseEnvironment env) {
RosettaExpression argument = exp.getArgument();
Expand All @@ -103,7 +101,7 @@ public RosettaInterpreterValue interp(RosettaCountOperation exp, RosettaInterpre
}

long count = RosettaInterpreterBaseValue.valueStream(interpretedArgument).count();
return new RosettaInterpreterIntegerValue(BigInteger.valueOf(count));
return new RosettaInterpreterNumberValue(BigDecimal.valueOf(count));
}

/**
Expand Down Expand Up @@ -288,13 +286,7 @@ public RosettaInterpreterValue interp(SumOperation exp, RosettaInterpreterBaseEn
// to numbers for further simplicity
for (int i = 0; i < values.size(); i++) {
RosettaInterpreterValue v = values.get(i);
if (v instanceof RosettaInterpreterIntegerValue) {
RosettaInterpreterIntegerValue valInt =
(RosettaInterpreterIntegerValue)v;
values.set(i, new RosettaInterpreterNumberValue(
BigDecimal.valueOf(valInt.getValue().longValue())));
}
else if (!(v instanceof RosettaInterpreterNumberValue)) {
if (!(v instanceof RosettaInterpreterNumberValue)) {
return new RosettaInterpreterErrorValue(
new RosettaInterpreterError("Cannot take sum"
+ "of non-number value"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,29 @@ public RosettaInterpreterValue interp(ArithmeticOperation expr) {
*/
public RosettaInterpreterValue interp(ArithmeticOperation expr,
RosettaInterpreterBaseEnvironment env) {

String leftString = null;
String rightString = null;

RosettaExpression left = expr.getLeft();
RosettaExpression right = expr.getRight();
RosettaInterpreterValue leftInterpreted = left.accept(visitor, env);
RosettaInterpreterValue rightInterpreted = right.accept(visitor, env);

if (!(leftInterpreted instanceof RosettaInterpreterNumberValue
|| leftInterpreted instanceof RosettaInterpreterStringValue
|| leftInterpreted instanceof RosettaInterpreterIntegerValue)
|| leftInterpreted instanceof RosettaInterpreterStringValue)
|| !(rightInterpreted instanceof RosettaInterpreterNumberValue
|| rightInterpreted instanceof RosettaInterpreterStringValue
|| rightInterpreted instanceof RosettaInterpreterIntegerValue)) {
|| rightInterpreted instanceof RosettaInterpreterStringValue)) {

// Check for errors in the left or right side of the binary operation
RosettaInterpreterErrorValue leftErrors =
checkForErrors(leftInterpreted, "Leftside");
RosettaInterpreterErrorValue rightErrors =
checkForErrors(rightInterpreted, "Rightside");
return RosettaInterpreterErrorValue.merge(List.of(leftErrors, rightErrors));
}
}

boolean sameType =
(leftInterpreted instanceof RosettaInterpreterStringValue
&& rightInterpreted instanceof RosettaInterpreterStringValue)
|| (!(leftInterpreted instanceof RosettaInterpreterStringValue)
&& !(rightInterpreted instanceof RosettaInterpreterStringValue));
if (!sameType) {
return new RosettaInterpreterErrorValue(
new RosettaInterpreterError(
"The terms of the operation "
+ "are neither both strings nor both numbers"));
}

if (leftInterpreted instanceof RosettaInterpreterStringValue) {
leftString = ((RosettaInterpreterStringValue) leftInterpreted)
if (leftInterpreted instanceof RosettaInterpreterStringValue
&& rightInterpreted instanceof RosettaInterpreterStringValue) {
String leftString = ((RosettaInterpreterStringValue) leftInterpreted)
.getValue();
rightString = ((RosettaInterpreterStringValue) rightInterpreted)
String rightString = ((RosettaInterpreterStringValue) rightInterpreted)
.getValue();
if (expr.getOperator().equals("+")) {
return new RosettaInterpreterStringValue(leftString + rightString);
Expand All @@ -84,39 +67,29 @@ public RosettaInterpreterValue interp(ArithmeticOperation expr,
"The terms are strings but the operation "
+ "is not concatenation: not implemented"));
}
}

RosettaNumber leftNumber;
RosettaNumber rightNumber;
} else if (leftInterpreted instanceof RosettaInterpreterNumberValue
&& rightInterpreted instanceof RosettaInterpreterNumberValue) {
RosettaNumber leftNumber = ((RosettaInterpreterNumberValue) leftInterpreted).getValue();
RosettaNumber rightNumber = ((RosettaInterpreterNumberValue) rightInterpreted).getValue();

if (leftInterpreted instanceof RosettaInterpreterNumberValue) {
leftNumber = ((RosettaInterpreterNumberValue) leftInterpreted).getValue();
}
else {
leftNumber = RosettaNumber
.valueOf(((RosettaInterpreterIntegerValue) leftInterpreted)
.getValue());
}
if (rightInterpreted instanceof RosettaInterpreterNumberValue) {
rightNumber = ((RosettaInterpreterNumberValue) rightInterpreted).getValue();
} else {
rightNumber = RosettaNumber
.valueOf(((RosettaInterpreterIntegerValue) rightInterpreted)
.getValue());
}
if (expr.getOperator().equals("+")) {
return new RosettaInterpreterNumberValue((leftNumber
.add(rightNumber)).bigDecimalValue());
} else if (expr.getOperator().equals("-")) {
return new RosettaInterpreterNumberValue((leftNumber
.subtract(rightNumber)).bigDecimalValue());
} else if (expr.getOperator().equals("*")) {
return new RosettaInterpreterNumberValue((leftNumber
.multiply(rightNumber)).bigDecimalValue());
if (expr.getOperator().equals("+")) {
return new RosettaInterpreterNumberValue((leftNumber
.add(rightNumber)).bigDecimalValue());
} else if (expr.getOperator().equals("-")) {
return new RosettaInterpreterNumberValue((leftNumber
.subtract(rightNumber)).bigDecimalValue());
} else if (expr.getOperator().equals("*")) {
return new RosettaInterpreterNumberValue((leftNumber
.multiply(rightNumber)).bigDecimalValue());
} else {
return new RosettaInterpreterNumberValue((leftNumber
.divide(rightNumber)).bigDecimalValue());
}
} else {
return new RosettaInterpreterNumberValue((leftNumber
.divide(rightNumber)).bigDecimalValue());
}
return new RosettaInterpreterErrorValue(
new RosettaInterpreterError(
"The terms of the operation are neither both strings nor both numbers"));
}
}


Expand All @@ -134,8 +107,7 @@ public RosettaInterpreterValue interp(ArithmeticOperation expr,
private RosettaInterpreterErrorValue checkForErrors(
RosettaInterpreterValue interpretedValue, String side) {
if (interpretedValue instanceof RosettaInterpreterNumberValue
|| interpretedValue instanceof RosettaInterpreterStringValue
|| interpretedValue instanceof RosettaInterpreterIntegerValue) {
|| interpretedValue instanceof RosettaInterpreterStringValue) {
// 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 Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.regnosys.rosetta.interpreternew.visitors;

import java.math.BigDecimal;

import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterBaseValue;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnvironment;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterIntegerValue;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterNumberValue;
import com.regnosys.rosetta.rosetta.expression.RosettaIntLiteral;

public class RosettaInterpreterRosettaIntLiteralInterpreter
Expand All @@ -12,8 +14,8 @@ public RosettaInterpreterBaseValue interp(RosettaIntLiteral expr) {
return interp(expr, new RosettaInterpreterEnvironment());
}

public RosettaInterpreterIntegerValue interp(RosettaIntLiteral expr,
public RosettaInterpreterNumberValue interp(RosettaIntLiteral expr,
RosettaInterpreterEnvironment env) {
return new RosettaInterpreterIntegerValue(expr.getValue());
return new RosettaInterpreterNumberValue(new BigDecimal(expr.getValue()));
}
}
Loading

0 comments on commit f3aab4e

Please sign in to comment.