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

Scala example source code file (NodeBuffer.scala)

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

any, any, array, array, atom, iterable, iterator, node, node, nodebuffer, nodebuffer, text, unit

The Scala NodeBuffer.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */


package scala.xml

/**
 * <p>
 *   This class acts as a Buffer for nodes. If it is used as a sequence
 *   of nodes <code>Seq[Node], it must be ensured that no updates
 *   occur after that point, because <code>scala.xml.Node is assumed
 *   to be immutable.
 * </p>
 * <p>
 *   Despite this being a sequence, don't use it as key in a hashtable.
 *   Calling the hashcode function will result in a runtime error.
 * </p>
 *
 * @author  Burak Emir
 * @version 1.0
 */
class NodeBuffer extends scala.collection.mutable.ArrayBuffer[Node] {

  /**
   * Append given object to this buffer, returns reference on this NodeBuffer 
   * for convenience. Some rules apply: If o is null, it is ignored. If it is 
   * an Iterator or Iterable, its elements will be added. If o is a node, it is
   * added as it is. If it is anything else, it gets wrapped in an Atom.
   *
   * @param o converts to an xml node and adds to this node buffer
   * @return  this nodebuffer
   */
  def &+(o: Any): NodeBuffer = {
    o match {
      case null | _: Unit | Text("")  => // ignore
      case it: Iterator[_]            => it foreach &+
      case n: Node                    => super.+=(n)
      case ns: Iterable[_]            => this &+ ns.iterator
      case ns: Array[_]               => this &+ ns.iterator
      case d                          => super.+=(new Atom(d))
    }
    this
  }
}

Other Scala examples (source code examples)

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