Drop deprecations: FileTreeWalk deprecated API elements.
This commit is contained in:
@@ -42,34 +42,11 @@ public class FileTreeWalk private constructor(
|
|||||||
private val onEnter: ((File) -> Boolean)?,
|
private val onEnter: ((File) -> Boolean)?,
|
||||||
private val onLeave: ((File) -> Unit)?,
|
private val onLeave: ((File) -> Unit)?,
|
||||||
private val onFail: ((f: File, e: IOException) -> Unit)?,
|
private val onFail: ((f: File, e: IOException) -> Unit)?,
|
||||||
private val filter: (File) -> Boolean = { true },
|
private val maxDepth: Int = Int.MAX_VALUE
|
||||||
private val maxDepth: Int = Int.MAX_VALUE,
|
|
||||||
dummy: Boolean = false
|
|
||||||
) : Sequence<File> {
|
) : Sequence<File> {
|
||||||
|
|
||||||
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. */
|
/** Returns an iterator walking through files. */
|
||||||
override public fun iterator(): Iterator<File> = FileTreeWalkIterator()
|
override public fun iterator(): Iterator<File> = FileTreeWalkIterator()
|
||||||
@@ -95,7 +72,7 @@ public class FileTreeWalk private constructor(
|
|||||||
private val state = Stack<WalkState>()
|
private val state = Stack<WalkState>()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (start.isDirectory && filter(start)) {
|
if (start.isDirectory) {
|
||||||
state.push(directoryState(start))
|
state.push(directoryState(start))
|
||||||
} else if (start.isFile) {
|
} else if (start.isFile) {
|
||||||
state.push(SingleFileState(start))
|
state.push(SingleFileState(start))
|
||||||
@@ -136,8 +113,6 @@ public class FileTreeWalk private constructor(
|
|||||||
return gotoNext()
|
return gotoNext()
|
||||||
} else {
|
} else {
|
||||||
// Check that file/directory matches the filter
|
// Check that file/directory matches the filter
|
||||||
if (!filter(file))
|
|
||||||
return gotoNext()
|
|
||||||
if (file == topState.root || !file.isDirectory || state.size >= maxDepth) {
|
if (file == topState.root || !file.isDirectory || state.size >= maxDepth) {
|
||||||
// Proceed to a root directory or a simple file
|
// Proceed to a root directory or a simple file
|
||||||
return file
|
return file
|
||||||
@@ -241,7 +216,6 @@ public class FileTreeWalk private constructor(
|
|||||||
override fun step(): File? {
|
override fun step(): File? {
|
||||||
if (visited) return null
|
if (visited) return null
|
||||||
visited = true
|
visited = true
|
||||||
if (!filter(root)) return null
|
|
||||||
return root
|
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.
|
* 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 {
|
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].
|
* Sets leave directory [function].
|
||||||
* Leave [function] is called AFTER the corresponding directory and its files are visited.
|
* Leave [function] is called AFTER the corresponding directory and its files are visited.
|
||||||
*/
|
*/
|
||||||
public fun onLeave(function: (File) -> Unit): FileTreeWalk {
|
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].
|
* Set fail entering directory [function].
|
||||||
* Fail [function] is called when walker is unable to get list of directory files.
|
* Fail [function] is called when walker is unable to get list of directory files.
|
||||||
* Enter and leave functions are called even in this case.
|
* Enter and leave functions are called even in this case.
|
||||||
*/
|
*/
|
||||||
public fun onFail(function: (File, IOException) -> Unit): FileTreeWalk {
|
public fun onFail(function: (File, IOException) -> Unit): FileTreeWalk {
|
||||||
return FileTreeWalk(start, direction, onEnter, onLeave, function, filter, maxDepth)
|
return FileTreeWalk(start, direction, onEnter, onLeave, function, 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -302,7 +255,7 @@ public class FileTreeWalk private constructor(
|
|||||||
public fun maxDepth(depth: Int): FileTreeWalk {
|
public fun maxDepth(depth: Int): FileTreeWalk {
|
||||||
if (depth <= 0)
|
if (depth <= 0)
|
||||||
throw IllegalArgumentException("Use positive depth value")
|
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.
|
* Depth-first search is used and directories are visited after all their files.
|
||||||
*/
|
*/
|
||||||
public fun File.walkBottomUp(): FileTreeWalk = walk(FileWalkDirection.BOTTOM_UP)
|
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)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -245,7 +245,7 @@ public fun File.copyRecursively(dst: File,
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
// We cannot break for loop from inside a lambda, so we have to use an exception here
|
// 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 (!src.exists()) {
|
||||||
if (onError(src, NoSuchFileException(file = src, reason = "The source file doesn't exist")) ==
|
if (onError(src, NoSuchFileException(file = src, reason = "The source file doesn't exist")) ==
|
||||||
OnErrorAction.TERMINATE)
|
OnErrorAction.TERMINATE)
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ class FileTreeWalkTest {
|
|||||||
try {
|
try {
|
||||||
for (walk in listOf(File::walkTopDown, File::walkBottomUp)) {
|
for (walk in listOf(File::walkTopDown, File::walkBottomUp)) {
|
||||||
assertEquals(testFile, walk(testFile).single(), "${walk.name}")
|
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")
|
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")
|
assertTrue(walk(nonExistantFile).none(), "${walk.name} - enter should not be called for single file")
|
||||||
@@ -151,7 +150,7 @@ class FileTreeWalkTest {
|
|||||||
child.delete()
|
child.delete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (file in basedir.walkTopDown().enter(::enter)) {
|
for (file in basedir.walkTopDown().onEnter { enter(it); true }) {
|
||||||
val name = file.relativeToOrSelf(basedir).invariantSeparatorsPath
|
val name = file.relativeToOrSelf(basedir).invariantSeparatorsPath
|
||||||
assertFalse(namesTopDown.contains(name), "$name is visited twice")
|
assertFalse(namesTopDown.contains(name), "$name is visited twice")
|
||||||
namesTopDown.add(name)
|
namesTopDown.add(name)
|
||||||
@@ -174,7 +173,7 @@ class FileTreeWalkTest {
|
|||||||
child.delete()
|
child.delete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (file in basedir.walkBottomUp().enter(::enter)) {
|
for (file in basedir.walkBottomUp().onEnter { enter(it); true }) {
|
||||||
val name = file.relativeToOrSelf(basedir).invariantSeparatorsPath
|
val name = file.relativeToOrSelf(basedir).invariantSeparatorsPath
|
||||||
assertFalse(namesTopDown.contains(name), "$name is visited twice")
|
assertFalse(namesTopDown.contains(name), "$name is visited twice")
|
||||||
namesTopDown.add(name)
|
namesTopDown.add(name)
|
||||||
@@ -187,14 +186,14 @@ class FileTreeWalkTest {
|
|||||||
|
|
||||||
private fun compareWalkResults(expected: Set<String>, basedir: File, filter: (File) -> Boolean) {
|
private fun compareWalkResults(expected: Set<String>, basedir: File, filter: (File) -> Boolean) {
|
||||||
val namesTopDown = HashSet<String>()
|
val namesTopDown = HashSet<String>()
|
||||||
for (file in basedir.walkTopDown().treeFilter { filter(it) }) {
|
for (file in basedir.walkTopDown().onEnter { filter(it) }) {
|
||||||
val name = file.toRelativeString(basedir)
|
val name = file.toRelativeString(basedir)
|
||||||
assertFalse(namesTopDown.contains(name), "$name is visited twice")
|
assertFalse(namesTopDown.contains(name), "$name is visited twice")
|
||||||
namesTopDown.add(name)
|
namesTopDown.add(name)
|
||||||
}
|
}
|
||||||
assertEquals(expected, namesTopDown, "Top-down walk results differ")
|
assertEquals(expected, namesTopDown, "Top-down walk results differ")
|
||||||
val namesBottomUp = HashSet<String>()
|
val namesBottomUp = HashSet<String>()
|
||||||
for (file in basedir.walkBottomUp().treeFilter { filter(it) }) {
|
for (file in basedir.walkBottomUp().onEnter { filter(it) }) {
|
||||||
val name = file.toRelativeString(basedir)
|
val name = file.toRelativeString(basedir)
|
||||||
assertFalse(namesBottomUp.contains(name), "$name is visited twice")
|
assertFalse(namesBottomUp.contains(name), "$name is visited twice")
|
||||||
namesBottomUp.add(name)
|
namesBottomUp.add(name)
|
||||||
@@ -202,7 +201,7 @@ class FileTreeWalkTest {
|
|||||||
assertEquals(expected, namesBottomUp, "Bottom-up walk results differ")
|
assertEquals(expected, namesBottomUp, "Bottom-up walk results differ")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun withFilter() {
|
@Test fun withDirectoryFilter() {
|
||||||
val basedir = createTestFiles()
|
val basedir = createTestFiles()
|
||||||
try {
|
try {
|
||||||
// Every directory ended with 3 and its content is filtered out
|
// 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()
|
val basedir = createTestFiles()
|
||||||
try {
|
try {
|
||||||
val referenceNames = emptySet<String>()
|
val referenceNames = emptySet<String>()
|
||||||
@@ -266,9 +265,10 @@ class FileTreeWalkTest {
|
|||||||
val dirs = HashSet<File>()
|
val dirs = HashSet<File>()
|
||||||
val failed = HashSet<String>()
|
val failed = HashSet<String>()
|
||||||
val stack = ArrayList<File>()
|
val stack = ArrayList<File>()
|
||||||
fun beforeVisitDirectory(dir: File) {
|
fun beforeVisitDirectory(dir: File): Boolean {
|
||||||
stack.add(dir)
|
stack.add(dir)
|
||||||
dirs.add(dir.relativeToOrSelf(basedir))
|
dirs.add(dir.relativeToOrSelf(basedir))
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun afterVisitDirectory(dir: File) {
|
fun afterVisitDirectory(dir: File) {
|
||||||
@@ -286,8 +286,8 @@ class FileTreeWalkTest {
|
|||||||
//stack.removeAt(stack.lastIndex)
|
//stack.removeAt(stack.lastIndex)
|
||||||
failed.add(dir.name)
|
failed.add(dir.name)
|
||||||
}
|
}
|
||||||
basedir.walkTopDown().enter(::beforeVisitDirectory).leave(::afterVisitDirectory).
|
basedir.walkTopDown().onEnter(::beforeVisitDirectory).onLeave(::afterVisitDirectory).
|
||||||
fail(::visitDirectoryFailed).forEach { it -> if (!it.isDirectory) visitFile(it) }
|
onFail(::visitDirectoryFailed).forEach { it -> if (!it.isDirectory) visitFile(it) }
|
||||||
assertTrue(stack.isEmpty())
|
assertTrue(stack.isEmpty())
|
||||||
for (fileName in arrayOf("", "1", "1/2", "1/3", "6", "8")) {
|
for (fileName in arrayOf("", "1", "1/2", "1/3", "6", "8")) {
|
||||||
assertTrue(dirs.contains(File(fileName)), fileName)
|
assertTrue(dirs.contains(File(fileName)), fileName)
|
||||||
@@ -299,7 +299,7 @@ class FileTreeWalkTest {
|
|||||||
//limit maxDepth
|
//limit maxDepth
|
||||||
files.clear()
|
files.clear()
|
||||||
dirs.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) }
|
forEach { it -> if (it != basedir) visitFile(it) }
|
||||||
assertTrue(stack.isEmpty())
|
assertTrue(stack.isEmpty())
|
||||||
assertEquals(setOf(File("")), dirs)
|
assertEquals(setOf(File("")), dirs)
|
||||||
@@ -312,8 +312,8 @@ class FileTreeWalkTest {
|
|||||||
try {
|
try {
|
||||||
files.clear()
|
files.clear()
|
||||||
dirs.clear()
|
dirs.clear()
|
||||||
basedir.walkTopDown().enter(::beforeVisitDirectory).leave(::afterVisitDirectory).
|
basedir.walkTopDown().onEnter(::beforeVisitDirectory).onLeave(::afterVisitDirectory).
|
||||||
fail(::visitDirectoryFailed).forEach { it -> if (!it.isDirectory) visitFile(it) }
|
onFail(::visitDirectoryFailed).forEach { it -> if (!it.isDirectory) visitFile(it) }
|
||||||
assertTrue(stack.isEmpty())
|
assertTrue(stack.isEmpty())
|
||||||
assertEquals(setOf("1"), failed)
|
assertEquals(setOf("1"), failed)
|
||||||
assertEquals(listOf("", "1", "6", "8").map { File(it) }.toSet(), dirs)
|
assertEquals(listOf("", "1", "6", "8").map { File(it) }.toSet(), dirs)
|
||||||
|
|||||||
Reference in New Issue
Block a user