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

net.cassite.pure.aop.Generator Maven / Gradle / Ivy

The newest version!
package net.cassite.pure.aop;

import net.cassite.style.interfaces.RFunc0;

/**
 * it's an object generator, in which the RFunc0[R] apply() method would only be called at most once, it will store the value, and use it for other invocations
 */
public class Generator {
        private final RFunc0 func;
        private boolean valueRetrieved = false;
        private R r;

        public Generator(RFunc0 func) {
                this.func = func;
        }

        /**
         * get object retrieved from the function
         *
         * @return object retrieved from the function
         * @throws Throwable exception
         */
        public R get() throws Throwable {
                if (!valueRetrieved) {
                        synchronized (func) {
                                if (!valueRetrieved) {
                                        r = func.apply();
                                        valueRetrieved = true;
                                }
                        }
                }
                return r;
        }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy