Skip to content

Commit

Permalink
Refactored PackedBinaryConversion trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Adams committed Dec 19, 2024
1 parent 416bf4f commit 8b0b392
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ class BCDDecimalKnownLengthParser(
) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint)
with HasKnownLengthInBits {

override def toBigInteger(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, binaryDecimalVirtualPoint)

}

Expand All @@ -48,9 +47,8 @@ class BCDDecimalRuntimeLengthParser(
) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint)
with HasRuntimeExplicitLength {

override def toBigInteger(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, binaryDecimalVirtualPoint)

}

Expand All @@ -64,9 +62,8 @@ class BCDDecimalPrefixedLengthParser(
) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint)
with PrefixedLengthParserMixin {

override def toBigInteger(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, binaryDecimalVirtualPoint)

override def childProcessors: Vector[Processor] = Vector(prefixedLengthParser)

Expand All @@ -83,19 +80,15 @@ class BCDIntegerRuntimeLengthParser(
) extends PackedBinaryIntegerBaseParser(e)
with HasRuntimeExplicitLength {

override def toBigInteger(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)

}

class BCDIntegerKnownLengthParser(e: ElementRuntimeData, val lengthInBits: Int)
extends PackedBinaryIntegerBaseParser(e)
with HasKnownLengthInBits {

override def toBigInteger(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)

}

Expand All @@ -108,9 +101,7 @@ class BCDIntegerPrefixedLengthParser(
) extends PackedBinaryIntegerBaseParser(e)
with PrefixedLengthParserMixin {

override def toBigInteger(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)

override def childProcessors: Vector[Processor] = Vector(prefixedLengthParser)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ class IBM4690PackedDecimalKnownLengthParser(
) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint)
with HasKnownLengthInBits {

override def toBigInteger(num: Array[Byte]): JBigInteger =
DecimalUtils.ibm4690ToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, binaryDecimalVirtualPoint)

}

Expand All @@ -49,10 +47,8 @@ class IBM4690PackedDecimalRuntimeLengthParser(
) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint)
with HasRuntimeExplicitLength {

override def toBigInteger(num: Array[Byte]): JBigInteger =
DecimalUtils.ibm4690ToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, binaryDecimalVirtualPoint)

}

Expand All @@ -66,10 +62,8 @@ class IBM4690PackedDecimalPrefixedLengthParser(
) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint)
with PrefixedLengthParserMixin {

override def toBigInteger(num: Array[Byte]): JBigInteger =
DecimalUtils.ibm4690ToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, binaryDecimalVirtualPoint)

override def childProcessors: Vector[Processor] = Vector(prefixedLengthParser)

Expand All @@ -86,10 +80,8 @@ class IBM4690PackedIntegerRuntimeLengthParser(
) extends PackedBinaryIntegerBaseParser(e)
with HasRuntimeExplicitLength {

override def toBigInteger(num: Array[Byte]): JBigInteger =
override def toNumber(num: Array[Byte]): JBigInteger =
DecimalUtils.ibm4690ToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, scale)

}

Expand All @@ -99,10 +91,8 @@ class IBM4690PackedIntegerKnownLengthParser(
) extends PackedBinaryIntegerBaseParser(e)
with HasKnownLengthInBits {

override def toBigInteger(num: Array[Byte]): JBigInteger =
override def toNumber(num: Array[Byte]): JBigInteger =
DecimalUtils.ibm4690ToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, scale)

}

Expand All @@ -115,10 +105,8 @@ class IBM4690PackedIntegerPrefixedLengthParser(
) extends PackedBinaryIntegerBaseParser(e)
with PrefixedLengthParserMixin {

override def toBigInteger(num: Array[Byte]): JBigInteger =
override def toNumber(num: Array[Byte]): JBigInteger =
DecimalUtils.ibm4690ToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, scale)

override def childProcessors: Vector[Processor] = Vector(prefixedLengthParser)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,15 @@ import org.apache.daffodil.runtime1.processors.dfa.TextDelimitedParserBase

import passera.unsigned.ULong

trait PackedBinaryConversion {
def toBigInteger(num: Array[Byte]): JBigInteger
def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal
trait PackedBinaryConversion[A <: Number] {
def toNumber(num: Array[Byte]): A

def toPrimitiveType(context: ElementRuntimeData, num: Array[Byte]): DataValueNumber = {
def toPrimType(context: ElementRuntimeData, num: Array[Byte]): DataValueNumber = {
context.optPrimType.get match {
case pn: PrimType.PrimNumeric => pn.fromNumber(toBigInteger(num))
case pn: PrimType.PrimNumeric => pn.fromNumber(toNumber(num))
// Non-numeric types such as Time can still use these funcitons and
// expect BigIntegers as the output of the conversion
case _ => toBigInteger(num)
}
}

def toPrimitiveType(context: ElementRuntimeData, num: Array[Byte], scale: Int): DataValueNumber = {
context.optPrimType.get match {
case pn: PrimType.PrimNumeric => pn.fromNumber(toBigDecimal(num, scale))
// Non-numeric types such as Time can still use these funcitons and
// expect BigDecimal as the output of the conversion
case _ => toBigDecimal(num, scale)
case _ => toNumber(num)
}
}
}
Expand Down Expand Up @@ -94,7 +84,7 @@ abstract class PackedBinaryDecimalBaseParser(
override val context: ElementRuntimeData,
binaryDecimalVirtualPoint: Int
) extends PrimParser
with PackedBinaryConversion
with PackedBinaryConversion[JBigDecimal]
with PackedBinaryLengthCheck {
override lazy val runtimeDependencies = Vector()

Expand All @@ -115,7 +105,7 @@ abstract class PackedBinaryDecimalBaseParser(
}

try {
val dec = toPrimitiveType(context, dis.getByteArray(nBits, start), binaryDecimalVirtualPoint)
val dec = toPrimType(context, dis.getByteArray(nBits, start))
start.simpleElement.setDataValue(dec)
} catch {
case n: NumberFormatException => PE(start, "Error in packed data: \n%s", n.getMessage())
Expand All @@ -127,7 +117,7 @@ abstract class PackedBinaryDecimalBaseParser(
abstract class PackedBinaryIntegerBaseParser(
override val context: ElementRuntimeData
) extends PrimParser
with PackedBinaryConversion
with PackedBinaryConversion[JBigInteger]
with PackedBinaryLengthCheck {
override lazy val runtimeDependencies = Vector()

Expand Down Expand Up @@ -155,7 +145,7 @@ abstract class PackedBinaryIntegerBaseParser(
}

try {
val int = toPrimitiveType(context, dis.getByteArray(nBits, start))
val int = toPrimType(context, dis.getByteArray(nBits, start))
start.simpleElement.setDataValue(int)
} catch {
case n: NumberFormatException => PE(start, "Error in packed data: \n%s", n.getMessage())
Expand All @@ -177,7 +167,7 @@ abstract class PackedBinaryIntegerDelimitedBaseParser(
fieldDFAEv,
isDelimRequired
)
with PackedBinaryConversion {
with PackedBinaryConversion[JBigInteger] {

override def processResult(parseResult: Maybe[dfa.ParseResult], state: PState): Unit = {
Assert.invariant(
Expand All @@ -196,7 +186,7 @@ abstract class PackedBinaryIntegerDelimitedBaseParser(
return
} else {
try {
val num = toPrimitiveType(context, fieldBytes)
val num = toPrimType(context, fieldBytes)
state.simpleElement.setDataValue(num)
} catch {
case n: NumberFormatException =>
Expand Down Expand Up @@ -225,7 +215,7 @@ abstract class PackedBinaryDecimalDelimitedBaseParser(
fieldDFAEv,
isDelimRequired
)
with PackedBinaryConversion {
with PackedBinaryConversion[JBigDecimal] {

/**
* We are treating packed binary formats as just a string in iso-8859-1 encoding.
Expand Down Expand Up @@ -256,7 +246,7 @@ abstract class PackedBinaryDecimalDelimitedBaseParser(
return
} else {
try {
val num = toPrimitiveType(e, fieldBytes, binaryDecimalVirtualPoint)
val num = toPrimType(e, fieldBytes)
state.simpleElement.setDataValue(num)
} catch {
case n: NumberFormatException =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ class PackedDecimalKnownLengthParser(
) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint)
with HasKnownLengthInBits {

override def toBigInteger(num: Array[Byte]): JBigInteger =
DecimalUtils.packedToBigInteger(num, packedSignCodes)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, scale, packedSignCodes)
override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, binaryDecimalVirtualPoint, packedSignCodes)

}

Expand All @@ -51,11 +49,8 @@ class PackedDecimalRuntimeLengthParser(
) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint)
with HasRuntimeExplicitLength {

override def toBigInteger(num: Array[Byte]): JBigInteger =
DecimalUtils.packedToBigInteger(num, packedSignCodes)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, scale, packedSignCodes)

override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, binaryDecimalVirtualPoint, packedSignCodes)
}

class PackedDecimalPrefixedLengthParser(
Expand All @@ -69,10 +64,8 @@ class PackedDecimalPrefixedLengthParser(
) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint)
with PrefixedLengthParserMixin {

override def toBigInteger(num: Array[Byte]): JBigInteger =
DecimalUtils.packedToBigInteger(num, packedSignCodes)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, scale, packedSignCodes)
override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, binaryDecimalVirtualPoint, packedSignCodes)

override def childProcessors: Vector[Processor] = Vector(prefixedLengthParser)

Expand All @@ -90,10 +83,8 @@ class PackedIntegerRuntimeLengthParser(
) extends PackedBinaryIntegerBaseParser(e)
with HasRuntimeExplicitLength {

override def toBigInteger(num: Array[Byte]): JBigInteger =
override def toNumber(num: Array[Byte]): JBigInteger =
DecimalUtils.packedToBigInteger(num, packedSignCodes)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, scale, packedSignCodes)

}

Expand All @@ -104,10 +95,8 @@ class PackedIntegerKnownLengthParser(
) extends PackedBinaryIntegerBaseParser(e)
with HasKnownLengthInBits {

override def toBigInteger(num: Array[Byte]): JBigInteger =
override def toNumber(num: Array[Byte]): JBigInteger =
DecimalUtils.packedToBigInteger(num, packedSignCodes)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, scale, packedSignCodes)

}

Expand All @@ -121,10 +110,8 @@ class PackedIntegerPrefixedLengthParser(
) extends PackedBinaryIntegerBaseParser(e)
with PrefixedLengthParserMixin {

override def toBigInteger(num: Array[Byte]): JBigInteger =
override def toNumber(num: Array[Byte]): JBigInteger =
DecimalUtils.packedToBigInteger(num, packedSignCodes)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, scale, packedSignCodes)

override def childProcessors: Vector[Processor] = Vector(prefixedLengthParser)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,8 @@ class PackedIntegerDelimitedParser(
packedSignCodes: PackedSignCodes
) extends PackedBinaryIntegerDelimitedBaseParser(erd, textParser, fieldDFAEv, isDelimRequired) {

override def toBigInteger(num: Array[Byte]): JBigInteger =
override def toNumber(num: Array[Byte]): JBigInteger =
DecimalUtils.packedToBigInteger(num, packedSignCodes)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, scale, packedSignCodes)

}

Expand All @@ -238,10 +236,8 @@ class PackedDecimalDelimitedParser(
binaryDecimalVirtualPoint
) {

override def toBigInteger(num: Array[Byte]): JBigInteger =
DecimalUtils.packedToBigInteger(num, packedSignCodes)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, scale, packedSignCodes)
override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.packedToBigDecimal(num, binaryDecimalVirtualPoint, packedSignCodes)

}

Expand All @@ -252,9 +248,7 @@ class BCDIntegerDelimitedParser(
isDelimRequired: Boolean
) extends PackedBinaryIntegerDelimitedBaseParser(erd, textParser, fieldDFAEv, isDelimRequired) {

override def toBigInteger(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)

}

Expand All @@ -272,9 +266,8 @@ class BCDDecimalDelimitedParser(
binaryDecimalVirtualPoint
) {

override def toBigInteger(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, binaryDecimalVirtualPoint)

}

Expand All @@ -285,10 +278,8 @@ class IBM4690PackedIntegerDelimitedParser(
isDelimRequired: Boolean
) extends PackedBinaryIntegerDelimitedBaseParser(erd, textParser, fieldDFAEv, isDelimRequired) {

override def toBigInteger(num: Array[Byte]): JBigInteger =
override def toNumber(num: Array[Byte]): JBigInteger =
DecimalUtils.ibm4690ToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, scale)

}

Expand All @@ -306,9 +297,7 @@ class IBM4690PackedDecimalDelimitedParser(
binaryDecimalVirtualPoint
) {

override def toBigInteger(num: Array[Byte]): JBigInteger =
DecimalUtils.ibm4690ToBigInteger(num)
override def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, scale)
override def toNumber(num: Array[Byte]): JBigDecimal =
DecimalUtils.ibm4690ToBigDecimal(num, binaryDecimalVirtualPoint)

}

0 comments on commit 8b0b392

Please sign in to comment.