stevedlawrence commented on a change in pull request #83: Adding value of 'bcd'
to binaryCalendarRep
URL: https://github.com/apache/incubator-daffodil/pull/83#discussion_r203358898
##########
File path:
daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/ConvertBinaryCalendarUnparser.scala
##########
@@ -82,3 +95,110 @@ case class ConvertBinaryCalendarSecMilliUnparser(
}
}
}
+
+abstract class BinaryCalendarBCDUnparser (
+ override val context: ElementRuntimeData,
+ pattern: String,
+ localeEv: CalendarLanguageEv,
+ calendarEv: CalendarEv)
+ extends ConvertTextCalendarProcessorBase {
+
+ protected def fromBigInteger(bigInt: JBigInteger, nBits: Int): Array[Byte] =
DecimalUtils.bcdFromBigInteger(bigInt, nBits)
+
+ protected def putNumber(dos: DataOutputStream, bigDec: JBigDecimal, nBits:
Int, finfo: FormatInfo): Boolean = {
+ val packedNum = fromBigInteger(bigDec.unscaledValue, nBits)
+ dos.putByteArray(packedNum, packedNum.length * 8, finfo)
+ }
+
+ def doUnparse(state: UState, bitLength: Int): Unit = {
+
+ val locale: ULocale = localeEv.evaluate(state)
+ val calendar: Calendar = calendarEv.evaluate(state)
+
+ calendar.clear()
+
+ val df = tlDataFormatter(locale, calendar)
+ df.setCalendar(calendar)
+
+ val node = state.currentInfosetNode.asSimple
+
+ val calValue = node.dataValue match {
+ case dc: DFDLCalendar => dc.calendar
+ case x => Assert.invariantFailed("ConvertBinaryCalendar received
unsupported type. %s of type %s.".format(x, Misc.getNameFromClass(x)))
+ }
+
+ val str: String = df.format(calValue)
+
+ val strAsBigDec: JBigDecimal =
DecimalUtils.bcdToBigDecimal(DecimalUtils.bcdFromBigInteger(new
JBigInteger(str), 0), 0)
Review comment:
If I'm reading this right, it looks like this converts a string to a BigInt,
converts that to an Array[Byte], converts that to a BigDecimal. Then putNumber
converts that BigDecimal to another Array[Byte], and finally writes that byte
array. Seems like we should be able to skip the BigDeicmal and putNumber
conversions. Instead can we just do something like
```scala
val str = fs.format(calValue)
val bigInt = new JBigInteger(str)
val arr = fromBigInteger(bigInt, bitLength)
dos.putByteArray(arr, bitLength)
```
Does that effectively do the same thing? If that works, then putNumber can
be removed. Also, I think this parser then will become exactly the same for
bcd, packed, and ibm4690Packed aside from the fromBigInteger function, so those
should be quicker to implement.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services