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

de.larssh.utils.Finals Maven / Gradle / Ivy

// Generated by delombok at Sat Apr 03 10:50:23 UTC 2021
package de.larssh.utils;

import java.util.function.Supplier;
import edu.umd.cs.findbugs.annotations.Nullable;

/**
 * This class contains helper methods for final fields.
 */
public final class Finals {
	/**
	 * Returns {@code value} unchanged. This prevents compilers from inlining
	 * constants (static final fields). Depending classes referring to the constant
	 * do not need to be recompiled if the constant value changes.
	 *
	 * 

* Inlining affects primitive data types and strings. * *

* Usage example: *

public static final String CONSTANT_FIELD = constant("constant value");
* * @param return type * @param value value * @return value */ public static T constant(final T value) { return value; } /** * Returns a new {@link Supplier} that calculates its return value at most one * time. Therefore it can be used to create lazy fields. * *

* This implementation is synchronized. {@code supplier} is guaranteed to be * called at max once. * * @param return type * @param supplier value supplier * @return value */ public static Supplier lazy(final Supplier supplier) { return new CachedSupplier<>(supplier); } /** * {@link Supplier} implementation proxying another supplier while calculating * its return value at most one time. * *

* Threads are synchronized upon calling the wrapped supplier. * * @param return type */ private static class CachedSupplier implements Supplier { /** * Wrapped supplier * * @return wrapped supplier */ private final Supplier supplier; /** * Cached value or empty optional * * @return cached value or empty optional */ @Nullable private volatile T value = null; /** * Object used for locking */ private final Object lock = new Object(); /** {@inheritDoc} */ @Override public T get() { synchronized (lock) { if (value == null) { value = supplier.get(); } } return Nullables.orElseThrow(value); } @java.lang.SuppressWarnings("all") @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code") @lombok.Generated public CachedSupplier(final Supplier supplier) { this.supplier = supplier; } } @java.lang.SuppressWarnings("all") @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code") @lombok.Generated private Finals() { throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated"); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy