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

Scala example source code file (phonebook3.scala)

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

elem, iterator, iterator, john, john, name, name:string, node, prettyprinter, text, text, where, where, where:string

The Scala phonebook3.scala source code

package phonebook;

object phonebook3 {

  import scala.xml.{Elem, Node, Text} ;
  import scala.xml.PrettyPrinter ;
  import Node.NoAttributes ;

  /* this method "changes" (returns an updated copy) of the phonebook when the
   *   entry for Name exists. If it has an attribute "where" whose value is equal to the
   *   parameter Where, it is changed, otherwise, it is added.
   */
  def change ( phonebook:Node, Name:String, Where:String, newPhone:String ) = {

    /** this nested function walks through tree, and returns an updated copy of it  */
    def copyOrChange ( ch: Iterator[Node] ) = {

      import xml.Utility.{trim,trimProper} //removes whitespace nodes, which are annoying in matches

      for( val c <- ch ) yield 
        trimProper(c) match {

          // if the node is the particular entry we are looking for, return an updated copy

          case x @ <entry>{ Text(Name) }{ ch1 @ _* } => 

            var updated = false;
            val ch2 = for(c <- ch1) yield c match { // does it have the phone number?

              case y @ <phone>{ _* } if y \ "@where" == Where => 
                updated = true
                <phone where={ Where }>{ newPhone }
              
              case y => y
              
            }
            if( !updated ) { // no, so we add as first entry
            
              <entry>
                <name>{ Name }
                <phone where={ Where }>{ newPhone }
                { ch1 }
              </entry>
              
            } else {         // yes, and we changed it as we should
              
              <entry>
                { ch2 }
              </entry>
        
            } 
          // end case x @ <entry>...
          
          // other entries are copied without changing them

          case x =>           
            x
          
        }
    } ; // for ... yield ... returns an Iterator[Node]
    
    // decompose phonebook, apply updates
    phonebook match {
      case <phonebook>{ ch @ _* } =>
        <phonebook>{ copyOrChange( ch.iterator ) }
    }
    
  }

  val pb2 = 
    change( phonebook1.labPhoneBook, "John", "work", "+41 55 555 55 55" );

  val pp = new PrettyPrinter( 80, 5 );

  def main( args:Array[String] ) = {
    Console.println("---before---");
    Console.println( pp.format( phonebook1.labPhoneBook ));
    Console.println("---after---");
    Console.println( pp.format( pb2 ));
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala phonebook3.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.