]> git.basschouten.com Git - openhab-addons.git/blob
bb8838697ebfe2ed2e6d0b45fef3813e8e7d6784
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.nibeuplink.internal;
14
15 import java.util.concurrent.Future;
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * trait class which contains useful helper methods. Thus, the interface can be implemented and methods are available
23  * within the class.
24  *
25  * @author Alexander Friese - initial contribution
26  */
27 @NonNullByDefault
28 public interface AtomicReferenceTrait {
29
30     /**
31      * this should usually not called directly. use updateJobReference or cancelJobReference instead
32      *
33      * @param job job to cancel.
34      */
35     default void cancelJob(@Nullable Future<?> job) {
36         if (job != null) {
37             job.cancel(true);
38         }
39     }
40
41     /**
42      * updates a job reference with a new job. the old job will be cancelled if there is one.
43      *
44      * @param jobReference reference to be updated
45      * @param newJob job to be assigned
46      */
47     default void updateJobReference(AtomicReference<@Nullable Future<?>> jobReference, Future<?> newJob) {
48         cancelJob(jobReference.getAndSet(newJob));
49     }
50
51     /**
52      * updates a job reference to null and cancels any existing job which might be assigned to the reference.
53      *
54      * @param jobReference to be updated to null.
55      */
56     default void cancelJobReference(AtomicReference<@Nullable Future<?>> jobReference) {
57         cancelJob(jobReference.getAndSet(null));
58     }
59 }