Skip to content

Commit 346b982

Browse files
committed
release v2.2: remove automatic implicit converters java <=> scala
use java.toScala and scala.toJava instead
1 parent 1aa4ade commit 346b982

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ val f = File("/User/johndoe/Documents")
2424
val f1: File = file"/User/johndoe/Documents"
2525
val f2: File = root/"User"/"johndoe"/"Documents"
2626
val f3: File = home/"Documents"
27-
val f4: File = new java.io.File("/User/johndoe/Documents")
27+
val f4: File = new java.io.File("/User/johndoe/Documents").toScala
2828
val f5: File = "/User"/"johndoe"/"Documents"
2929
val f6: File = "/User/johndoe/Documents".toFile
3030
val f7: File = root/"User"/"johndoe"/"Documents"/"presentations"/`..`
@@ -89,7 +89,7 @@ val reader : java.io.BufferedReader = file.reader
8989
val outputstream : java.io.OutputStream = file.out
9090
val writer : java.io.BufferedWriter = file.writer
9191
val inputstream : java.io.InputStream = file.in
92-
val file : java.io.File = file // implicit conversion
92+
val javaFile : java.io.File = file.toJava
9393
val path : java.nio.file.Path = file.path
9494
val fs : java.nio.file.FileSystem = file.fileSystem
9595
val channel : java.nio.channel.FileChannel = file.channel

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name := "better-files"
2-
version := "2.1.1"
2+
version := "2.2.0"
33
description := "Scala wrapper for Java files"
44
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
55
organization := "com.github.pathikrit"

src/main/scala/better/files/package.scala

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ package object files {
2020
* Scala wrapper for java.io.File
2121
*/
2222
case class File(file: JFile) {
23+
24+
def toJava: JFile = file
25+
2326
def path: Path = file.toPath
2427

2528
def fullPath: String = path.toString
@@ -43,14 +46,16 @@ package object files {
4346

4447
def contentType: Option[String] = Option(Files.probeContentType(path))
4548

46-
def parent: File = file.getParentFile
49+
def parent: File = file.getParentFile.toScala
4750

48-
def /(child: String): File = new JFile(file, child)
51+
def /(child: String): File = new JFile(file, child).toScala
4952

5053
def /(f: File => File): File = f(this)
5154

5255
def createIfNotExists(): File = if (file.exists()) this else {parent.mkdirs(); Files.createFile(path)}
5356

57+
def exists: Boolean = file.exists()
58+
5459
def content(implicit codec: Codec): BufferedSource = Source.fromFile(file)(codec)
5560
def source(implicit codec: Codec): BufferedSource = content(codec)
5661

@@ -59,7 +64,7 @@ package object files {
5964
Iterator.continually(stream.read()).takeWhile(-1 !=).map(_.toByte) //TODO: close the stream
6065
}
6166

62-
def mkdir(): File = {file.mkdirs(); this}
67+
def mkdirs(): File = {file.mkdirs(); this}
6368

6469
def chars(implicit codec: Codec): Iterator[Char] = content(codec)
6570

@@ -117,7 +122,7 @@ package object files {
117122
//def hide(): Boolean = ???
118123
//def unhide(): Boolean = ???
119124

120-
def list: Files = file.listFiles().toIterator map javaToFile
125+
def list: Files = file.listFiles().toIterator.map(_.toScala)
121126
def children: Files = list
122127

123128
def listRecursively(maxDepth: Int = Int.MaxValue): Files = Files.walk(path, maxDepth)
@@ -223,7 +228,7 @@ package object files {
223228
def sameFileAs(that: File): Boolean = Files.isSameFile(this.path, that.path)
224229

225230
/**
226-
* @return true if this file is exactly same as that file TODO: recursively for directories
231+
* @return true if this file is exactly same as that file TODO: recursively for directories (or empty files?)
227232
*/
228233
def contentSameAs(that: File): Boolean = this.bytes sameElements that.bytes
229234
val `===` = contentSameAs _
@@ -296,13 +301,17 @@ package object files {
296301
def ls(file: File): Files = file.list
297302
def ls_r(file: File): Files = file.listRecursively()
298303
def touch(file: File): File = file.touch()
299-
def mkdir(file: File): File = file.mkdir()
304+
def mkdir(file: File): File = file.mkdirs()
300305
def chown(owner: String, file: File): File = file.setOwner(owner)
301306
def chgrp(group: String, file: File): File = file.setGroup(group)
302307
def chmod_+(permission: PosixFilePermission, file: File): File = file.addPermissions(permission)
303308
def chmod_-(permission: PosixFilePermission, file: File): File = file.removePermissions(permission)
304309
}
305310

311+
implicit class FileOps(file: JFile) {
312+
def toScala: File = File(file)
313+
}
314+
306315
implicit class InputStreamOps(in: InputStream) {
307316
def >(out: OutputStream): Unit = pipeTo(out)
308317

@@ -344,9 +353,8 @@ package object files {
344353
}
345354

346355
implicit def codecToCharSet(codec: Codec): Charset = codec.charSet
347-
implicit def pathToFile(path: Path): File = path.toFile
348-
implicit def javaToFile(file: JFile): File = File(file) //TODO: ISO micro-macros
349-
implicit def toJavaFile(file: File): JFile = file.file //TODO: Move implicit converters to a special import?
356+
357+
implicit def pathToFile(path: Path): File = path.toFile.toScala
350358

351359
private[files] implicit def pathStreamToFiles(files: JStream[Path]): Files = files.iterator().map(pathToFile)
352360
private[files] def when[A](condition: Boolean)(f: => A): Option[A] = if (condition) Some(f) else None

src/test/scala/better/FilesSpec.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class FilesSpec extends FlatSpec with BeforeAndAfterEach with Matchers {
4949
val f1: File = file"/User/johndoe/Documents"
5050
val f2: File = root/"User"/"johndoe"/"Documents"
5151
val f3: File = home/"Documents"
52-
val f4: File = new java.io.File("/User/johndoe/Documents")
52+
val f4: File = new java.io.File("/User/johndoe/Documents").toScala
5353
val f5: File = "/User"/"johndoe"/"Documents"
5454
val f6: File = "/User/johndoe/Documents".toFile
5555
val f7: File = root/"User"/"johndoe"/"Documents"/"presentations" / `..`
@@ -65,14 +65,14 @@ class FilesSpec extends FlatSpec with BeforeAndAfterEach with Matchers {
6565
case SymbolicLink(to) => fail() //this must be first case statement if you want to handle symlinks specially; else will follow link
6666
case Directory(children) => fail()
6767
case RegularFile(contents) => fail()
68-
case other if other.exists() => fail() //A file may not be one of the above e.g. UNIX pipes, sockets, devices etc
68+
case other if other.exists => fail() //A file may not be one of the above e.g. UNIX pipes, sockets, devices etc
6969
case _ => //A file that does not exist
7070
}
7171
root/"dev"/"null" match {
7272
case SymbolicLink(to) => fail()
7373
case Directory(children) => fail()
7474
case RegularFile(contents) => fail()
75-
case other if other.exists() => //A file can be not any of the above e.g. UNIX pipes & sockets etc
75+
case other if other.exists => //A file can be not any of the above e.g. UNIX pipes & sockets etc
7676
case _ => fail()
7777
}
7878
root/"dev" match {
@@ -168,20 +168,20 @@ class FilesSpec extends FlatSpec with BeforeAndAfterEach with Matchers {
168168
t1.checksum() should not equal t2.checksum()
169169
a[java.nio.file.FileAlreadyExistsException] should be thrownBy (t1 copyTo t2)
170170
t1.copyTo(t2, overwrite = true)
171-
t1.exists() shouldBe true
171+
t1.exists shouldBe true
172172
t1.checksum() shouldEqual t2.checksum()
173173
b2.contentAsString shouldEqual magicWord
174174
// rename
175175
t2.name shouldBe "t2.txt"
176-
t2.exists() shouldBe true
176+
t2.exists shouldBe true
177177
val t3 = t2 renameTo "t3.txt"
178178
t3.name shouldBe "t3.txt"
179-
t2.exists() shouldBe false
180-
t3.exists() shouldBe true
179+
t2.exists shouldBe false
180+
t3.exists shouldBe true
181181
// move
182182
t3 moveTo t2
183-
t2.exists() shouldBe true
184-
t3.exists() shouldBe false
183+
t2.exists shouldBe true
184+
t3.exists shouldBe false
185185
}
186186

187187
it should "do I/O via streams/writers" in {

0 commit comments

Comments
 (0)