2 * Copyright (c) 2010-2023 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.jellyfin.internal.util;
15 import java.util.concurrent.CountDownLatch;
16 import java.util.concurrent.TimeUnit;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.jellyfin.sdk.compatibility.JavaContinuation;
23 * The {@link SyncCallback} util to consume kotlin suspend functions.
25 * @author Miguel Álvarez - Initial contribution
28 public abstract class SyncCallback<T> extends JavaContinuation<@Nullable T> {
29 private final CountDownLatch latch;
33 private Throwable error;
35 protected SyncCallback() {
36 latch = new CountDownLatch(1);
40 public void onSuccess(@Nullable T result) {
46 public void onError(@Nullable Throwable error) {
51 public T awaitResult() throws SyncCallbackError {
52 return awaitResult(10);
55 public T awaitResult(int timeoutSecs) throws SyncCallbackError {
57 if (!latch.await(timeoutSecs, TimeUnit.SECONDS)) {
58 throw new SyncCallbackError("Execution timeout");
60 } catch (InterruptedException e) {
61 throw new SyncCallbackError(e);
63 var error = this.error;
65 throw new SyncCallbackError(error);
67 var result = this.result;
69 throw new SyncCallbackError("Missing result");
74 public static class SyncCallbackError extends Exception {
75 private static final long serialVersionUID = 2157912759968949551L;
77 protected SyncCallbackError(String message) {
81 protected SyncCallbackError(Throwable original) {