// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package stdgen

// **** demo classes ****
class P {
  def mP() { }
}
class C extends P {
  def mC() { }
}
class Container[T] {
  var o: Option[T] = None
  def put(o: Option[T]) { this.o = o }
  def get(): Option[T] = o
}
object StdGen {
  // **** standard generic methods ****
  def test[T](co: Container[T], o: Option[T]) {
    //co.put(Some(new P())) // no required relation between T and P
    //co.put(Some(new C())) // no required relation between T and P
    co.put(o)
    //val pcopy: Option[P] = co.get() // no required relation between T and P
    //pcopy.get.mP()
    //pcopy.get.mC()
    //val ccopy: Option[C] = co.get() // no required relation between T and P
    //ccopy.get.mP()
    //ccopy.get().mC()
    val tcopy: Option[T] = co.get()
    //tcopy.get.mP()
    //tcopy.get.mC()
  }
  def testp[T <: P](co: Container[T], o: Option[T]) {
    //co.put(Some(new P())) // problem if T is C or a subclass of C
    //co.put(Some(new C())) // problem if T is a subclass of C
    co.put(o)
    val pcopy: Option[P] = co.get()
    pcopy.get.mP()
    //pcopy.get.mC()
    //val ccopy: Option[C] = co.get() // problem if T is P
    //ccopy.get.mP()
    //ccopy.get().mC()
    val tcopy: Option[T] = co.get()
    tcopy.get.mP()
    //tcopy.get.mC()
  }
  def testc[T <: C](co: Container[T], o: Option[T]) {
    //co.put(Some(new P())) // problem if T is C or a subclass of C
    //co.put(Some(new C())) // problem if T is a subclass of C
    co.put(o)
    val pcopy: Option[P] = co.get()
    pcopy.get.mP()
    //pcopy.get.mC()
    val ccopy: Option[C] = co.get()
    ccopy.get.mP()
    ccopy.get.mC()
    val tcopy: Option[T] = co.get()
    tcopy.get.mP()
    tcopy.get.mC()
  }
  // ****
  def main(args: Array[String]): Unit = {
    testp(new Container[P](), Some(new P()));
    testp(new Container[C](), Some(new C()));
    //testc(new Container<P>(), Some(new P())); // P does not match "T extends C"
    testc(new Container[C](), Some(new C()));
  }
}
