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

net.dongliu.commons.async.LazyService Maven / Gradle / Ivy

There is a newer version: 12.0.2
Show newest version
package net.dongliu.commons.async;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.function.Supplier;

/**
 * Wrapper service to lazy create
 *
 * @author Liu Dong
 */

public class LazyService {

    @SuppressWarnings("unchecked")
    public static  T create(Class interfaceClass, Supplier supplier) {
        Lazy lazy = Lazy.create(supplier);
        InvocationHandler ih = (proxy, method, args) -> method.invoke(lazy.get(), args);
        T service = (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, ih);
        return service;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy