2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nibeuplink.internal;
15 import java.util.concurrent.Future;
16 import java.util.concurrent.atomic.AtomicReference;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
22 * trait class which contains useful helper methods. Thus, the interface can be implemented and methods are available
25 * @author Alexander Friese - initial contribution
28 public interface AtomicReferenceTrait {
31 * this should usually not called directly. use updateJobReference or cancelJobReference instead
33 * @param job job to cancel.
35 default void cancelJob(@Nullable Future<?> job) {
42 * updates a job reference with a new job. the old job will be cancelled if there is one.
44 * @param jobReference reference to be updated
45 * @param newJob job to be assigned
47 default void updateJobReference(AtomicReference<@Nullable Future<?>> jobReference, Future<?> newJob) {
48 cancelJob(jobReference.getAndSet(newJob));
52 * updates a job reference to null and cancels any existing job which might be assigned to the reference.
54 * @param jobReference to be updated to null.
56 default void cancelJobReference(AtomicReference<@Nullable Future<?>> jobReference) {
57 cancelJob(jobReference.getAndSet(null));