alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (t1143-2.scala)

This example Scala source code file (t1143-2.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

a, anyref, array, array, component, form, printer, serialversionuid, serialversionuid, t, t, unit, varmodel, varmodel

The Scala t1143-2.scala source code

object Serialize {
  @throws(classOf[java.io.IOException])
  def write[A](o: A): Array[Byte] = {
    val ba = new java.io.ByteArrayOutputStream(512)
    val out = new java.io.ObjectOutputStream(ba)
    out.writeObject(o)
    out.close()
    ba.toByteArray()
  }
  @throws(classOf[java.io.IOException])
  @throws(classOf[ClassNotFoundException])
  def read[A](buffer: Array[Byte]): A = {
    val in =
      new java.io.ObjectInputStream(new java.io.ByteArrayInputStream(buffer))
    in.readObject().asInstanceOf[A]
  }
}

@serializable
@SerialVersionUID(1L)
class VarModel[T]( getter: => T, setter: T => Unit )
{
  Serialize.write(getter)
  Serialize.write(setter)

  def this( getter: => T ) = this( getter, null )

  def getObject: AnyRef = getter.asInstanceOf[AnyRef]

  def setObject( v: AnyRef ) = {
    if( setter==null )
      throw new RuntimeException( "Tried to set readonly model!")
    setter( v.asInstanceOf[T] )
  }

  def detach = ()
}

@serializable
@SerialVersionUID(1L)
class Printer( p: VarModel[String] ) {
  def print = println( p.getObject );
}

class Component extends Marker { }

class Form extends Component { }

@serializable
@SerialVersionUID(1L)
class Main {
  var pass = "pass"
  def main(args : Array[String]) : Unit = {
    val f = new Form {
      val p = new Printer( new VarModel( pass, s => pass = s ) );
      p.print
    }
    ()
  }
}

object Test {
  def main(args: Array[String]) {
    (new Main).main(Array[String]())
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala t1143-2.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.