package util
The package o1.util
contains miscellaneous tools that are used internally by some of the
given programs in O1 for added convenience.
NOTE TO STUDENTS: In this course, you don't need to understand how this package works or can be used.
This documentation lists only some of the tools in the package. The listed tools are largely a mix of:
- functions for simple I/O from files and URLs;
- aliases for easy access (via
import o1.util._
) to some of the tools fromscala.util
; and - implicit classes that add a few convenience methods to selected types from the Scala API.
- Alphabetic
- By Inheritance
- util
- iofuncs
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- type Closeable = AutoCloseable
A structural supertype for everything that has a
close
method.A structural supertype for everything that has a
close
method. This is an alias forjava.lang.AutoCloseable
.- Definition Classes
- iofuncs
- See also
- implicit final class ConvenientCollection[Element, Collection[Element] <: Iterable[Element]] extends AnyVal
This class extends the interface of
scala.Iterable
with convenience methods. - implicit final class ConvenientDouble extends AnyVal
This class extends the interface of
Double
with convenience methods. - implicit final class ConvenientFloat extends AnyVal
This class extends the interface of
Float
with convenience methods. - implicit final class ConvenientInt extends AnyVal
This class extends the interface of
Int
with convenience methods. - implicit final class ConvenientLong extends AnyVal
This class extends the interface of
Long
with convenience methods. - implicit final class ConvenientMap[Key, Value] extends AnyVal
This class extends the interface of
scala.Map
with convenience methods. - implicit final class ConvenientOption[Content] extends AnyVal
This class extends the interface of
Option
with convenience methods. - implicit class ConvenientPath extends AnyRef
This class extends the interface of Java’s
Path
with convenience methods.This class extends the interface of Java’s
Path
with convenience methods.- Definition Classes
- iofuncs
- implicit final class ConvenientSeq[Element] extends AnyVal
This class extends the interface of
scala.Seq
with convenience methods. - type Failure[T] = scala.util.Failure[T]
An alias for convenient use of
scala.util.Try
viaimport o1.util._
. - type Path = java.nio.file.Path
An alias for convenient use of
java.nio.file.Path
viaimport o1.util._
. - type Random = scala.util.Random
An alias for convenient use of
scala.util.Random
viaimport o1.util._
. - type Source = scala.io.Source
An alias for convenient use of
scala.io.Source
viaimport o1.util._
. - type Success[T] = scala.util.Success[T]
An alias for convenient use of
scala.util.Try
viaimport o1.util._
. - type Try[T] = scala.util.Try[T]
An alias for convenient use of
scala.util.Try
viaimport o1.util._
. - type URL = java.net.URL
An alias for convenient use of
java.net.URL
viaimport o1.util._
.
Value Members
- val DoubleOrdering: TotalOrdering.type
An alias for convenient use of
scala.math.Ordering.Double.TotalOrdering
viaimport o1.util._
. - val Failure: scala.util.Failure.type
An alias for convenient use of
scala.util.Try
viaimport o1.util._
. - def Path(url: URL): Path
Constructs a new
Path
object from a given string URL.Constructs a new
Path
object from a given string URL.- Definition Classes
- iofuncs
- def Path(name: String): Path
Constructs a new
Path
object from a given string.Constructs a new
Path
object from a given string.- Definition Classes
- iofuncs
- val Random: scala.util.Random.type
An alias for convenient use of
scala.util.Random
viaimport o1.util._
. - val Source: scala.io.Source.type
An alias for convenient use of
scala.io.Source
viaimport o1.util._
. - val Success: scala.util.Success.type
An alias for convenient use of
scala.util.Try
viaimport o1.util._
. - val Try: scala.util.Try.type
An alias for convenient use of
scala.util.Try
viaimport o1.util._
. - def findReadable(name: String): Option[Path]
Searches the working directory and, failing that, the classpath for a readable file or folder with the given relative path.
Searches the working directory and, failing that, the classpath for a readable file or folder with the given relative path. Returns the
Path
in anOption
wrapper.- Definition Classes
- iofuncs
- def forEachLine(source: Source)(effect: (String) => Unit): Unit
Calls
getLines
on the givenSource
and performs the specifiedeffect
on each of the lines.Calls
getLines
on the givenSource
and performs the specifiedeffect
on each of the lines. Callsclose()
on the source even if an exception occurs, but doesn’t catch any exceptions.- Definition Classes
- iofuncs
- def ifImplemented[Result](computation: => Result): Option[Result]
Performs a given computation and checks to see if it crashes with a
NotImplementedError
.Performs a given computation and checks to see if it crashes with a
NotImplementedError
. Returns the result of the computation in anOption
; returnsNone
only in the computation wasn’t implemented. Any other exception is thrown.- Result
the type of the given computation
- computation
a computation that may or may not be implemented
- def isImplemented[Result](computation: => Result): Boolean
Performs a given computation and determines whether it crashes with a
NotImplementedError
.Performs a given computation and determines whether it crashes with a
NotImplementedError
. Returnstrue
if it did andfalse
otherwise.- Result
the type of the given computation
- computation
a computation that may or may not be implemented
- def localFile(resourcePath: String): Option[Path]
Searches the classpath for a file with the given relative path and returns the corresponding
Path
.Searches the classpath for a file with the given relative path and returns the corresponding
Path
. The result comes in anOption
wrapper in case no such file is accessible.- Definition Classes
- iofuncs
- def localSource(resourcePath: String): Option[Source]
Searches the classpath for a file with the given relative path and returns a corresponding
Source
.Searches the classpath for a file with the given relative path and returns a corresponding
Source
. The result comes in anOption
wrapper in case no such file is accessible. Assumes UTF-8 encoding.- Definition Classes
- iofuncs
- def localURL(resourcePath: String): Option[URL]
Searches the classpath for a file with the given relative path.
Searches the classpath for a file with the given relative path. Delegates the task to the class loader. Returns the URL of any found file in an
Option
wrapper.- Definition Classes
- iofuncs
- def readFileLines(relativePath: String, trimEach: Boolean = true, excludeIfEmpty: Boolean = true): Option[Vector[String]]
Reads and returns the lines from a classpath-relative text file.
Reads and returns the lines from a classpath-relative text file. The result comes in an
Option
wrapper in case no such file is accessible. Callsclose()
on the file even if an exception occurs, but doesn’t catch any exceptions. Assumes UTF-8 encoding.- relativePath
the path to a resource; relative to a classpath entry
- trimEach
whether to remove whitespace around each of the lines
- excludeIfEmpty
whether to exclude (possibly trimmed) empty lines from the return value
- Definition Classes
- iofuncs
- def readLinesFromSource(source: Source, trimEach: Boolean = true, excludeIfEmpty: Boolean = true): Vector[String]
Calls
getLines
on the givenSource
and returns the lines in a vector.Calls
getLines
on the givenSource
and returns the lines in a vector. Callsclose()
on the source even if an exception occurs, but doesn’t catch any exceptions.- source
the source to read the lines from
- trimEach
whether to remove whitespace around each of the lines
- excludeIfEmpty
whether to exclude (possibly trimmed) empty lines from the return value
- Definition Classes
- iofuncs
- def readTextFile(relativePath: String): Option[String]
Returns, as a single
String
, the entire contents of a classpath-relative text file.Returns, as a single
String
, the entire contents of a classpath-relative text file. The result comes in anOption
wrapper in case no such file is accessible. Callsclose()
on the file even if an exception occurs, but doesn’t catch any exceptions. Assumes UTF-8 encoding.- relativePath
the path to a resource; relative to a classpath entry
- Definition Classes
- iofuncs
- val reflect: JavaUniverse
An alias for convenient access to the macros in
scala.reflect.runtime.universe
viaimport o1.util._
. - def tryForSource(pathOrURL: String): Try[Source]
Takes in a path or a URL as a string and tries to construct a corresponding
Source
.Takes in a path or a URL as a string and tries to construct a corresponding
Source
. Tries to interpret the given string as one of the following, in order:- A resource relative to the classpath (as per localURL).
- A complete URL string, with protocol and all (e.g., "http://example.com/").
- A URL string with "http://" missing (e.g., "example.com").
- A URL string with "https://" missing.
Calls
scala.io.Source.fromURL
on each of those candidates until a source is successfully constructed or all attempts have failed. Assumes UTF-8 encoding.
- pathOrURL
the possible path or URL to a resource
- returns
the first
Success
, or aFailure
if none of the candidates can be read as aSource
- Definition Classes
- iofuncs
- def tryForURL(pathOrURL: String): Try[URL]
Takes in a path or a URL as a string and tries to locate the corresponding resource.
Takes in a path or a URL as a string and tries to locate the corresponding resource. Tries to interpret the given string as one of the following (in order until a match is found):
- A resource relative to the classpath (as per localURL).
- A complete URL string, with protocol and all (e.g., "http://example.com/").
- A URL string with "http://" missing (e.g., "example.com").
- pathOrURL
the possible path or URL to a resource
- returns
the first
Success
, or aFailure
if the string isn’t even valid for forming a URL
- Definition Classes
- iofuncs
- def useAndClose[Resource <: Closeable, Result](resource: Resource)(operation: (Resource) => Result): Result
Attempts to apply the given
operation
to the givenresource
and returns the result, making sure toclose
the resource.Attempts to apply the given
operation
to the givenresource
and returns the result, making sure toclose
the resource. Callsclose()
on the resource even if the attempt fails with an exception. Doesn’t catch any exceptions. (This in an implementation of the so-called “loan pattern”.) Since Scala 2.13, the functionality is available in the standard API, and this is now just an alias forscala.util.Using.resource
.)- resource
a resource, such as a file, that has a
close()
method- operation
an operation that can be applied to
resource
- Definition Classes
- iofuncs
- def workingDir: String
An alias for convenient access to
java.lang.System.getProperty("user.dir")
viaimport o1.util._
. - def writeTextFile(filePath: String, text: String): Unit
Writes the given
text
in a new text file at the givenfilePath
.Writes the given
text
in a new text file at the givenfilePath
. Overwrites any existing file there. Encodes the output as UTF-8. Callsclose()
on the output stream even if an exception occurs, but doesn’t catch any exceptions.- filePath
an absolute path to a local file or a path relative to the working directory
- Definition Classes
- iofuncs