From 8bf638c60f016b8ff99a950e9b7e4a2ad1ef9aed Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 19 Jan 2016 21:28:58 +0300 Subject: [PATCH] Drop deprecations: FileTreeWalk deprecated API elements. --- .../src/kotlin/io/files/FileTreeWalk.kt | 72 ++----------------- libraries/stdlib/src/kotlin/io/files/Utils.kt | 2 +- libraries/stdlib/test/io/FileTreeWalks.kt | 26 +++---- 3 files changed, 21 insertions(+), 79 deletions(-) diff --git a/libraries/stdlib/src/kotlin/io/files/FileTreeWalk.kt b/libraries/stdlib/src/kotlin/io/files/FileTreeWalk.kt index 54fe7a1fa84..1b933871c16 100644 --- a/libraries/stdlib/src/kotlin/io/files/FileTreeWalk.kt +++ b/libraries/stdlib/src/kotlin/io/files/FileTreeWalk.kt @@ -42,34 +42,11 @@ public class FileTreeWalk private constructor( private val onEnter: ((File) -> Boolean)?, private val onLeave: ((File) -> Unit)?, private val onFail: ((f: File, e: IOException) -> Unit)?, - private val filter: (File) -> Boolean = { true }, - private val maxDepth: Int = Int.MAX_VALUE, - dummy: Boolean = false + private val maxDepth: Int = Int.MAX_VALUE ) : Sequence { - internal constructor(start: File, direction: FileWalkDirection = FileWalkDirection.TOP_DOWN): this(start, direction, null, null, null, dummy = false) + internal constructor(start: File, direction: FileWalkDirection = FileWalkDirection.TOP_DOWN): this(start, direction, null, null, null) -/* - private constructor( - start: File, - direction: FileWalkDirection = FileWalkDirection.TOP_DOWN, - onEnter: ((File) -> Boolean)? = null, - onLeave: ((File) -> Unit)? = null, - onFail: ((f: File, e: IOException) -> Unit)? = null, - maxDepth: Int = Int.MAX_VALUE - ) : this(start, direction, onEnter, onLeave, onFail, maxDepth = maxDepth, dummy = false) -*/ - - @Deprecated("Use builder methods on an instance obtained from File.walk/walkTopDown/walkBottomUp instead of directly calling constructor.") - public constructor( - start: File, - direction: FileWalkDirection = FileWalkDirection.TOP_DOWN, - enter: (File) -> Unit = {}, - leave: (File) -> Unit = {}, - fail: (f: File, e: IOException) -> Unit = { f, e -> Unit }, - filter: (File) -> Boolean = { true }, - maxDepth: Int = Int.MAX_VALUE - ) : this(start, direction, onEnter = { enter(it); true }, onLeave = leave, onFail = fail, filter = filter, maxDepth = maxDepth) /** Returns an iterator walking through files. */ override public fun iterator(): Iterator = FileTreeWalkIterator() @@ -95,7 +72,7 @@ public class FileTreeWalk private constructor( private val state = Stack() init { - if (start.isDirectory && filter(start)) { + if (start.isDirectory) { state.push(directoryState(start)) } else if (start.isFile) { state.push(SingleFileState(start)) @@ -136,8 +113,6 @@ public class FileTreeWalk private constructor( return gotoNext() } else { // Check that file/directory matches the filter - if (!filter(file)) - return gotoNext() if (file == topState.root || !file.isDirectory || state.size >= maxDepth) { // Proceed to a root directory or a simple file return file @@ -241,7 +216,6 @@ public class FileTreeWalk private constructor( override fun step(): File? { if (visited) return null visited = true - if (!filter(root)) return null return root } } @@ -254,45 +228,24 @@ public class FileTreeWalk private constructor( * If the [function] returns `false` the directory is not entered, and neither it nor its files are not visited. */ public fun onEnter(function: (File) -> Boolean): FileTreeWalk { - return FileTreeWalk(start, direction, onEnter = function, onLeave = onLeave, onFail = onFail, filter = filter, maxDepth = maxDepth) + return FileTreeWalk(start, direction, onEnter = function, onLeave = onLeave, onFail = onFail, maxDepth = maxDepth) } - @Deprecated("Use onEnter instead.") - public fun enter(function: (File) -> Unit): FileTreeWalk = onEnter { function(it); true } - /** * Sets leave directory [function]. * Leave [function] is called AFTER the corresponding directory and its files are visited. */ public fun onLeave(function: (File) -> Unit): FileTreeWalk { - return FileTreeWalk(start, direction, onEnter, function, onFail, filter, maxDepth, false) + return FileTreeWalk(start, direction, onEnter, function, onFail, maxDepth) } - @Deprecated("Use onLeave instead.", ReplaceWith("onLeave(function)")) - public fun leave(function: (File) -> Unit): FileTreeWalk = onLeave(function) - /** * Set fail entering directory [function]. * Fail [function] is called when walker is unable to get list of directory files. * Enter and leave functions are called even in this case. */ public fun onFail(function: (File, IOException) -> Unit): FileTreeWalk { - return FileTreeWalk(start, direction, onEnter, onLeave, function, filter, maxDepth) - } - - @Deprecated("Use onFail instead.", ReplaceWith("onFail(function)")) - public fun fail(function: (File, IOException) -> Unit): FileTreeWalk = onFail(function) - - - /** - * Sets tree filter [predicate]. - * Tree filter [predicate] function is called before visiting files and entering directories. - * If it returns `false`, file is not visited, directory is not entered and all its content is also not visited. - * If it returns `true`, everything goes in the regular way. - */ - @Deprecated("Filter out directories entirely with onEnter, and all items with filter().") - public fun treeFilter(predicate: (File) -> Boolean): FileTreeWalk { - return FileTreeWalk(start, direction, onEnter, onLeave, onFail, predicate, maxDepth) + return FileTreeWalk(start, direction, onEnter, onLeave, function, maxDepth) } /** @@ -302,7 +255,7 @@ public class FileTreeWalk private constructor( public fun maxDepth(depth: Int): FileTreeWalk { if (depth <= 0) throw IllegalArgumentException("Use positive depth value") - return FileTreeWalk(start, direction, onEnter, onLeave, onFail, filter, depth) + return FileTreeWalk(start, direction, onEnter, onLeave, onFail, depth) } } @@ -325,14 +278,3 @@ public fun File.walkTopDown(): FileTreeWalk = walk(FileWalkDirection.TOP_DOWN) * Depth-first search is used and directories are visited after all their files. */ public fun File.walkBottomUp(): FileTreeWalk = walk(FileWalkDirection.BOTTOM_UP) - -/** - * Recursively process this file and all children with the given block. - * Note that if this file doesn't exist, then the block will be executed on it anyway. - * - * @param function the function to call on each file. - */ -@Deprecated("It's recommended to use walkTopDown() / walkBottomUp()", ReplaceWith("walkTopDown().forEach(function)"), DeprecationLevel.ERROR) -public fun File.recurse(function: (File) -> Unit): Unit { - walkTopDown().forEach(function) -} diff --git a/libraries/stdlib/src/kotlin/io/files/Utils.kt b/libraries/stdlib/src/kotlin/io/files/Utils.kt index 38879ab3ff7..a9bf98ea2ed 100644 --- a/libraries/stdlib/src/kotlin/io/files/Utils.kt +++ b/libraries/stdlib/src/kotlin/io/files/Utils.kt @@ -245,7 +245,7 @@ public fun File.copyRecursively(dst: File, } try { // We cannot break for loop from inside a lambda, so we have to use an exception here - for (src in walkTopDown().fail { f, e -> if (onError(f, e) == OnErrorAction.TERMINATE) throw TerminateException(f) }) { + for (src in walkTopDown().onFail { f, e -> if (onError(f, e) == OnErrorAction.TERMINATE) throw TerminateException(f) }) { if (!src.exists()) { if (onError(src, NoSuchFileException(file = src, reason = "The source file doesn't exist")) == OnErrorAction.TERMINATE) diff --git a/libraries/stdlib/test/io/FileTreeWalks.kt b/libraries/stdlib/test/io/FileTreeWalks.kt index 464f12f9a01..f8b4700cf99 100644 --- a/libraries/stdlib/test/io/FileTreeWalks.kt +++ b/libraries/stdlib/test/io/FileTreeWalks.kt @@ -53,7 +53,6 @@ class FileTreeWalkTest { try { for (walk in listOf(File::walkTopDown, File::walkBottomUp)) { assertEquals(testFile, walk(testFile).single(), "${walk.name}") - assertTrue(walk(testFile).treeFilter { false }.none(), "${walk.name}") assertEquals(testFile, testFile.walk().onEnter { false }.single(), "${walk.name} - enter should not be called for single file") assertTrue(walk(nonExistantFile).none(), "${walk.name} - enter should not be called for single file") @@ -151,7 +150,7 @@ class FileTreeWalkTest { child.delete() } } - for (file in basedir.walkTopDown().enter(::enter)) { + for (file in basedir.walkTopDown().onEnter { enter(it); true }) { val name = file.relativeToOrSelf(basedir).invariantSeparatorsPath assertFalse(namesTopDown.contains(name), "$name is visited twice") namesTopDown.add(name) @@ -174,7 +173,7 @@ class FileTreeWalkTest { child.delete() } } - for (file in basedir.walkBottomUp().enter(::enter)) { + for (file in basedir.walkBottomUp().onEnter { enter(it); true }) { val name = file.relativeToOrSelf(basedir).invariantSeparatorsPath assertFalse(namesTopDown.contains(name), "$name is visited twice") namesTopDown.add(name) @@ -187,14 +186,14 @@ class FileTreeWalkTest { private fun compareWalkResults(expected: Set, basedir: File, filter: (File) -> Boolean) { val namesTopDown = HashSet() - for (file in basedir.walkTopDown().treeFilter { filter(it) }) { + for (file in basedir.walkTopDown().onEnter { filter(it) }) { val name = file.toRelativeString(basedir) assertFalse(namesTopDown.contains(name), "$name is visited twice") namesTopDown.add(name) } assertEquals(expected, namesTopDown, "Top-down walk results differ") val namesBottomUp = HashSet() - for (file in basedir.walkBottomUp().treeFilter { filter(it) }) { + for (file in basedir.walkBottomUp().onEnter { filter(it) }) { val name = file.toRelativeString(basedir) assertFalse(namesBottomUp.contains(name), "$name is visited twice") namesBottomUp.add(name) @@ -202,7 +201,7 @@ class FileTreeWalkTest { assertEquals(expected, namesBottomUp, "Bottom-up walk results differ") } - @Test fun withFilter() { + @Test fun withDirectoryFilter() { val basedir = createTestFiles() try { // Every directory ended with 3 and its content is filtered out @@ -215,7 +214,7 @@ class FileTreeWalkTest { } } - @Test fun withTotalFilter() { + @Test fun withTotalDirectoryFilter() { val basedir = createTestFiles() try { val referenceNames = emptySet() @@ -266,9 +265,10 @@ class FileTreeWalkTest { val dirs = HashSet() val failed = HashSet() val stack = ArrayList() - fun beforeVisitDirectory(dir: File) { + fun beforeVisitDirectory(dir: File): Boolean { stack.add(dir) dirs.add(dir.relativeToOrSelf(basedir)) + return true } fun afterVisitDirectory(dir: File) { @@ -286,8 +286,8 @@ class FileTreeWalkTest { //stack.removeAt(stack.lastIndex) failed.add(dir.name) } - basedir.walkTopDown().enter(::beforeVisitDirectory).leave(::afterVisitDirectory). - fail(::visitDirectoryFailed).forEach { it -> if (!it.isDirectory) visitFile(it) } + basedir.walkTopDown().onEnter(::beforeVisitDirectory).onLeave(::afterVisitDirectory). + onFail(::visitDirectoryFailed).forEach { it -> if (!it.isDirectory) visitFile(it) } assertTrue(stack.isEmpty()) for (fileName in arrayOf("", "1", "1/2", "1/3", "6", "8")) { assertTrue(dirs.contains(File(fileName)), fileName) @@ -299,7 +299,7 @@ class FileTreeWalkTest { //limit maxDepth files.clear() dirs.clear() - basedir.walkTopDown().enter(::beforeVisitDirectory).leave(::afterVisitDirectory).maxDepth(1). + basedir.walkTopDown().onEnter(::beforeVisitDirectory).onLeave(::afterVisitDirectory).maxDepth(1). forEach { it -> if (it != basedir) visitFile(it) } assertTrue(stack.isEmpty()) assertEquals(setOf(File("")), dirs) @@ -312,8 +312,8 @@ class FileTreeWalkTest { try { files.clear() dirs.clear() - basedir.walkTopDown().enter(::beforeVisitDirectory).leave(::afterVisitDirectory). - fail(::visitDirectoryFailed).forEach { it -> if (!it.isDirectory) visitFile(it) } + basedir.walkTopDown().onEnter(::beforeVisitDirectory).onLeave(::afterVisitDirectory). + onFail(::visitDirectoryFailed).forEach { it -> if (!it.isDirectory) visitFile(it) } assertTrue(stack.isEmpty()) assertEquals(setOf("1"), failed) assertEquals(listOf("", "1", "6", "8").map { File(it) }.toSet(), dirs)