From 54af6d359a3031bddc3f5e6700ccb1a3ef578d40 Mon Sep 17 00:00:00 2001 From: Pathikrit Bhowmick Date: Sat, 14 Jan 2017 12:30:22 -0500 Subject: [PATCH 1/2] Fix #92: Remove File.Type --- CHANGES.md | 4 ++ README.md | 4 +- core/src/main/scala/better/files/File.scala | 49 ++++++------------- .../test/scala/better/files/FileSpec.scala | 28 ++--------- 4 files changed, 25 insertions(+), 60 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a6d807e2..d5a89178 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,3 +6,7 @@ ## v2.17.1 * [PR #99](https://github.com/pathikrit/better-files/pull/99): Release for Scala 2.12 + +## v2.18.0 + +* [PR #100](https://github.com/pathikrit/better-files/pull/100): Fix issue in unzip of parents diff --git a/README.md b/README.md index 4ab57325..29c2d176 100644 --- a/README.md +++ b/README.md @@ -38,13 +38,13 @@ To use the [Akka based file monitor](akka), also add this: ```scala libraryDependencies ++= Seq( "com.github.pathikrit" %% "better-files-akka" % version, - "com.typesafe.akka" %% "akka-actor" % "2.3.15" + "com.typesafe.akka" %% "akka-actor" % "2.4.16" ) ``` Latest `version`: [![Maven][mavenImg]][mavenLink] Although this library is currently only actively developed for Scala 2.12, -you can find reasonably recent versions of this library for Scala 2.10 and 2.11 [here](https://oss.sonatype.org/#nexus-search;quick~better-files) +you can find reasonably recent versions of this library for Scala 2.10 and 2.11 [here](https://oss.sonatype.org/#nexus-search;quick~better-files). ## Tests [![codecov][codecovImg]][codecovLink] * [FileSpec](core/src/test/scala/better/files/FileSpec.scala) diff --git a/core/src/main/scala/better/files/File.scala b/core/src/main/scala/better/files/File.scala index 262dabd9..254c4fe1 100644 --- a/core/src/main/scala/better/files/File.scala +++ b/core/src/main/scala/better/files/File.scala @@ -722,24 +722,31 @@ class File private(val path: Path) { /** * @return true if file is not present or empty directory or 0-bytes file */ - def isEmpty: Boolean = { - this match { - case File.Type.Directory(children) => children.isEmpty - case File.Type.RegularFile(content) => content.isEmpty - case _ => notExists + def isEmpty(implicit linkOptions: File.LinkOptions = File.LinkOptions.default): Boolean = { + if (isDirectory(linkOptions)) { + children.isEmpty + } else if (isRegularFile(linkOptions)) { + size == 0 + } else { + notExists(linkOptions) } } + def isNotEmpty(implicit linkOptions: File.LinkOptions = File.LinkOptions.default): Boolean = { + !isEmpty(linkOptions) + } + /** * If this is a directory, remove all its children * If its a file, empty the contents * * @return this */ - def clear(): this.type = { - this match { - case File.Type.Directory(children) => children.foreach(_.delete()) - case _ => writeByteArray(Array.emptyByteArray)(File.OpenOptions.default) + def clear()(implicit linkOptions: File.LinkOptions = File.LinkOptions.default): this.type = { + if (isDirectory(linkOptions)) { + children.foreach(_.delete()) + } else { + writeByteArray(Array.emptyByteArray)(File.OpenOptions.default) } this } @@ -882,30 +889,6 @@ object File { val default : Order = byDirectoriesFirst } - /** - * Denote various file types using this - * - * @tparam Content The type of underlying contents e.g. a directory has its children files as contents but a regular file may have bytes as contents - */ - sealed trait Type[Content] { - def unapply(file: File): Option[Content] - } - - object Type { - - case object RegularFile extends Type[BufferedSource] { - override def unapply(file: File) = when(file.isRegularFile)(file.newBufferedSource) - } - - case object Directory extends Type[Files] { - override def unapply(file: File) = when(file.isDirectory)(file.children) - } - - case object SymbolicLink extends Type[File] { - override def unapply(file: File) = file.symbolicLink - } - } - class PathMatcherSyntax private (val name: String) object PathMatcherSyntax { val glob = new PathMatcherSyntax("glob") diff --git a/core/src/test/scala/better/files/FileSpec.scala b/core/src/test/scala/better/files/FileSpec.scala index c2ace5ae..a37c9d27 100644 --- a/core/src/test/scala/better/files/FileSpec.scala +++ b/core/src/test/scala/better/files/FileSpec.scala @@ -1,7 +1,6 @@ package better.files import File.{root, home} -import File.Type._ import Cmds._ import org.scalatest._ @@ -92,27 +91,6 @@ class FileSpec extends FlatSpec with BeforeAndAfterEach with Matchers { Seq(f, f1, f2, f4, /*f5,*/ f6, f8, f9).map(_.toString).toSet shouldBe Set(f.toString) } - it can "be matched" in { - "src"/"test"/"foo" match { - case SymbolicLink(to) => fail() //this must be first case statement if you want to handle symlinks specially; else will follow link - case Directory(children) => fail() - case RegularFile(contents) => fail() - case other if other.exists => fail() //A file may not be one of the above e.g. UNIX pipes, sockets, devices etc - case _ => //A file that does not exist - } - root/"dev"/"null" match { - case SymbolicLink(to) => fail() - case Directory(children) => fail() - case RegularFile(contents) => fail() - case other if other.exists => //A file can be not any of the above e.g. UNIX pipes & sockets etc - case _ => fail() - } - root/"dev" match { - case Directory(children) => children.exists(_.pathAsString == "/dev/null") shouldBe true // /dev should have 'null' - case _ => fail() - } - } - it should "do basic I/O" in { t1 < "hello" t1.contentAsString shouldEqual "hello" @@ -197,9 +175,9 @@ class FileSpec extends FlatSpec with BeforeAndAfterEach with Matchers { it should "support sorting" in { testRoot.list.toSeq.sorted(File.Order.byName) should not be empty - testRoot.list.toSeq.max(File.Order.bySize) should not be empty - testRoot.list.toSeq.min(File.Order.byDepth) should not be empty - testRoot.list.toSeq.min(File.Order.byModificationTime) should not be empty + testRoot.list.toSeq.max(File.Order.bySize).isEmpty shouldBe false + testRoot.list.toSeq.min(File.Order.byDepth).isEmpty shouldBe false + testRoot.list.toSeq.min(File.Order.byModificationTime).isEmpty shouldBe false testRoot.list.toSeq.sorted(File.Order.byDirectoriesFirst) should not be empty } From 2629708550442e4885d862f8aa26adf5711e3cb9 Mon Sep 17 00:00:00 2001 From: Pathikrit Bhowmick Date: Sat, 14 Jan 2017 12:38:34 -0500 Subject: [PATCH 2/2] Update docs --- CHANGES.md | 1 + README.md | 17 ----------------- core/src/main/scala/better/files/File.scala | 2 +- 3 files changed, 2 insertions(+), 18 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d5a89178..2b1b0495 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,3 +10,4 @@ ## v2.18.0 * [PR #100](https://github.com/pathikrit/better-files/pull/100): Fix issue in unzip of parents +* [PR #101](https://github.com/pathikrit/better-files/pull/101): Removed File.Type diff --git a/README.md b/README.md index 29c2d176..0604c5bd 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ 0. [Simple I/O](#file-readwrite) 0. [Streams and Codecs](#streams-and-codecs) 0. [Java compatibility](#java-interoperability) - 0. [Pattern matching](#pattern-matching) 0. [Globbing](#globbing) 0. [File system operations](#file-system-operations) 0. [UNIX DSL](#unix-dsl) @@ -221,22 +220,6 @@ val br : BufferedReader = reader.buffered val bw : BufferedWriter = writer.buffered val mm : MappedByteBuffer = fileChannel.toMappedByteBuffer ``` - -### Pattern matching -Instead of `if-else`, more idiomatic powerful Scala [pattern matching](http://pathikrit.github.io/better-files/latest/api/better/files/File$$Type$.html): -```scala -/** - * @return true if file is a directory with no children or a file with no contents - */ -def isEmpty(file: File): Boolean = file match { - case File.Type.SymbolicLink(to) => isEmpty(to) // this must be first case statement if you want to handle symlinks specially; else will follow link - case File.Type.Directory(files) => files.isEmpty - case File.Type.RegularFile(content) => content.isEmpty - case _ => file.notExists // a file may not be one of the above e.g. UNIX pipes, sockets, devices etc -} -// or as extractors on LHS: -val File.Type.Directory(researchDocs) = home/"Downloads"/"research" -``` ### Globbing No need to port [this](http://docs.oracle.com/javase/tutorial/essential/io/find.html) to Scala: diff --git a/core/src/main/scala/better/files/File.scala b/core/src/main/scala/better/files/File.scala index 254c4fe1..8f630732 100644 --- a/core/src/main/scala/better/files/File.scala +++ b/core/src/main/scala/better/files/File.scala @@ -726,7 +726,7 @@ class File private(val path: Path) { if (isDirectory(linkOptions)) { children.isEmpty } else if (isRegularFile(linkOptions)) { - size == 0 + toJava.length() == 0 } else { notExists(linkOptions) }