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
closemethod.A structural supertype for everything that has a
closemethod. 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.Iterablewith convenience methods. - implicit final class ConvenientDouble extends AnyVal
This class extends the interface of
Doublewith convenience methods. - implicit final class ConvenientFloat extends AnyVal
This class extends the interface of
Floatwith convenience methods. - implicit final class ConvenientInt extends AnyVal
This class extends the interface of
Intwith convenience methods. - implicit final class ConvenientLong extends AnyVal
This class extends the interface of
Longwith convenience methods. - implicit final class ConvenientMap[Key, Value] extends AnyVal
This class extends the interface of
scala.Mapwith convenience methods. - implicit final class ConvenientOption[Content] extends AnyVal
This class extends the interface of
Optionwith convenience methods. - implicit class ConvenientPath extends AnyRef
This class extends the interface of Java’s
Pathwith convenience methods.This class extends the interface of Java’s
Pathwith convenience methods.- Definition Classes
- iofuncs
- implicit final class ConvenientSeq[Element] extends AnyVal
This class extends the interface of
scala.Seqwith convenience methods. - type Failure[T] = scala.util.Failure[T]
An alias for convenient use of
scala.util.Tryviaimport o1.util._. - type Path = java.nio.file.Path
An alias for convenient use of
java.nio.file.Pathviaimport o1.util._. - type Random = scala.util.Random
An alias for convenient use of
scala.util.Randomviaimport o1.util._. - type Source = scala.io.Source
An alias for convenient use of
scala.io.Sourceviaimport o1.util._. - type Success[T] = scala.util.Success[T]
An alias for convenient use of
scala.util.Tryviaimport o1.util._. - type Try[T] = scala.util.Try[T]
An alias for convenient use of
scala.util.Tryviaimport o1.util._. - type URL = java.net.URL
An alias for convenient use of
java.net.URLviaimport o1.util._.
Value Members
- val DoubleOrdering: TotalOrdering.type
An alias for convenient use of
scala.math.Ordering.Double.TotalOrderingviaimport o1.util._. - val Failure: scala.util.Failure.type
An alias for convenient use of
scala.util.Tryviaimport o1.util._. - def Path(url: URL): Path
Constructs a new
Pathobject from a given string URL.Constructs a new
Pathobject from a given string URL.- Definition Classes
- iofuncs
- def Path(name: String): Path
Constructs a new
Pathobject from a given string.Constructs a new
Pathobject from a given string.- Definition Classes
- iofuncs
- val Random: scala.util.Random.type
An alias for convenient use of
scala.util.Randomviaimport o1.util._. - val Source: scala.io.Source.type
An alias for convenient use of
scala.io.Sourceviaimport o1.util._. - val Success: scala.util.Success.type
An alias for convenient use of
scala.util.Tryviaimport o1.util._. - val Try: scala.util.Try.type
An alias for convenient use of
scala.util.Tryviaimport 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
Pathin anOptionwrapper.- Definition Classes
- iofuncs
- def forEachLine(source: Source)(effect: (String) => Unit): Unit
Calls
getLineson the givenSourceand performs the specifiedeffecton each of the lines.Calls
getLineson the givenSourceand performs the specifiedeffecton 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; returnsNoneonly 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. Returnstrueif it did andfalseotherwise.- 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 anOptionwrapper 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 anOptionwrapper 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
Optionwrapper.- 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
Optionwrapper 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
getLineson the givenSourceand returns the lines in a vector.Calls
getLineson the givenSourceand 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 anOptionwrapper 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.universeviaimport 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.fromURLon 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 aFailureif 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 aFailureif 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
operationto the givenresourceand returns the result, making sure toclosethe resource.Attempts to apply the given
operationto the givenresourceand returns the result, making sure toclosethe 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
textin a new text file at the givenfilePath.Writes the given
textin 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