Skip to content

Commit 956376c

Browse files
committed
[refactor] DoubleValue.convertTo
- extract conversions toDecimalValue, toIntegerValue, toIntegerSubType - construct XPathException in conversionError method - use switch-statement in convertTo for readability
1 parent 004cb5a commit 956376c

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

exist-core/src/main/java/org/exist/xquery/value/DoubleValue.java

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -166,50 +166,51 @@ public boolean isPositive() {
166166

167167
@Override
168168
public AtomicValue convertTo(final int requiredType) throws XPathException {
169-
switch (requiredType) {
170-
case Type.ITEM, Type.NUMBER, Type.ATOMIC, Type.DOUBLE -> {
171-
return this;
172-
}
173-
case Type.FLOAT -> {
174-
return new FloatValue(getExpression(), (float) value);
175-
}
176-
case Type.UNTYPED_ATOMIC -> {
177-
return new UntypedAtomicValue(getExpression(), getStringValue());
178-
}
179-
case Type.STRING -> {
180-
return new StringValue(getExpression(), getStringValue());
181-
}
182-
case Type.DECIMAL -> {
183-
if (isNaN() || isInfinite()) {
184-
throw new XPathException(getExpression(), ErrorCodes.FORG0001, "Cannot convert "
185-
+ Type.getTypeName(getType()) + "('" + getStringValue() + "') to "
186-
+ Type.getTypeName(requiredType));
187-
}
188-
return new DecimalValue(getExpression(), BigDecimal.valueOf(value));
189-
}
190-
case Type.INTEGER,
191-
Type.POSITIVE_INTEGER, Type.NEGATIVE_INTEGER,
192-
Type.NON_NEGATIVE_INTEGER, Type.NON_POSITIVE_INTEGER,
169+
return switch (requiredType) {
170+
case Type.ITEM, Type.NUMBER, Type.ATOMIC, Type.DOUBLE -> this;
171+
case Type.FLOAT -> new FloatValue(getExpression(), (float) value);
172+
case Type.UNTYPED_ATOMIC -> new UntypedAtomicValue(getExpression(), getStringValue());
173+
case Type.STRING -> new StringValue(getExpression(), getStringValue());
174+
case Type.DECIMAL -> toDecimalValue();
175+
case Type.INTEGER -> toIntegerValue();
176+
case Type.POSITIVE_INTEGER, Type.NEGATIVE_INTEGER, Type.NON_NEGATIVE_INTEGER, Type.NON_POSITIVE_INTEGER,
193177
Type.LONG, Type.INT, Type.SHORT, Type.BYTE,
194-
Type.UNSIGNED_LONG, Type.UNSIGNED_INT, Type.UNSIGNED_SHORT, Type.UNSIGNED_BYTE -> {
195-
if (isNaN() || isInfinite()) {
196-
throw new XPathException(getExpression(), ErrorCodes.FORG0001, "Cannot convert "
197-
+ Type.getTypeName(getType()) + "('" + getStringValue() + "') to "
198-
+ Type.getTypeName(requiredType));
199-
}
200-
if (requiredType != Type.INTEGER && value > Integer.MAX_VALUE) {
201-
throw new XPathException(getExpression(), ErrorCodes.FOCA0003, "Value is out of range for type "
202-
+ Type.getTypeName(requiredType));
203-
}
204-
return new IntegerValue(getExpression(), (long) value, requiredType);
205-
}
206-
case Type.BOOLEAN -> {
207-
return new BooleanValue(getExpression(), this.effectiveBooleanValue());
208-
}
209-
default -> throw new XPathException(getExpression(), ErrorCodes.FORG0001, "cannot cast '"
210-
+ Type.getTypeName(this.getItemType()) + "(\"" + getStringValue() + "\")' to "
211-
+ Type.getTypeName(requiredType));
178+
Type.UNSIGNED_LONG, Type.UNSIGNED_INT, Type.UNSIGNED_SHORT, Type.UNSIGNED_BYTE
179+
-> toIntegerSubType(requiredType);
180+
case Type.BOOLEAN -> new BooleanValue(getExpression(), effectiveBooleanValue());
181+
default -> throw conversionError(requiredType);
182+
};
183+
}
184+
185+
public DecimalValue toDecimalValue() throws XPathException {
186+
if (isNaN() || isInfinite()) {
187+
throw conversionError(Type.DECIMAL);
212188
}
189+
return new DecimalValue(getExpression(), BigDecimal.valueOf(value));
190+
}
191+
192+
public IntegerValue toIntegerValue() throws XPathException {
193+
if (isNaN() || isInfinite()) {
194+
throw conversionError(Type.INTEGER);
195+
}
196+
return new IntegerValue(getExpression(), (long) value);
197+
}
198+
199+
public IntegerValue toIntegerSubType(final int subType) throws XPathException {
200+
if (isNaN() || isInfinite()) {
201+
throw conversionError(subType);
202+
}
203+
if (subType != Type.INTEGER && value > Integer.MAX_VALUE) {
204+
throw new XPathException(getExpression(), ErrorCodes.FOCA0003, "Value is out of range for type "
205+
+ Type.getTypeName(subType));
206+
}
207+
return new IntegerValue(getExpression(), (long) value, subType);
208+
}
209+
210+
private XPathException conversionError(final int type) {
211+
return new XPathException(getExpression(), ErrorCodes.FORG0001, "Cannot convert "
212+
+ Type.getTypeName(getType()) + "('" + getStringValue() + "') to "
213+
+ Type.getTypeName(type));
213214
}
214215

215216
@Override

0 commit comments

Comments
 (0)