com.regnosys.rosetta.translate.synonymmap.SynonymCondition Maven / Gradle / Ivy
package com.regnosys.rosetta.translate.synonymmap;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.regnosys.rosetta.rosetta.RosettaCallableWithArgs;
import com.regnosys.rosetta.rosetta.RosettaMapPath;
import com.regnosys.rosetta.rosetta.RosettaMapRosettaPath;
import com.regnosys.rosetta.rosetta.RosettaMapTest;
import com.regnosys.rosetta.rosetta.RosettaMapTestAbsentExpression;
import com.regnosys.rosetta.rosetta.RosettaMapTestEqualityOperation;
import com.regnosys.rosetta.rosetta.RosettaMapTestExistsExpression;
import com.regnosys.rosetta.rosetta.RosettaMapTestExpression;
import com.regnosys.rosetta.rosetta.RosettaMapTestFunc;
import com.regnosys.rosetta.rosetta.RosettaModel;
public class SynonymCondition {
private static final Logger LOGGER = LoggerFactory.getLogger(SynonymCondition.class);
//An optional value that will be used if the condition is true
//if it is null then a Synonym value is expected to be used
private final RosettaMapTestExpression setToValue;
//A condition to decide whether this mapping will be used
//it consists of ANDed expression
private final List condition;
//see the toStringMethod to see how this relates to rosetta
public static SynonymCondition create(RosettaMapTestExpression setToValue, List condition) {
return new SynonymCondition(setToValue, toTests(condition));
}
public SynonymCondition(RosettaMapTestExpression setToValue, List condition) {
this.setToValue = setToValue;
this.condition = condition;
}
private static List toTests(List condition) {
List result = new ArrayList<>();
for (RosettaMapTest exp: condition) {
if (exp instanceof RosettaMapTestEqualityOperation) {
result.add(new SynonymBinaryTest((RosettaMapTestEqualityOperation)exp));
}
else if (exp instanceof RosettaMapTestExistsExpression) {
result.add(new SynonymExistsTest((RosettaMapTestExistsExpression)exp));
}
else if (exp instanceof RosettaMapTestAbsentExpression) {
result.add(new SynonymAbsentTest((RosettaMapTestAbsentExpression)exp));
}
else if (exp instanceof RosettaMapPath) {
result.add(new SynonymPathTest((RosettaMapPath) exp));
}
else if (exp instanceof RosettaMapRosettaPath) {
result.add(new SynonymRosettaPathTest((RosettaMapRosettaPath) exp));
}
else if (exp instanceof RosettaMapTestFunc) {
result.add(new SynonymConditionFuncTest((RosettaMapTestFunc) exp));
}
else {
throw new UnsupportedOperationException("Cannot convert expression of type "+exp.getClass().getSimpleName()+" to SynonymTest");
}
}
return result;
}
public RosettaMapTestExpression getSetToValue() {
return setToValue;
}
public List getCondition() {
return condition;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((condition == null) ? 0 : condition.hashCode());
result = prime * result + ((setToValue == null) ? 0 : setToValue.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SynonymCondition other = (SynonymCondition) obj;
if (condition == null) {
if (other.condition != null)
return false;
} else if (!condition.equals(other.condition))
return false;
if (setToValue == null) {
if (other.setToValue != null)
return false;
} else if (!setToValue.equals(other.setToValue))
return false;
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (condition==null || condition.isEmpty() && setToValue!=null) {
builder.append("default to ");
builder.append(SynonymTest.toString(setToValue));
}
else {
if (setToValue!=null) {
builder.append("set to ");
builder.append(SynonymTest.toString(setToValue));
}
if (!(condition==null || condition.isEmpty())) {
builder.append(" when ");
builder.append(condition.stream().map(t->t.toString()).collect(Collectors.joining(" and ")));
}
}
return builder.toString();
}
}