Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update handling of Infinity / NaN values as special constants when they are not represented as the data type of the parent object. #894

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import gov.nasa.arc.pds.xml.generated.SpecialConstants;
import gov.nasa.pds.tools.validate.content.ProblemReporter;
import gov.nasa.pds.tools.validate.content.SpecialConstantBitPatternTransforms;

public class SpecialConstantChecker {
final private static List<String> INF_NAN_VALUES = Arrays.asList("INF", "-INF", "+INF", "INFINITY",
"-INFINITY", "+INFINITY", "NAN", "-NAN", "+NAN");
public static boolean isInf (String value) {
return INF_NAN_VALUES.subList(0, 6).contains(value.toUpperCase());
}
public static boolean isInfOrNan (String value) {
return INF_NAN_VALUES.contains (value.toUpperCase());
}
/**
* Use this when the special constant may not conform to the type constraints
* meaning only string comparisons can be done.
Expand Down Expand Up @@ -151,6 +161,23 @@ public static boolean sameContent (Number number, String constant_repr) {
if (number.toString().equals(constant_repr)) {
return true;
}
if (isInfOrNan(constant_repr)) {
if (number instanceof BigDecimal || number instanceof Double) {
Double d = number instanceof Double ? (Double)number : ((BigDecimal)number).doubleValue();
if (isInf(constant_repr)) {
return d.isInfinite();
} else {
return d.isNaN();
}
}
if (number instanceof Float) {
if (isInf(constant_repr)) {
return ((Float)number).isInfinite();
} else {
return ((Float)number).isNaN();
}
}
}
if (number instanceof BigDecimal) number = ((BigDecimal)number).doubleValue();
if (number instanceof Byte) number = BigInteger.valueOf(number.byteValue());
if (number instanceof Double) number = BigInteger.valueOf(Double.doubleToRawLongBits((Double)number));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
public class FieldValueValidator {
private static final Logger LOG = LoggerFactory.getLogger(FieldValueValidator.class);
/** List of invalid values. */
private final List<String> INF_NAN_VALUES = Arrays.asList("INF", "-INF", "+INF", "INFINITY",
"-INFINITY", "+INFINITY", "NAN", "-NAN", "+NAN");

/** List of valid datetime formats. */
private static final Map<String, String> DATE_TIME_VALID_FORMATS = new HashMap<>();
Expand Down Expand Up @@ -578,7 +576,7 @@ private void checkType(String value, FieldType type) throws InvalidTableExceptio

LOG.debug("checkType:value,type:after [{}],[{}]", value, type);

if (INF_NAN_VALUES.contains(value.toUpperCase()) && !realTypes.contains(type)) {
if (SpecialConstantChecker.isInfOrNan(value) && !realTypes.contains(type)) {
throw new InvalidTableException(value + " is not allowed");
}
if (FieldType.ASCII_INTEGER.getXMLType().equals(type.getXMLType())) {
Expand Down