From 149f0184e80219139c989f288d2f11d885ebeb90 Mon Sep 17 00:00:00 2001 From: guicamest Date: Sat, 23 Dec 2023 22:32:25 +0100 Subject: [PATCH] chore: Rename state types; ScanDirectoriesState, SizeCompareState, ContentCompareState --- .../core/FindDuplicatesExecution.kt | 24 ++++---- .../core/FindDuplicatesState.kt | 34 +++++------ .../gduplicatefinder/core/ScanFileVisitor.kt | 6 +- .../gduplicatefinder/core/StopResumeTest.kt | 56 +++++++++---------- 4 files changed, 60 insertions(+), 60 deletions(-) diff --git a/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/FindDuplicatesExecution.kt b/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/FindDuplicatesExecution.kt index 8a1e3db9..93a576c2 100644 --- a/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/FindDuplicatesExecution.kt +++ b/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/FindDuplicatesExecution.kt @@ -37,7 +37,7 @@ class CoroutinesFindDuplicatesExecution( filter: PathFilter, ) : this( scope = scope, - initialState = ScanExecutionStateImpl.empty(initialDirectories = directories, filter = filter), + initialState = ScanDirectoriesStateImpl.empty(initialDirectories = directories, filter = filter), ) override suspend fun duplicateEntries(): Collection = result.await() @@ -53,28 +53,28 @@ class CoroutinesFindDuplicatesExecution( init { job = scope.launch(CoroutineName("findDuplicatesExecution")) { - if (ScanExecutionState::class in stateHolder) { + if (ScanDirectoriesState::class in stateHolder) { collectFiles(stateHolder) - stateHolder.updateStateToSizeFilter() + stateHolder.updateStateToSizeCompare() } delay(1L) - if (SizeFilterExecutionState::class in stateHolder) { + if (SizeCompareState::class in stateHolder) { groupFilesBySize(stateHolder) - stateHolder.updateStateToContentFilter() + stateHolder.updateStateToContentCompare() } delay(1) groupFilesByContent(stateHolder) result.complete( - stateHolder.stateAs().processedFiles.duplicateGroups(), + stateHolder.stateAs().processedFiles.duplicateGroups(), ) } } private fun collectFiles(stateHolder: FindProgressStateHolder) { val (initialDirectories, filter) = - stateHolder.stateAs().run { + stateHolder.stateAs().run { initialDirectories to filter } val visitor = ScanFileVisitor(filter, stateHolder) @@ -84,7 +84,7 @@ class CoroutinesFindDuplicatesExecution( } private fun groupFilesBySize(stateHolder: FindProgressStateHolder) { - val filesToProcess = stateHolder.stateAs().filesToProcess + val filesToProcess = stateHolder.stateAs().filesToProcess val destination = HashMap>(initialSize(filesToProcess)) val filteredFiles = filesToProcess @@ -95,7 +95,7 @@ class CoroutinesFindDuplicatesExecution( .flatMap { it.value } .toSet() - stateHolder.update { currentState: SizeFilterExecutionStateImpl -> + stateHolder.update { currentState: SizeCompareStateImpl -> currentState.copy(processedFiles = filteredFiles, filesToProcess = emptySet()) } } @@ -104,7 +104,7 @@ class CoroutinesFindDuplicatesExecution( private fun groupFilesByContent(stateHolder: FindProgressStateHolder) { val filesWithHashes = - with(stateHolder.stateAs()) { + with(stateHolder.stateAs()) { processedFiles + collectHashes(stateHolder, filesToProcess) } @@ -120,7 +120,7 @@ class CoroutinesFindDuplicatesExecution( .flatMap { it.value } .toSet() - stateHolder.update { currentState: ContentFilterExecutionStateImpl -> + stateHolder.update { currentState: ContentCompareStateImpl -> currentState.copy(processedFiles = filteredFiles) } } @@ -136,7 +136,7 @@ class CoroutinesFindDuplicatesExecution( attributes = pwa.attributes, contentHash = pwa.contentHash(), ) - stateHolder.update { currentState: ContentFilterExecutionStateImpl -> + stateHolder.update { currentState: ContentCompareStateImpl -> currentState.copy( filesToProcess = currentState.filesToProcess - pwa, processedFiles = currentState.processedFiles + pathWithAttributesAndContent, diff --git a/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/FindDuplicatesState.kt b/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/FindDuplicatesState.kt index b0ae36a2..3edca53f 100644 --- a/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/FindDuplicatesState.kt +++ b/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/FindDuplicatesState.kt @@ -7,25 +7,25 @@ import java.nio.file.Path sealed interface FindDuplicatesExecutionState -interface ScanExecutionState : FindDuplicatesExecutionState { +interface ScanDirectoriesState : FindDuplicatesExecutionState { val initialDirectories: Collection val filter: PathFilter val visitedDirectories: Set val filesToProcess: Set } -data class ScanExecutionStateImpl( +data class ScanDirectoriesStateImpl( override val initialDirectories: Collection, override val filter: PathFilter, override val visitedDirectories: Set, override val filesToProcess: Set, -) : ScanExecutionState { +) : ScanDirectoriesState { companion object { internal fun empty( initialDirectories: Collection, filter: PathFilter, - ): ScanExecutionState = - ScanExecutionStateImpl( + ): ScanDirectoriesState = + ScanDirectoriesStateImpl( initialDirectories = initialDirectories, filter = filter, visitedDirectories = emptySet(), @@ -34,25 +34,25 @@ data class ScanExecutionStateImpl( } } -interface SizeFilterExecutionState : FindDuplicatesExecutionState { +interface SizeCompareState : FindDuplicatesExecutionState { val filesToProcess: Set val processedFiles: Set } -data class SizeFilterExecutionStateImpl( +data class SizeCompareStateImpl( override val filesToProcess: Set, override val processedFiles: Set, -) : SizeFilterExecutionState +) : SizeCompareState -interface ContentFilterExecutionState : FindDuplicatesExecutionState { +interface ContentCompareState : FindDuplicatesExecutionState { val filesToProcess: Set val processedFiles: Set } -data class ContentFilterExecutionStateImpl( +data class ContentCompareStateImpl( override val filesToProcess: Set, override val processedFiles: Set, -) : ContentFilterExecutionState +) : ContentCompareState internal class FindProgressStateHolder(initial: FindDuplicatesExecutionState) { private val _state = MutableStateFlow(initial) @@ -74,17 +74,17 @@ internal class FindProgressStateHolder(initial: FindDuplicatesExecutionState) { } } -internal fun FindProgressStateHolder.updateStateToSizeFilter() = - update { currentState: ScanExecutionState -> - SizeFilterExecutionStateImpl( +internal fun FindProgressStateHolder.updateStateToSizeCompare() = + update { currentState: ScanDirectoriesState -> + SizeCompareStateImpl( filesToProcess = currentState.filesToProcess, processedFiles = emptySet(), ) } -internal fun FindProgressStateHolder.updateStateToContentFilter() = - update { currentState: SizeFilterExecutionState -> - ContentFilterExecutionStateImpl( +internal fun FindProgressStateHolder.updateStateToContentCompare() = + update { currentState: SizeCompareState -> + ContentCompareStateImpl( filesToProcess = currentState.processedFiles, processedFiles = emptySet(), ) diff --git a/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/ScanFileVisitor.kt b/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/ScanFileVisitor.kt index 20181abe..1b35cc9f 100644 --- a/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/ScanFileVisitor.kt +++ b/core/src/main/kotlin/com/sleepcamel/gduplicatefinder/core/ScanFileVisitor.kt @@ -19,7 +19,7 @@ internal class ScanFileVisitor( directory, filter, attributes, - stateHolder.stateAs().visitedDirectories, + stateHolder.stateAs().visitedDirectories, ) ) { FileVisitResult.CONTINUE @@ -56,7 +56,7 @@ internal class ScanFileVisitor( } private fun addFileToState(pathWithAttributes: PathWithAttributes) { - stateHolder.update { currentState: ScanExecutionStateImpl -> + stateHolder.update { currentState: ScanDirectoriesStateImpl -> currentState.copy( filesToProcess = currentState.filesToProcess + pathWithAttributes, ) @@ -64,7 +64,7 @@ internal class ScanFileVisitor( } private fun addVisitedDirectoryToState(directory: Path) { - stateHolder.update { currentState: ScanExecutionStateImpl -> + stateHolder.update { currentState: ScanDirectoriesStateImpl -> currentState.copy( visitedDirectories = currentState.visitedDirectories.plusElement(directory), ) diff --git a/core/src/test/kotlin/com/sleepcamel/gduplicatefinder/core/StopResumeTest.kt b/core/src/test/kotlin/com/sleepcamel/gduplicatefinder/core/StopResumeTest.kt index 0c810ccf..e2de77df 100644 --- a/core/src/test/kotlin/com/sleepcamel/gduplicatefinder/core/StopResumeTest.kt +++ b/core/src/test/kotlin/com/sleepcamel/gduplicatefinder/core/StopResumeTest.kt @@ -57,12 +57,12 @@ class StopResumeTest { fun stopReturnsStateWhileScanning() = runTest { val state = findDuplicates(this, directory = searchDirectory).stop() - assertThat(state).isInstanceOf(ScanExecutionState::class.java) + assertThat(state).isInstanceOf(ScanDirectoriesState::class.java) } @ExperimentalCoroutinesApi @Test - @DisplayName("return size filter state when `stop` is called") + @DisplayName("return size compare state when `stop` is called") fun stopReturnsStateAfterScan() = runTest { val state = @@ -70,13 +70,13 @@ class StopResumeTest { advanceTimeBy(1) stop() } - assertThat(state).isInstanceOf(SizeFilterExecutionState::class.java) + assertThat(state).isInstanceOf(SizeCompareState::class.java) } @ExperimentalCoroutinesApi @Test - @DisplayName("return content filter state when `stop` is called") - fun stopReturnsStateAfterSizeFilter() = + @DisplayName("return content compare state when `stop` is called") + fun stopReturnsStateAfterSizeCompare() = runTest { val state = findDuplicates(this, directory = searchDirectory).run { @@ -84,7 +84,7 @@ class StopResumeTest { advanceTimeBy(1) stop() } - assertThat(state).isInstanceOf(ContentFilterExecutionState::class.java) + assertThat(state).isInstanceOf(ContentCompareState::class.java) } @ExperimentalCoroutinesApi @@ -100,20 +100,20 @@ class StopResumeTest { } advanceUntilIdle() - assertThat(allStates.first()).isInstanceOf(ScanExecutionState::class.java) + assertThat(allStates.first()).isInstanceOf(ScanDirectoriesState::class.java) - val withoutScans = allStates.dropWhile { it is ScanExecutionState } - assertThat(withoutScans.first()).isInstanceOf(SizeFilterExecutionState::class.java) + val withoutScans = allStates.dropWhile { it is ScanDirectoriesState } + assertThat(withoutScans.first()).isInstanceOf(SizeCompareState::class.java) - val withoutSizeFilter = withoutScans.dropWhile { it is SizeFilterExecutionState } - assertThat(withoutSizeFilter).isNotEmpty.allMatch { it is ContentFilterExecutionState } + val withoutSizeStates = withoutScans.dropWhile { it is SizeCompareState } + assertThat(withoutSizeStates).isNotEmpty.allMatch { it is ContentCompareState } } @DisplayName("update scan state as it visits files/directories") @Nested @TestInstance(TestInstance.Lifecycle.PER_CLASS) inner class ScanStateTest { - private lateinit var scanStates: List + private lateinit var scanStates: List @BeforeAll @ExperimentalCoroutinesApi @@ -126,7 +126,7 @@ class StopResumeTest { state.toList(allStates) } advanceUntilIdle() - scanStates = allStates.takeWhile { it is ScanExecutionState }.map { it as ScanExecutionState } + scanStates = allStates.filterIsInstance() } } @@ -183,7 +183,7 @@ class StopResumeTest { @Nested @TestInstance(TestInstance.Lifecycle.PER_CLASS) inner class SizeStateTest { - private lateinit var sizeFilterStates: List + private lateinit var sizeCompareStates: List @BeforeAll @ExperimentalCoroutinesApi @@ -196,30 +196,30 @@ class StopResumeTest { state.toList(allStates) } advanceUntilIdle() - sizeFilterStates = + sizeCompareStates = allStates.dropWhile { - it is ScanExecutionState - }.takeWhile { it is SizeFilterExecutionState }.map { it as SizeFilterExecutionState } + it is ScanDirectoriesState + }.takeWhile { it is SizeCompareState }.map { it as SizeCompareState } } } @Test fun thereShouldBeTwoSizeStates() { - assertThat(sizeFilterStates).hasSize(2) + assertThat(sizeCompareStates).hasSize(2) } @Test fun checkFirstState() { - assertThat(sizeFilterStates[0].filesToProcess.map { it.path }) + assertThat(sizeCompareStates[0].filesToProcess.map { it.path }) .withPathComparator() .containsExactlyInAnyOrderElementsOf(paths) - assertThat(sizeFilterStates[0].processedFiles).isEmpty() + assertThat(sizeCompareStates[0].processedFiles).isEmpty() } @Test fun checkSecondState() { - assertThat(sizeFilterStates[1].filesToProcess).isEmpty() - assertThat(sizeFilterStates[1].processedFiles.map { it.path }) + assertThat(sizeCompareStates[1].filesToProcess).isEmpty() + assertThat(sizeCompareStates[1].processedFiles.map { it.path }) .withPathComparator() .containsExactlyInAnyOrderElementsOf(paths) } @@ -229,7 +229,7 @@ class StopResumeTest { @Nested @TestInstance(TestInstance.Lifecycle.PER_CLASS) inner class ContentStateTest { - private lateinit var contentStates: List + private lateinit var contentStates: List @BeforeAll @ExperimentalCoroutinesApi @@ -244,10 +244,10 @@ class StopResumeTest { advanceUntilIdle() contentStates = allStates.dropWhile { - it !is ContentFilterExecutionState + it !is ContentCompareState }.takeWhile { - it is ContentFilterExecutionState - }.map { it as ContentFilterExecutionState } + it is ContentCompareState + }.map { it as ContentCompareState } } } @@ -318,7 +318,7 @@ class StopResumeTest { state.toList(allStates) } advanceUntilIdle() - val withoutScans = allStates.dropWhile { it is ScanExecutionState } + val withoutScans = allStates.dropWhile { it is ScanDirectoriesState } val execution = resumeFindDuplicates(this, fromState = withoutScans.first()) val duplicateEntries = execution.duplicateEntries() @@ -341,7 +341,7 @@ class StopResumeTest { state.toList(allStates) } advanceUntilIdle() - val contentStates = allStates.dropWhile { it !is ContentFilterExecutionState } + val contentStates = allStates.dropWhile { it !is ContentCompareState } val execution = resumeFindDuplicates(this, fromState = contentStates.first()) val duplicateEntries = execution.duplicateEntries()