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

Scala example source code file (Fileish.scala)

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

file, file, fileish, fileish, inputstream, inputstream, io, jar, jarentry, path, string, string

The Scala Fileish.scala source code

/* NSC -- new Scala compiler
 * Copyright 2005-2011 LAMP/EPFL
 * @author Paul Phillips
 */

package scala.tools.nsc
package io

import java.io.{ InputStream }
import java.util.jar.JarEntry

/** A common interface for File-based things and Stream-based things.
 *  (In particular, io.File and JarEntry.)
 */
class Fileish(val path: Path, val input: () => InputStream) extends Streamable.Chars {
  def inputStream() = input()

  def parent       = path.parent
  def name         = path.name
  def isSourceFile = path.hasExtension("java", "scala")
  
  private lazy val pkgLines = lines() collect { case x if x startsWith "package " => x stripPrefix "package" trim }
  lazy val pkgFromPath      = parent.path.replaceAll("""[/\\]""", ".")
  lazy val pkgFromSource    = pkgLines map (_ stripSuffix ";") mkString "."
  
  override def toString = path.path
}

object Fileish {
  def apply(f: File): Fileish = new Fileish(f, () => f.inputStream())
  def apply(f: JarEntry, in: () => InputStream): Fileish  = new Fileish(Path(f.getName), in)
  def apply(path: String, in: () => InputStream): Fileish = new Fileish(Path(path), in)
}

Other Scala examples (source code examples)

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