org.glassfish.pfl.basic.contain.ResourceFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webservices-rt Show documentation
Show all versions of webservices-rt Show documentation
This module contains the Metro runtime code.
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.glassfish.pfl.basic.contain;
import org.glassfish.pfl.basic.func.NullaryFunction;
/** A thread-safe, contention-free lazy resource factory.
*
* @param Type created by resource factory.
* @author ken
*/
public class ResourceFactory {
// Volatile double-checked locking. Can't use resource holder for
// a general-purpose library, because it requires a class per value.
// Resource holder also doesn't work for non-singleton values.
private volatile T value ;
private final NullaryFunction cons ;
public ResourceFactory( NullaryFunction cons ) {
this.cons = cons ;
}
public T get() {
if (value != null) {
return value ;
}
synchronized (this) {
if (value == null) {
value = cons.evaluate() ;
}
return value ;
}
}
}