package org.implab.gradle.common.utils;

import java.util.function.Supplier;

import javax.inject.Inject;

import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.NamedDomainObjectFactory;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;

import groovy.lang.Closure;

public interface ObjectsMixin {
    @Inject
    ObjectFactory getObjects();

    @Inject
    ProviderFactory getProviders();

    default <T> Provider<T> provider(Supplier<T> supplier) {
        return getProviders().provider(supplier::get);
    }

    default <T> Provider<T> provider(Closure<T> closure) {
        return getProviders().provider(closure);
    }

    default <T> T newInstance(Class<T> clazz) {
        return getObjects().newInstance(clazz);
    }

    default <T> T newInstance(Class<T> clazz, Object... params) {
        return getObjects().newInstance(clazz, params);
    }

    default <T> NamedDomainObjectContainer<T> newContainer(Class<T> clazz) {
        return getObjects().domainObjectContainer(clazz);
    }

    default <T> NamedDomainObjectContainer<T> newContainer(Class<T> clazz, NamedDomainObjectFactory<T> factory) {
        return getObjects().domainObjectContainer(clazz, factory);
    }
}
