forked from eclipse-kuksa/kuksa-android-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Limit Maximum Number of Logs
Closes: eclipse-kuksa#19 Signed-Off-By: Andre Weber <[email protected]>
- Loading branch information
Showing
4 changed files
with
200 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
app/src/main/kotlin/org/eclipse/kuksa/testapp/util/MaxElementSet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.eclipse.kuksa.testapp.util | ||
|
||
class MaxElementSet<T>(private val maxNumberEntries: Int = Int.MAX_VALUE) : MutableSet<T> { | ||
|
||
private val map: LinkedHashMap<T, Boolean> = object : LinkedHashMap<T, Boolean>() { | ||
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<T, Boolean>?): Boolean { | ||
return size > maxNumberEntries | ||
} | ||
} | ||
|
||
override val size: Int | ||
get() = map.size | ||
|
||
override fun contains(element: T): Boolean { | ||
return map[element] == true | ||
} | ||
|
||
override fun containsAll(elements: Collection<T>): Boolean { | ||
var containsAll = true | ||
elements.forEach { element -> | ||
containsAll = containsAll && contains(element) | ||
} | ||
return containsAll | ||
} | ||
|
||
override fun add(element: T): Boolean { | ||
map[element] = true | ||
return true | ||
} | ||
|
||
override fun addAll(elements: Collection<T>): Boolean { | ||
val associatedElements = elements.associateWith { true } | ||
|
||
map.putAll(associatedElements) | ||
return true | ||
} | ||
|
||
override fun clear() { | ||
map.clear() | ||
} | ||
|
||
override fun isEmpty(): Boolean { | ||
return map.isEmpty() | ||
} | ||
|
||
override fun iterator(): MutableIterator<T> { | ||
return map.keys.iterator() | ||
} | ||
|
||
override fun retainAll(elements: Collection<T>): Boolean { | ||
throw UnsupportedOperationException("not supported") | ||
} | ||
|
||
override fun removeAll(elements: Collection<T>): Boolean { | ||
elements.forEach { element -> | ||
map.remove(element) | ||
} | ||
return true | ||
} | ||
|
||
override fun remove(element: T): Boolean { | ||
return map.remove(element) != null | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
app/src/test/kotlin/org/eclipse/kuksa/testapp/util/MaxElementSetTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.eclipse.kuksa.testapp.util | ||
|
||
import io.kotest.core.spec.style.BehaviorSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
class MaxElementSetTest : BehaviorSpec({ | ||
given("An Instance of MaxElementSet with Type TestElement and maxNumberEntries of 100") { | ||
val classUnderTest = MaxElementSet<TestElement>(100) | ||
|
||
`when`("Adding 100 elements") { | ||
val testElements = mutableListOf<TestElement>() | ||
repeat(100) { | ||
val testElement = TestElement() | ||
|
||
testElements.add(testElement) | ||
classUnderTest.add(testElement) | ||
} | ||
|
||
then("All 100 elements are added successfully") { | ||
classUnderTest.size shouldBe 100 | ||
} | ||
|
||
then("The order is kept intact") { | ||
classUnderTest.withIndex().forEach { | ||
val index = it.index | ||
val value = it.value | ||
|
||
value shouldBe testElements[index] | ||
} | ||
} | ||
} | ||
} | ||
|
||
given("An Instance of MaxElementSet with Type TestElement and maxNumberEntries of 10") { | ||
val classUnderTest = MaxElementSet<TestElement>(10) | ||
|
||
`when`("Trying to add the same element twice") { | ||
val testElement = TestElement() | ||
classUnderTest.add(testElement) | ||
classUnderTest.add(testElement) | ||
|
||
then("It is only added once") { | ||
classUnderTest.size shouldBe 1 | ||
} | ||
} | ||
|
||
`when`("Adding more than 10 elements (100)") { | ||
val testElements = mutableListOf<TestElement>() | ||
repeat(100) { | ||
val testElement = TestElement() | ||
|
||
testElements.add(testElement) | ||
classUnderTest.add(testElement) | ||
} | ||
|
||
then("Only the last 10 Elements are kept") { | ||
classUnderTest.size shouldBe 10 | ||
|
||
val subList = testElements.subList(90, 100) | ||
val containsLastTenElements = classUnderTest.containsAll(subList) | ||
containsLastTenElements shouldBe true | ||
} | ||
} | ||
} | ||
}) | ||
|
||
class TestElement |