0

My intent is to put the list of Tse objects into Bts object in Java:

    Bts Object should contain List=
    List(Tse(1285927200000,1285928100000,0.0), 
    Tse(1285928100000,1285929000000,1.0), 
    Tse(1285929000000,1285929900000,2.0), 
    Tse(1285929900000,1285930800000,3.0))

I have scala files Bts, Ts and Tse:

Bts.scala

trait Bts extends Ts{
  def start:Long
  def end:Long
  def timezone:TimeZone
}

Ts.scala

Object Ts { //Some methods }
trait Ts extends Iterator[Tse] {
  //Some methods
}

Tse.scala

case class Tse(val start:Long, val end:Long, val value:Option[Double], val elapsed:Long) {
 def this(start: java.lang.Long, end: java.lang.Long, 
 value: java.lang.Double) = {
    this(start, end, Option(value), end-start)
  }
  //Some methods
}

So, in Java code, I want to make Bts object such that it should contain the following:

List(Tse(1285927200000,1285928100000,0.0), 
Tse(1285928100000,1285929000000,1.0), 
Tse(1285929000000,1285929900000,2.0), 
Tse(1285929900000,1285930800000,3.0))

My Java code to do so:

Tse tse1= new Tse(1285927200000L, 1285928100000L, 0.0);

Tse tse2 = new Tse(1285928100000L, 1285929000000L, 1.0);

Tse tse3= new Tse(1285929000000L, 1285929900000L, 2.0);

Tse tse4 = new Tse(1285929900000L, 1285930800000L, 3.0);

List<Tse> entryList = new ArrayList<Tse>();
entryList.add(tse1);
entryList.add(tse2);
entryList.add(tse3);
entryList.add(tse4);

Bts bts= (Bts) entryList; //exception

Tsr$ tsr= Tsr$.MODULE$;
tsr.mcall(bts);

As you see that last line is giving me exception:

Caused by: java.lang.ClassCastException: java.util.ArrayList 
cannot be cast to com.testapp.people.user.Bts

UPDATE:

Tsr.scala

object Tsr {
def mcall(sourceData: Iterator[Tse]) : Bts = { //Other calls}
}

UPDATE:

I tried to create an abstract class in Bts and then tried to access it from Java:

Added the following in Bts.scala:

abstract class Bs(buffer:Iterator[Tse]) extends Bts{
}

From java:

Iterator<Tse> iterator = entryList.iterator();
Bts bts= new Bs(scala.collection.JavaConversions$.MODULE$.
asScalaIterator(iterator)); //exception

Now I am getting compile time error as:

Cannot instantiate the type Bs
sjain
  • 23,126
  • 28
  • 107
  • 185
  • Could you add Bts class ? – Spooky Jan 07 '16 at 10:18
  • Bts is a trait that I already added. – sjain Jan 07 '16 at 10:21
  • Well a `Bts` is not an `ArrayList[Tse]`. If you cast that this way, there is no surprise you get a `ClassCastException`. Could specify more clearly what you want to achieve with the last line? Do you want to create an instance of `Bts` from Java? Note that traits cannot always be implemented in Java, as per [this question](https://stackoverflow.com/questions/7637752/using-scala-traits-with-implemented-methods-in-java). Probably the best would be to provide an `abstract class` to be used from Java. – 0__ Jan 07 '16 at 11:13
  • I want to put List of Tse objects i.e. `tse1`, `tse2`, `tse3` and `tse4` into the `Bts` which is the whole purpose of the story. Later I will call some scala method whose parameter will be this bts object containing the list. Since I didn't found any correct scala way that can put all the `tse1`, `tse2`, `tse3` and `tse4` objects into list and then to `bts`. So I casted it and got `ClassCastException`. – sjain Jan 07 '16 at 11:30
  • Is it even possible to put the list into `Bts` from java? I am able to do the same from scala but getting problems here. See my update. The `mcall `method is taking `Iterator[Tse]` as argument so I need to pass that from java class which I believe is a list of `tse` objects in iterator. Also I tried creating an `abstract class` and used in java without luck. – sjain Jan 07 '16 at 12:40
  • Thanks it is working now by using class (not abstract) that acts as a wrapper for `Bts` :) – sjain Jan 07 '16 at 16:13
  • If it's working now, you should add an answer and accept your own answer. – Kyrstellaine Jan 07 '16 at 21:24

1 Answers1

0

The example you posted is a total mess of weird class names that don't match each other.

I think i understand what you're trying to do, and it's really easy if you make a bit less mess.

Also you should never refer to anything containing $ from your source code unless you really know what you are doing. It's there for the compiler to use.

Scala:

trait Data {
  val foo: Int
  val Bar: Int
}

case class DataCase(foo: Int, Bar: Int) extends Data

object Test {
  def doSomething(it: Iterator[Data]) = {
    for (x <- it) println(x)
  }
}

Java:

import scala.collection.JavaConversions;
import java.util.ArrayList;
import java.util.List;

public class TestJava {
    public static void main(String[] args) {
        List<Data> list = new ArrayList<>();

        list.add(new DataCase(1, 2));
        list.add(new DataCase(3, 4));
        list.add(new DataCase(4, 5));

        Test.doSomething(JavaConversions.asScalaIterator(list.iterator()));
    }
}
bdew
  • 1,330
  • 9
  • 22