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
Btsis not anArrayList[Tse]. If you cast that this way, there is no surprise you get aClassCastException. Could specify more clearly what you want to achieve with the last line? Do you want to create an instance ofBtsfrom Java? Note that traits cannot always be implemented in Java, as per this question. Probably the best would be to provide anabstract classto be used from Java.tse1,tse2,tse3andtse4into theBtswhich 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 thetse1,tse2,tse3andtse4objects into list and then tobts. So I casted it and gotClassCastException.Btsfrom java? I am able to do the same from scala but getting problems here. See my update. Themcallmethod is takingIterator[Tse]as argument so I need to pass that from java class which I believe is a list oftseobjects in iterator. Also I tried creating anabstract classand used in java without luck.