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

Scala example source code file (absoverride.scala)

This example Scala source code file (absoverride.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

absiterator, absiterator, boolean, iter, loggediterator, richiterator, string, stringiterator, stringiterator, synciterator, synciterator, t, t, test

The Scala absoverride.scala source code

abstract class AbsIterator {
  type T
  def hasNext: Boolean
  def next: T
}

trait RichIterator extends AbsIterator {
  def foreach(f: T => Unit) {
    while (hasNext) f(next)
  }
}

class StringIterator(s: String) extends AbsIterator {
  type T = Char
  private var i = 0
  def hasNext = i < s.length()
  def next = { val x = s.charAt(i); i += 1; println("next: " + x); x }
}

trait SyncIterator extends AbsIterator {
  abstract override def hasNext: Boolean =
    synchronized(super.hasNext)
  abstract override def next: T =
    synchronized {
      println("<sync>"); val x = super.next; println(""); x
    }
}
trait LoggedIterator extends AbsIterator {
  abstract override def next: T = { 
    val x = super.next; println("log: " + x); x 
  }
}
class Iter2(s: String) extends StringIterator(s) 
               with SyncIterator with LoggedIterator;         
object Test {
  def main(args: Array[String]) {
    class Iter extends StringIterator(args(0)) with RichIterator with SyncIterator with LoggedIterator
    val iter = new Iter
    iter foreach Console.println 
  }
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.