no.digipost.concurrent.OneTimeAssignment Maven / Gradle / Ivy
Show all versions of digg Show documentation
/*
* Copyright (C) Posten Norge AS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package no.digipost.concurrent;
import no.digipost.util.Assignment;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
/**
* A reference which may or may not be assigned a value, with the added constraint
* that it can only be assigned once. If it is assigned a value more than one time
* it will throw an exception.
*
* The class is thread-safe in the sense that it is not possible for concurrent threads to
* assign a value to it twice, though relevant cases for concurrent use are probably limited. The
* motivation is rather to enable fail-fast for erroneous API-usage involving assignments,
* e.g. builder implementations.
*
*
* @param The type of the object which is referenced.
*/
public final class OneTimeAssignment implements Assignment {
/**
* Create a new non-assigned instance with a default value.
*
* @param defaultValue The default value to use in case an assignment
* through {@link #set(Object)} is never performed.
* @see #defaultTo(Supplier)
*/
public static final OneTimeAssignment defaultTo(V defaultValue) {
return defaultTo(() -> defaultValue);
}
/**
* Create a new non-assigned instance with a default value.
*
* @param defaultValue The supplier to acquire the default value to use in case an
* assignment through {@link #set(Object)} is never performed.
*/
public static final OneTimeAssignment defaultTo(Supplier extends V> defaultValue) {
return new OneTimeAssignment<>(defaultValue);
}
/**
* Create a new non-assigned instance.
*/
public static final OneTimeAssignment newInstance() {
return new OneTimeAssignment(() -> UNASSIGNED);
}
private final AtomicReference