scalascala-java-interopagents-jaderaw-types

Overriding java method with Vector type parameter


I am implementing a ContractNetInitiator from Jade in scala and I need to override this method:

protected void handleAllResponses(java.util.Vector responses,java.util.Vector acceptances)

And implemented it like this:

override def handleAllResponses(responses: Vector[_], acceptances: Vector[_]): Unit = {
    var acceptProposal: ACLMessage = null
    var bestProposal = Integer.MAX_VALUE
    var bestProposer = ""

    // Find best proposal and reject all proposal
    val e = responses.elements()

    while (e.hasMoreElements) {
        val response: ACLMessage = e.nextElement().asInstanceOf[ACLMessage]

        if (response.getPerformative == ACLMessage.PROPOSE) {
            val reply = response.createReply()
            reply.setPerformative(ACLMessage.REJECT_PROPOSAL)
            acceptances.addElement(reply) // Can't add the reply : "Type mismatch, expected: _$1, actual: ACLMessage"

            if (response.getUserDefinedParameter("Value").toInt < bestProposal) {
                bestProposal = response.getUserDefinedParameter("Value").toInt
                bestProposer = response.getSender.getLocalName
                acceptProposal = reply
            }
        }
    }

    // Accept proposal
    if (acceptProposal != null) {
        acceptProposal.setPerformative(ACLMessage.ACCEPT_PROPOSAL)
    }
}

But when I try to add a reply to acceptances I get a Type mismatch.

I tried to change "acceptances: Vector[_]" with "acceptances: Vector[ACLMessage]" and "acceptances: Vector[Any]", but it doesn't work since it doesn't correspond with the super class.

Is there a way to add elements to acceptances ?


Solution

  • You'll need to cast it:

     acceptances.asInstanceOf[Vector[ACLMessage]].addElement(reply)
    

    Normally it's something to avoid, but in this case it's entirely the library's fault for using raw types and only documenting effective type parameters.