summaryrefslogtreecommitdiff
path: root/libs/io/src/main/java/org/the_jk/cleversync
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2024-11-09 17:47:05 +0100
committerJoel Klinghed <the_jk@spawned.biz>2024-11-09 17:47:05 +0100
commitb5d99dd596a67e185414d65507b23541e016daed (patch)
treeaab741f46e09d07a109d86e75da7e2c57c3242f0 /libs/io/src/main/java/org/the_jk/cleversync
parent092f09aec7c447a8a93c1e5372634c54ad874ddd (diff)
single merge: Put non-mutable parameters in a data class
Reduces parameter count.
Diffstat (limited to 'libs/io/src/main/java/org/the_jk/cleversync')
-rw-r--r--libs/io/src/main/java/org/the_jk/cleversync/io/SingleMerge.kt45
1 files changed, 28 insertions, 17 deletions
diff --git a/libs/io/src/main/java/org/the_jk/cleversync/io/SingleMerge.kt b/libs/io/src/main/java/org/the_jk/cleversync/io/SingleMerge.kt
index 711daae..9869993 100644
--- a/libs/io/src/main/java/org/the_jk/cleversync/io/SingleMerge.kt
+++ b/libs/io/src/main/java/org/the_jk/cleversync/io/SingleMerge.kt
@@ -5,18 +5,17 @@ import java.time.Instant
object SingleMerge {
fun calculate(target: Directory, source: Directory, options: Options = Options()): List<Action> {
- return visit(target, source.list(), source, "/", options)
+ return visit(target, source.list(), Context(source, options), "/")
}
private fun visit(
target: Directory?,
source: Directory.Content,
- root: Directory,
+ context: Context,
path: String,
- options: Options,
) : List<Action> {
val targetContent = target?.list()
- val targetNames = if (targetContent != null && options.deleteFilesOnlyInTarget) {
+ val targetNames = if (targetContent != null && context.options.deleteFilesOnlyInTarget) {
val ret = mutableMapOf<String, Type>()
targetContent.directories.forEach { ret[it.name] = Type.DIRECTORY }
targetContent.files.forEach { ret[it.name] = Type.FILE }
@@ -26,7 +25,14 @@ object SingleMerge {
return buildList {
source.directories.forEach { sourceDir ->
targetNames?.remove(sourceDir.name)
- visitDir(targetContent, sourceDir.name, sourceDir.list(), this@buildList, root, path, options)
+ visitDir(
+ targetContent,
+ sourceDir.name,
+ sourceDir.list(),
+ this@buildList,
+ context,
+ path,
+ )
}
source.files.forEach { sourceFile ->
targetNames?.remove(sourceFile.name)
@@ -34,7 +40,7 @@ object SingleMerge {
}
source.links.forEach { sourceLink ->
targetNames?.remove(sourceLink.name)
- visitLink(targetContent, sourceLink, this@buildList, root, path, options)
+ visitLink(targetContent, sourceLink, this@buildList, context, path)
}
targetNames?.forEach { (name, type) ->
when (type) {
@@ -52,16 +58,15 @@ object SingleMerge {
sourceName: String,
sourceContent: Directory.Content,
actions: MutableList<Action>,
- root: Directory,
+ context: Context,
path: String,
- options: Options,
) {
val sourcePath = PathUtils.join(path, sourceName)
if (targetContent == null) {
actions.add(
Action.ChangeDir(
sourceName,
- visit(null, sourceContent, root, sourcePath, options),
+ visit(null, sourceContent, context, sourcePath),
create = true,
),
)
@@ -69,7 +74,10 @@ object SingleMerge {
val targetDir = targetContent.directories.find { it.name == sourceName }
if (targetDir != null) {
actions.add(
- Action.ChangeDir(sourceName, visit(targetDir, sourceContent, root, sourcePath, options)),
+ Action.ChangeDir(
+ sourceName,
+ visit(targetDir, sourceContent, context, sourcePath),
+ ),
)
} else {
if (targetContent.files.any { it.name == sourceName }) {
@@ -80,7 +88,7 @@ object SingleMerge {
actions.add(
Action.ChangeDir(
sourceName,
- visit(null, sourceContent, root, sourcePath, options),
+ visit(null, sourceContent, context, sourcePath),
create = true,
),
)
@@ -122,12 +130,11 @@ object SingleMerge {
targetContent: Directory.Content?,
sourceLink: Link,
actions: MutableList<Action>,
- root: Directory,
+ context: Context,
path: String,
- options: Options,
- ) {
+ ) {
val linkTarget = sourceLink.resolve()
- if (!options.resolveLinks && insideRoot(root, path, linkTarget.path)) {
+ if (!context.options.resolveLinks && insideRoot(context.root, path, linkTarget.path)) {
if (targetContent == null) {
actions.add(Action.Link(sourceLink.name, linkTarget.path))
} else {
@@ -160,9 +167,8 @@ object SingleMerge {
sourceLink.name,
linkTarget.directory.list(),
actions,
- root,
+ context,
path,
- options,
)
}
@@ -198,4 +204,9 @@ object SingleMerge {
FILE,
LINK,
}
+
+ private data class Context(
+ val root: Directory,
+ val options: Options,
+ )
}