All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.quantummaid.injectmaid.namespaces.NamespaceBuilder.kt Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2021 Richard Hauswald - https://quantummaid.de/.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 de.quantummaid.injectmaid.namespaces

import de.quantummaid.injectmaid.InjectMaidBuilder
import de.quantummaid.injectmaid.api.ReusePolicy
import de.quantummaid.injectmaid.api.ReusePolicy.PROTOTYPE
import de.quantummaid.injectmaid.api.customtype.api.Builder
import de.quantummaid.injectmaid.api.customtype.api.CustomType
import de.quantummaid.injectmaid.api.customtype.api.CustomType.customType
import de.quantummaid.injectmaid.api.customtype.api.FactoryBuilder00
import de.quantummaid.reflectmaid.GenericType
import de.quantummaid.reflectmaid.GenericType.Companion.genericType

class NamespaceBuilder(val namespace: GenericType, val injectMaidBuilder: InjectMaidBuilder) {

    companion object {
        inline fun  InjectMaidBuilder.withNamespace(builder: NamespaceBuilder.() -> Unit): InjectMaidBuilder {
            val namespace = genericType()
            val namespaceBuilder = NamespaceBuilder(namespace, this)
            builder(namespaceBuilder)
            return this
        }
    }

    inline fun  importing(): NamespaceBuilder {
        val genericType = genericType()
        injectMaidBuilder.withCustomType(
            customType(genericType(NamespacedType::class.java, genericType, namespace))
                .withDependency(genericType)
                .usingFactory { NamespacedType(it) }
        )
        return this
    }

    inline fun  exporting(): NamespaceBuilder {
        val genericType = genericType()
        injectMaidBuilder.withCustomType(
            customType(genericType)
                .withDependency(genericType(NamespacedType::class.java, genericType, namespace))
                .usingFactory {
                    @Suppress("UNCHECKED_CAST")
                    (it as NamespacedType).dependency()
                }
        )
        return this
    }

    inline fun  customType(reusePolicy: ReusePolicy = PROTOTYPE, builder: (FactoryBuilder00) -> CustomType): NamespaceBuilder {
        val genericType = genericType(NamespacedType::class.java, genericType(), namespace)
        val internalBuilder = Builder.builder(genericType, namespace)
        val entry: FactoryBuilder00 = FactoryBuilder00(internalBuilder)
        val customType = builder(entry)
        injectMaidBuilder.withCustomType(customType, reusePolicy)
        return this
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy