English 中文(简体)
我如何用抽象的类型来建立分类的工厂方法构造者,并从Schala进入这些工厂?
原标题:How do I create a class hierarchy of typed factory method constructors and access them from Scala using abstract types?
  • 时间:2011-11-17 05:00:44
  •  标签:
  • scala

(依次,我需要对上述两个问题进行某种综合(1 ,<2/a/a>,但Imartm 并不够合)。

我有一组日本宇宙航空研究开发机构的代表,如:

abstract class Representation {
  def marshalToXml(): String = {
    val context = JAXBContext.newInstance(this.getClass())
    val writer = new StringWriter
    context.createMarshaller.marshal(this, writer)
    writer.toString()
  }
}

class Order extends Representation {
  @BeanProperty
  var name: String = _
  ...
}
class Invoice extends Representation { ... }

我的问题是我的“施工者”方法:

def unmarshalFromJson(marshalledData: String): {{My Representation Subclass}} = {
  val mapper = new ObjectMapper()
  mapper.getDeserializationConfig().withAnnotationIntrospector(new JaxbAnnotationIntrospector())
  mapper.readValue(marshalledData, this.getClass())
}

def unmarshalFromXml(marshalledData: String): {{My Representation Subclass}} = {
  val context = JAXBContext.newInstance(this.getClass())
  val representation = context.createUnmarshaller().unmarshal(
    new StringReader(marshalledData)
  ).asInstanceOf[{{Type of My Representation Subclass}}]
  representation // Return the representation
}

具体来说,我可以指出,如何把这些不区分的方法以安全的方式和分国家的方式传给我的每一个班子,然后从Schala(希望有时只使用抽象的类型信息)。 换言之,我要这样做:

val newOrder = Order.unmarshalFromJson(someJson)

更有雄心的是:

class Resource[R <: Representation] {
    getRepresentation(marshalledData: String): R = 
        {{R s Singleton}}.unmarshalFromXml(marshalledData)
}

就我的特殊 blocks脚石而言:

I know this is a bit of a mammoth question touching on various different (but related) issues - any help gratefully received!

* A/63/150。

最佳回答

关键不是试图将这种方法推向上,而是把它作为参数。 说明你所期望的类型,让哪类系统处理。 我试图让那些读写略微读写法的无色职业。

val order = UnMarshalXml( xml ).toRepresentation[Order]

下面是一部可充分检验的法典。

abstract class Representation {
  def marshalToXml(): String = {
      val context = JAXBContext.newInstance(this.getClass)
      val writer = new StringWriter
      context.createMarshaller.marshal(this, writer)
      writer.toString
  }
}

@XmlRootElement
class Order extends Representation {
  @BeanProperty
  var name: String = _
}

case class UnMarshalXml( xml: String ) {
  def toRepresentation[T <: Representation](implicit m:Manifest[T]): T = {
    JAXBContext.newInstance(m.erasure).createUnmarshaller().unmarshal(
      new StringReader(xml)
    ).asInstanceOf[T]
  }
}

object test {
  def main( args: Array[String] ) {
    val order = new Order
    order.name = "my order"
    val xml = order.marshalToXml()
    println("marshalled: " + xml )
    val received = UnMarshalXml( xml ).toRepresentation[Order]
    println("received order named: " + received.getName )
  }
}

如果你进行试验,你应看到以下产出。 主要活动

marshalled: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><order><name>my order</name></order>
received name: my order
问题回答

这里是我用来支持第二个使用案例的尼尔斯法典最新版本,以及第一个版本:

case class UnmarshalXml(xml: String) {

  def toRepresentation[T <: Representation](implicit m: Manifest[T]): T =
    toRepresentation[T](m.erasure.asInstanceOf[Class[T]])

  def toRepresentation[T <: Representation](typeT: Class[T]): T =
    JAXBContext.newInstance(typeT).createUnmarshaller().unmarshal(
      new StringReader(xml)
    ).asInstanceOf[T]
}

这支持简单的例子:

val order = UnmarshalXml(xml).toRepresentation[Order]

但是,对于抽象的类型使用,你也可以这样做:

val order = UnmarshalXml(xml).toRepresentation[T](typeOfT)

(如果您在发布<条码>时使用另一条<条码>简单明了/条码>。





相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签