io.micronaut.kotlin.context.BeanLocatorExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-kotlin-extension-functions Show documentation
Show all versions of micronaut-kotlin-extension-functions Show documentation
Includes extensions to Micronaut for Kotlin
/*
* Copyright 2017-2020 original 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
*
* https://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 io.micronaut.kotlin.context
import io.micronaut.context.BeanLocator
import io.micronaut.kotlin.inject.qualifierByName
import io.micronaut.kotlin.inject.qualifierByStereotype
import java.util.stream.Stream
import kotlin.streams.asSequence
/**
* Extension for [BeanLocator.getBean] providing a `getBean()` variant.
*
* @param T The bean type
* @return The bean instance
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.getBean(): T = getBean(T::class.java)
/**
* Extension for [BeanLocator.getBean] providing a `getBean(name)` variant.
*
* @param T The bean type
* @param name The bean name
* @return The bean instance
* @author Luiz Picanço
* @since 2.1.2
*/
inline fun BeanLocator.getBean(name: String): T = getBean(T::class.java, qualifierByName(name))
/**
* Extension for [BeanLocator.getBean] providing a `getStereotypedBean()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return The bean instance
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.getStereotypedBean(): T =
getBean(T::class.java, qualifierByStereotype())
/**
* Extension for [BeanLocator.findBean] providing a `findBean()` variant.
*
* @param T The bean type
* @return The bean instance or null
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.findBean(): T? = findBean(T::class.java).orElse(null)
/**
* Extension for [BeanLocator.findBean] providing a `findStereotypedBean()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return The bean instance or null
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.findStereotypedBean(): T? =
findBean(T::class.java, qualifierByStereotype()).orElse(null)
/**
* Extension for [BeanLocator.getBeansOfType] providing a `getBeansOfType()` variant.
*
* @param T The bean type
* @return A [Collection] of bean instances
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.getBeansOfType(): Collection = getBeansOfType(T::class.java)
/**
* Extension for [BeanLocator.getBeansOfType] providing a `getStereotypedBeansOfType()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return A [Collection] of bean instances
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.getStereotypedBeansOfType(): Collection =
getBeansOfType(T::class.java, qualifierByStereotype())
/**
* Extension for [BeanLocator.streamOfType] providing a `streamOfType()` variant.
*
* @param T The bean type
* @return A [Stream] of bean instances
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.streamOfType(): Stream = streamOfType(T::class.java)
/**
* Extension for [BeanLocator.streamOfType] providing a `sequenceOfType()` variant.
*
* @param T The bean type
* @return A [Sequence] of bean instances
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.sequenceOfType(): Sequence = streamOfType(T::class.java).asSequence()
/**
* Extension for [BeanLocator.streamOfType] providing a `streamOfStereotypedType()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return A [Stream] of bean instances
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.streamOfStereotypedType(): Stream =
streamOfType(T::class.java, qualifierByStereotype())
/**
* Extension for [BeanLocator.streamOfType] providing a `sequenceOfStereotypedType()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return A [Sequence] of bean instances
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.sequenceOfStereotypedType(): Sequence =
streamOfType(T::class.java, qualifierByStereotype()).asSequence()
/**
* Extension for [BeanLocator.getProxyTargetBean] providing a `getProxyTargetBean()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return The original bean instance
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.getProxyTargetBean(): T =
getProxyTargetBean(T::class.java, qualifierByStereotype())
/**
* Extension for [BeanLocator.findOrInstantiateBean] providing a `findOrInstantiateBean()` variant.
*
* @param T The bean type
* @return The bean instance or null
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanLocator.findOrInstantiateBean(): T? = findOrInstantiateBean(T::class.java).orElse(null)