-
-
Notifications
You must be signed in to change notification settings - Fork 727
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
840 additions
and
29 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
168 changes: 168 additions & 0 deletions
168
projects/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Multibinding.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,168 @@ | ||
/* | ||
* Copyright 2017-Present the original author or authors. | ||
* | ||
* 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. | ||
*/ | ||
package org.koin.core.module | ||
|
||
import co.touchlab.stately.concurrency.AtomicInt | ||
import org.koin.core.parameter.ParametersHolder | ||
import org.koin.core.qualifier.Qualifier | ||
import org.koin.core.qualifier.StringQualifier | ||
import org.koin.core.scope.Scope | ||
import org.koin.ext.getFullName | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Multibinding of Map & Set | ||
* | ||
* @author - luozejiaqun | ||
*/ | ||
inline fun <reified K, reified V> mapMultibindingQualifier(): Qualifier = | ||
StringQualifier("map_multibinding_${K::class.getFullName()}_${V::class.getFullName()}") | ||
|
||
inline fun <reified E> setMultibindingQualifier(): Qualifier = | ||
StringQualifier("set_multibinding_${E::class.getFullName()}") | ||
|
||
@PublishedApi | ||
internal fun <K> multibindingValueQualifier(multibindingQualifier: Qualifier, key: K): Qualifier = | ||
StringQualifier("${multibindingQualifier.value}_of_$key") | ||
|
||
@PublishedApi | ||
internal fun <K> multibindingIterateKeyQualifier(multibindingQualifier: Qualifier, key: K): Qualifier = | ||
StringQualifier("${multibindingQualifier.value}_iterate_$key") | ||
|
||
@PublishedApi | ||
internal class MultibindingIterateKey<T>(val key: T, val valueQualifier: Qualifier) | ||
|
||
@PublishedApi | ||
internal class MapMultibinding<K : Any, V>( | ||
createdAtStart: Boolean, | ||
private val scope: Scope, | ||
private val qualifier: Qualifier, | ||
private val valueClass: KClass<*>, | ||
private val parametersHolder: ParametersHolder, | ||
) : Map<K, V> { | ||
|
||
init { | ||
if (createdAtStart) { | ||
values | ||
} | ||
} | ||
|
||
override val keys: Set<K> | ||
get() { | ||
val multibindingKeys = mutableSetOf<K>() | ||
scope.getAll<MultibindingIterateKey<*>>() | ||
.mapNotNullTo(multibindingKeys) { multibindingIterateKey -> | ||
if (multibindingIterateKey.valueQualifier.value.startsWith(qualifier.value)) { | ||
multibindingIterateKey.key as? K | ||
} else { | ||
null | ||
} | ||
} | ||
return multibindingKeys | ||
} | ||
|
||
override val size: Int | ||
get() = keys.size | ||
|
||
override val values: Collection<V> | ||
get() = keys.map { get(it) } | ||
|
||
override val entries: Set<Map.Entry<K, V>> | ||
get() { | ||
val filed = LinkedHashSet<Map.Entry<K, V>>() | ||
keys.mapTo(filed) { Entry(it, get(it)) } | ||
return filed | ||
} | ||
|
||
override fun containsKey(key: K): Boolean = | ||
keys.contains(key) | ||
|
||
override fun containsValue(value: V): Boolean = | ||
values.contains(value) | ||
|
||
override fun get(key: K): V { | ||
return scope.get(valueClass, multibindingValueQualifier(qualifier, key)) { parametersHolder } | ||
} | ||
|
||
override fun isEmpty(): Boolean = keys.isEmpty() | ||
|
||
private class Entry<K, V>(override val key: K, override val value: V) : Map.Entry<K, V> | ||
} | ||
|
||
@PublishedApi | ||
internal class SetMultibinding<E>( | ||
createdAtStart: Boolean, | ||
private val scope: Scope, | ||
private val qualifier: Qualifier, | ||
private val valueClass: KClass<*>, | ||
private val parametersHolder: ParametersHolder, | ||
) : Set<E> { | ||
init { | ||
if (createdAtStart) { | ||
getAll() | ||
} | ||
} | ||
|
||
override val size: Int | ||
get() = getAllKeys().size | ||
|
||
override fun contains(element: E): Boolean { | ||
return getAll().contains(element) | ||
} | ||
|
||
override fun containsAll(elements: Collection<E>): Boolean { | ||
return getAll().containsAll(elements) | ||
} | ||
|
||
override fun isEmpty(): Boolean = size == 0 | ||
|
||
override fun iterator(): Iterator<E> { | ||
return getAll().iterator() | ||
} | ||
|
||
private fun getAllKeys(): Set<Key> { | ||
val multibindingKeys = mutableSetOf<Key>() | ||
scope.getAll<MultibindingIterateKey<*>>() | ||
.mapNotNullTo(multibindingKeys) { multibindingIterateKey -> | ||
if (multibindingIterateKey.valueQualifier.value.startsWith(qualifier.value)) { | ||
multibindingIterateKey.key as? Key | ||
} else { | ||
null | ||
} | ||
} | ||
return multibindingKeys | ||
} | ||
|
||
private fun getAll(): Set<E> { | ||
val result = LinkedHashSet<E>() | ||
getAllKeys().mapTo(result) { get(it) } | ||
return result | ||
} | ||
|
||
private fun get(key: Key): E { | ||
return scope.get(valueClass, multibindingValueQualifier(qualifier, key)) { parametersHolder } | ||
} | ||
|
||
class Key(private val placeholder: Int) { | ||
override fun toString(): String = "placeholder_$placeholder" | ||
} | ||
|
||
companion object { | ||
private val accumulatingKey = AtomicInt(0) | ||
|
||
fun getDistinctKey() = Key(accumulatingKey.incrementAndGet()) | ||
} | ||
} |
Oops, something went wrong.