Home > other >  Scala associated object initialization problems refer to a piece of code
Scala associated object initialization problems refer to a piece of code

Time:09-21

Here I want to have mockLoadFile when mP object initialization, but don't know what to write, seek advice on how to modify the associated constructor to like?
In charge of not quite seem to apply?

 package test 

Trait PartitionStrategy extends the Serializable {
/* * Returns the partition number for a given edge. */
Def getPartition (key: Int) : Int
}

The object PartitionStrategy {
Def fromString (s: String) : PartitionStrategy=s match {
Case "MetisPartition=& gt;" MetisPartition
Case _=& gt; Throw new IllegalArgumentException (" Invalid PartitionStrategy: "+ s)
}

In case the object MetisPartition extends PartitionStrategy {
Val metisMap=new scala. Collection. The mutable. HashMap (Int, Int)
Def mockLoadFile () {
Try {
Var id=1;
While (id & lt; 10000)
{
The var partition=5;
MetisMap. Put (id, partition)
Id=id + 1
}
Println (" size: "+ metisMap. Size)
} catch {
Case ex: Exception=& gt; {//Handle missing file
Ex. PrintStackTrace ()
}
}
}

Override def getPartition (SRC: Int) : Int={
Var s=metisMap. Get (SRC)
if(! S.i sEmpty)
Return s.g etOrElse (0)
The else
Throw new IllegalArgumentException (" Metis can 't find partition!" )
}
}
}


Package test

The object TestPartition {

Def main (args: Array [String]) : Unit={
Try {
Var mP=PartitionStrategy. FromString (" MetisPartition ")
Println (mP. GetPartition (110))
} catch {
Case ex: Exception=& gt; {
Ex. PrintStackTrace ()
}
}
}
}
  • Related