You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@daffodil.apache.org by GitBox <gi...@apache.org> on 2019/07/24 22:15:31 UTC

[GitHub] [incubator-daffodil] bsloane1650 commented on a change in pull request #264: WIP - DAFFODIL-2169

bsloane1650 commented on a change in pull request #264: WIP - DAFFODIL-2169
URL: https://github.com/apache/incubator-daffodil/pull/264#discussion_r307044273
 
 

 ##########
 File path: daffodil-lib/src/main/scala/org/apache/daffodil/util/DataValue.scala
 ##########
 @@ -0,0 +1,91 @@
+package org.apache.daffodil.infoset
+
+import java.lang.{ Boolean => JBoolean, Number => JNumber, Long => JLong, Double => JDouble, String => JString, Float => JFloat, Byte => JByte, Integer => JInt, Short => JShort }
+import java.math.{ BigDecimal => JBigDecimal, BigInteger => JBigInt }
+import org.apache.daffodil.calendar.DFDLCalendar
+
+/*
+ * DINode is not visible to Daffodil-lib, so we cannot actually reference this here.
+ * We are trusting the rest of the codebase to only apply DataValueDINode to the DINode
+ * class.
+ * The DINode class itself defines an implicit DataValueDINode -> DINode conversion
+ */
+trait DataValueDINodeTrait extends NonNullable;
+
+trait NonNullable;
+trait Nullable extends NonNullable;
+sealed trait DataValuePrimitiveType
+
+final class DataValue[+T <: AnyRef, +X<:AnyRef] private (val v: T) extends AnyVal {
+  def isEmpty = DataValue.NoValue.v eq v
+  def isDefined = !isEmpty
+  def value = v
+  override def toString = if (isEmpty) "NoValue" else "DataValue("+ v.toString +")"
+  
+  def getAnyRef = v.asInstanceOf[AnyRef]
+  def getBigDecimal = v.asInstanceOf[JBigDecimal]
+  def getCalendar = v.asInstanceOf[DFDLCalendar]
+  def getByteArray = v.asInstanceOf[Array[Byte]]
+  def getBoolean = v.asInstanceOf[JBoolean]
+  def getNumber = v.asInstanceOf[JNumber]
+  def getLong = v.asInstanceOf[JLong]
+  def getDouble = v.asInstanceOf[JDouble]
+  def getBigInt = v.asInstanceOf[JBigInt]
+  def getString = v.asInstanceOf[JString]
+}
+
+
+object DataValue {
+  type DataValueAny = DataValue[AnyRef, Nullable]
+  type DataValueNonEmpty = DataValue[AnyRef, NonNullable]
+  type DataValuePrimitive = DataValue[AnyRef, DataValuePrimitiveType]
+  
+  type DataValueEmpty = DataValue[NoValueObj, Nullable with DataValuePrimitiveType]
+  type DataValueBigDecimal = DataValue[JBigDecimal, NonNullable with DataValuePrimitiveType]
+  type DataValueCalendar = DataValue[DFDLCalendar, NonNullable with DataValuePrimitiveType]
+  type DataValueByteArray = DataValue[Array[Byte], NonNullable with DataValuePrimitiveType]
+  type DataValueBool = DataValue[JBoolean, NonNullable with DataValuePrimitiveType]
+  type DataValueNumber = DataValue[JNumber, NonNullable with DataValuePrimitiveType]  
+  type DataValueLong = DataValue[JLong, NonNullable with DataValuePrimitiveType]  
+  type DataValueDouble = DataValue[JDouble, NonNullable with DataValuePrimitiveType]  
+  type DataValueBigInt = DataValue[JBigInt, NonNullable with DataValuePrimitiveType] 
+  type DataValueString = DataValue[JString, NonNullable with DataValuePrimitiveType] 
+  type DataValueFloat = DataValue[JFloat, NonNullable with DataValuePrimitiveType]
+  type DataValueByte = DataValue[JByte, NonNullable with DataValuePrimitiveType]
+  type DataValueInt = DataValue[JInt, NonNullable with DataValuePrimitiveType]
+  type DataValueShort = DataValue[JShort, NonNullable with DataValuePrimitiveType]
+  type DataValueDINode = DataValue[DataValueDINodeTrait, NonNullable with DataValuePrimitiveType]
+
+  import scala.language.implicitConversions
+    
+  implicit def toDataValue(v: JBigDecimal): DataValueBigDecimal = new DataValue(v)
+  implicit def toDataValue(v: DFDLCalendar) : DataValueCalendar = new DataValue(v)
+  implicit def toDataValue(v: Array[Byte]) : DataValueByteArray = new DataValue(v)
+  implicit def toDataValue(v: JBoolean) : DataValueBool = new DataValue(v)
+  implicit def toDataValue(v: JNumber) : DataValueNumber = new DataValue(v)
+  implicit def toDataValue(v: JLong) : DataValueLong = new DataValue(v)
+  implicit def toDataValue(v: JDouble) : DataValueDouble = new DataValue(v)
+  implicit def toDataValue(v: JBigInt) : DataValueBigInt = new DataValue(v)
+  implicit def toDataValue(v: JString) : DataValueString = new DataValue(v)
+  implicit def toDataValue(v: JFloat) : DataValueFloat = new DataValue(v)
+  implicit def toDataValue(v: JByte) : DataValueByte = new DataValue(v)
+  implicit def toDataValue(v: JInt) : DataValueInt = new DataValue(v)
+  implicit def toDataValue(v: JShort) : DataValueShort = new DataValue(v)
+  implicit def toDataValue(v: DataValueDINodeTrait) : DataValueDINode = new DataValue(v)
+  
+  
+  implicit def toDataValue(v: Long) : DataValueLong = new DataValue(v:JLong)
+  implicit def toDataValue(v: Double) : DataValueDouble = new DataValue(v:JDouble)
+  implicit def toDataValue(v: Boolean) : DataValueBool = new DataValue(v:JBoolean)
+  implicit def toDataValue(v: Float) : DataValueFloat = new DataValue(v:JFloat)
+  implicit def toDataValue(v: Byte) : DataValueByte = new DataValue(v:JByte)
+  implicit def toDataValue(v: Int) : DataValueInt = new DataValue(v:JInt)
+  implicit def toDataValue(v: Short) : DataValueShort = new DataValue(v:JShort)
+
+  val NoValue = new DataValue(new NoValueObj)
+
+}
+
+protected class NoValueObj {
 
 Review comment:
   Turned into an object. I cannot make it private, because "val NoValue = new DataValue(NoValueObj)" causes it to escape its defining scope.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services