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

Scala example source code file (package.scala)

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

boolean, file, file, io, list, no, option, path, path, set, string, string, stringwriter, stringwriter, util

The Scala package.scala source code

/* NEST (New Scala Test)
 * Copyright 2007-2011 LAMP/EPFL
 */

package scala.tools
package partest

import java.util.{ Timer, TimerTask }
import java.io.StringWriter
import nsc.io._

/** Misc code still looking for a good home.
 */
package object util {
  
  def allPropertiesString() = javaHashtableToString(System.getProperties)

  private def javaHashtableToString(table: java.util.Hashtable[_,_]) = {
    import collection.JavaConversions._
    (table.toList map { case (k, v) => "%s -> %s\n".format(k, v) }).sorted mkString
  }
  
  def filesToSet(pre: String, fs: List[String]): Set[AbstractFile] =
    fs flatMap (x => Option(AbstractFile getFile (Path(pre) / x).path)) toSet
  
  /** Copies one Path to another Path, trying to be sensible when one or the
   *  other is a Directory.  Returns true if it believes it succeeded.
   */
  def copyPath(from: Path, to: Path): Boolean = {
    if (!to.parent.isDirectory)
      to.parent.createDirectory(force = true)
    
    def copyDir = {
      val sub = to / from.name createDirectory true
      from.toDirectory.list forall (x => copyPath(x, sub))
    }
    (from.isDirectory, to.isDirectory) match {
      case (true, true)   => copyDir
      case (true, false)  => false
      case (false, true)  => from.toFile copyTo (to / from.name)
      case (false, false) => from.toFile copyTo to
    }
  }

  /**
  * Compares two files using a Java implementation of the GNU diff
  * available at http://www.bmsi.com/java/#diff.
  *
  * @param  f1  the first file to be compared
  * @param  f2  the second file to be compared
  * @return the text difference between the compared files
  */
  def diffFiles(f1: File, f2: File): String = {
    val diffWriter = new StringWriter
    val args = Array(f1.toAbsolute.path, f2.toAbsolute.path)

    io.DiffPrint.doDiff(args, diffWriter)
    val result = diffWriter.toString
    if (result == "No differences") "" else result
  }
}

Other Scala examples (source code examples)

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