All Downloads are FREE. Search and download functionalities are using the official Maven repository.

griffon.javafx.beans.binding.UIThreadAwareBindings Maven / Gradle / Ivy

/*
 * SPDX-License-Identifier: Apache-2.0
 *
 * Copyright 2008-2018 the original author or authors.
 *
 * 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 griffon.javafx.beans.binding;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.binding.FloatBinding;
import javafx.beans.binding.IntegerBinding;
import javafx.beans.binding.LongBinding;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.FloatProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.LongProperty;
import javafx.beans.property.MapProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SetProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableBooleanValue;
import javafx.beans.value.ObservableDoubleValue;
import javafx.beans.value.ObservableFloatValue;
import javafx.beans.value.ObservableIntegerValue;
import javafx.beans.value.ObservableLongValue;
import javafx.beans.value.ObservableStringValue;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.collections.ObservableSet;
import javafx.collections.SetChangeListener;

import javax.annotation.Nonnull;
import java.util.function.Consumer;

import static java.util.Objects.requireNonNull;

/**
 * @author Andres Almiray
 * @since 2.9.0
 */
public final class UIThreadAwareBindings {
    private static final String ERROR_LISTENER_NULL = "Argument 'listener' must not be null";
    private static final String ERROR_CONSUMER_NULL = "Argument 'consumer' must not be null";
    private static final String ERROR_RUNNABLE_NULL = "Argument 'runnable' must not be null";
    private static final String ERROR_OBSERVABLE_NULL = "Argument 'observable' must not be null";
    private static final String ERROR_PROPERTY_NULL = "Argument 'property' must not be null";
    private static final String ERROR_BINDING_NULL = "Argument 'binding' must not be null";

    private UIThreadAwareBindings() {
        // prevent instantiation
    }

    /**
     * Registers a {@code ChangeListener} on the supplied observable that will notify the target property.
     *
     * @param property   the property that will be notified of value changes.
     * @param observable the observable on which the listener will be registered.
     *
     * @return the wrapped change listener.
     */
    public static  ChangeListener uiThreadAwareBind(@Nonnull final Property property, @Nonnull final ObservableValue observable) {
        requireNonNull(property, ERROR_PROPERTY_NULL);
        property.setValue(observable.getValue());
        return uiThreadAwareChangeListener(observable, (v, o, n) -> property.setValue(n));
    }

    /**
     * Registers a {@code ChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param listener   the wrapped change listener.
     *
     * @return a {@code ChangeListener}.
     */
    public static  ChangeListener uiThreadAwareChangeListener(@Nonnull final ObservableValue observable, @Nonnull ChangeListener listener) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        ChangeListener uiListener = uiThreadAwareChangeListener(listener);
        observable.addListener(uiListener);
        return uiListener;
    }

    /**
     * Creates a {@code ChangeListener} that always handles notifications inside the UI thread.
     *
     * @param listener the wrapped change listener.
     *
     * @return a {@code ChangeListener}.
     */
    @Nonnull
    public static  ChangeListener uiThreadAwareChangeListener(@Nonnull ChangeListener listener) {
        requireNonNull(listener, ERROR_LISTENER_NULL);
        return listener instanceof UIThreadAware ? listener : new UIThreadAwareChangeListener<>(listener);
    }

    /**
     * Registers a {@code ChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param consumer   the consumer of the {@code newValue} argument.
     *
     * @return a {@code ChangeListener}.
     */
    public static  ChangeListener uiThreadAwareChangeListener(@Nonnull final ObservableValue observable, @Nonnull final Consumer consumer) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        ChangeListener listener = uiThreadAwareChangeListener(consumer);
        observable.addListener(listener);
        return listener;
    }

    /**
     * Creates a {@code ChangeListener} that always handles notifications inside the UI thread.
     *
     * @param consumer the consumer of the {@code newValue} argument.
     *
     * @return a {@code ChangeListener}.
     */
    @Nonnull
    public static  ChangeListener uiThreadAwareChangeListener(@Nonnull final Consumer consumer) {
        requireNonNull(consumer, ERROR_CONSUMER_NULL);
        return new UIThreadAwareChangeListener<>((observable, oldValue, newValue) -> consumer.accept(newValue));
    }

    /**
     * Registers a {@code ChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param runnable   the code to be executed when the listener is notified.
     *
     * @return a {@code ChangeListener}.
     */
    public static  ChangeListener uiThreadAwareChangeListener(@Nonnull final ObservableValue observable, @Nonnull final Runnable runnable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        ChangeListener listener = uiThreadAwareChangeListener(runnable);
        observable.addListener(listener);
        return listener;
    }

    /**
     * Creates a {@code ChangeListener} that always handles notifications inside the UI thread.
     *
     * @param runnable the code to be executed when the listener is notified.
     *
     * @return a {@code ChangeListener}.
     */
    @Nonnull
    public static  ChangeListener uiThreadAwareChangeListener(@Nonnull final Runnable runnable) {
        requireNonNull(runnable, ERROR_RUNNABLE_NULL);
        return new UIThreadAwareChangeListener<>((observable, oldValue, newValue) -> runnable.run());
    }

    /**
     * Registers a {@code InvalidationListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param listener   the wrapped invalidation listener.
     *
     * @return an {@code InvalidationListener}.
     */
    public static InvalidationListener uiThreadAwareInvalidationListener(@Nonnull final Observable observable, @Nonnull InvalidationListener listener) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        InvalidationListener uiListener = uiThreadAwareInvalidationListener(listener);
        observable.addListener(uiListener);
        return uiListener;
    }

    /**
     * Creates a {@code InvalidationListener} that always handles notifications inside the UI thread.
     *
     * @param listener the wrapped invalidation listener.
     *
     * @return a {@code InvalidationListener}.
     */
    @Nonnull
    public static InvalidationListener uiThreadAwareInvalidationListener(@Nonnull InvalidationListener listener) {
        requireNonNull(listener, ERROR_LISTENER_NULL);
        return listener instanceof UIThreadAware ? listener : new UIThreadAwareInvalidationListener(listener);
    }

    /**
     * Registers a {@code InvalidationListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param consumer   the consumer of the {@code observable} argument.
     *
     * @return a {@code InvalidationListener}.
     */
    public static InvalidationListener uiThreadAwareInvalidationListener(@Nonnull final Observable observable, @Nonnull final Consumer consumer) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        InvalidationListener listener = uiThreadAwareInvalidationListener(consumer);
        observable.addListener(listener);
        return listener;
    }

    /**
     * Creates a {@code InvalidationListener} that always handles notifications inside the UI thread.
     *
     * @param consumer the consumer of the {@code observable} argument.
     *
     * @return a {@code InvalidationListener}.
     */
    @Nonnull
    public static InvalidationListener uiThreadAwareInvalidationListener(@Nonnull final Consumer consumer) {
        requireNonNull(consumer, ERROR_CONSUMER_NULL);
        return new UIThreadAwareInvalidationListener(consumer::accept);
    }

    /**
     * Registers a {@code InvalidationListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param runnable   the code to be executed when the listener is notified.
     *
     * @return a {@code InvalidationListener}.
     */
    public static InvalidationListener uiThreadAwareInvalidationListener(@Nonnull final Observable observable, @Nonnull final Runnable runnable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        InvalidationListener listener = uiThreadAwareInvalidationListener(runnable);
        observable.addListener(listener);
        return listener;
    }

    /**
     * Creates a {@code InvalidationListener} that always handles notifications inside the UI thread.
     *
     * @param runnable the code to be executed when the listener is notified.
     *
     * @return a {@code InvalidationListener}.
     */
    @Nonnull
    public static InvalidationListener uiThreadAwareInvalidationListener(@Nonnull final Runnable runnable) {
        requireNonNull(runnable, ERROR_RUNNABLE_NULL);
        return new UIThreadAwareInvalidationListener(observable -> runnable.run());
    }

    /**
     * Registers a {@code ListChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param listener   the wrapped list change listener.
     *
     * @return a {@code ListChangeListener}.
     */
    public static  ListChangeListener uiThreadAwareListChangeListener(@Nonnull final ObservableList observable, @Nonnull ListChangeListener listener) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        ListChangeListener uiListener = uiThreadAwareListChangeListener(listener);
        observable.addListener(uiListener);
        return listener;
    }

    /**
     * Creates a {@code ListChangeListener} that always handles notifications inside the UI thread.
     *
     * @param listener the wrapped list change listener.
     *
     * @return a {@code ListChangeListener}.
     */
    @Nonnull
    public static  ListChangeListener uiThreadAwareListChangeListener(@Nonnull ListChangeListener listener) {
        requireNonNull(listener, ERROR_LISTENER_NULL);
        return listener instanceof UIThreadAware ? listener : new UIThreadAwareListChangeListener<>(listener);
    }

    /**
     * Registers a {@code ListChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param consumer   the consumer of the {@code newValue} argument.
     *
     * @return a {@code ListChangeListener}.
     */
    public static  ListChangeListener uiThreadAwareListChangeListener(@Nonnull final ObservableList observable, @Nonnull final Consumer> consumer) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        ListChangeListener listener = uiThreadAwareListChangeListener(consumer);
        observable.addListener(listener);
        return listener;
    }

    /**
     * Creates a {@code ListChangeListener} that always handles notifications inside the UI thread.
     *
     * @param consumer the consumer of the {@code change} argument.
     *
     * @return a {@code ListChangeListener}.
     */
    @Nonnull
    public static  ListChangeListener uiThreadAwareListChangeListener(@Nonnull final Consumer> consumer) {
        requireNonNull(consumer, ERROR_CONSUMER_NULL);
        return new UIThreadAwareListChangeListener(consumer::accept);
    }

    /**
     * Registers a {@code ListChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param runnable   the code to be executed when the listener is notified.
     *
     * @return a {@code ListChangeListener}.
     */
    public static  ListChangeListener uiThreadAwareListChangeListener(@Nonnull final ObservableList observable, @Nonnull final Runnable runnable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        ListChangeListener listener = uiThreadAwareListChangeListener(runnable);
        observable.addListener(listener);
        return listener;
    }

    /**
     * Creates a {@code ListChangeListener} that always handles notifications inside the UI thread.
     *
     * @param runnable the code to be executed when the listener is notified.
     *
     * @return a {@code ListChangeListener}.
     */
    @Nonnull
    public static  ListChangeListener uiThreadAwareListChangeListener(@Nonnull final Runnable runnable) {
        requireNonNull(runnable, ERROR_RUNNABLE_NULL);
        return new UIThreadAwareListChangeListener<>(change -> runnable.run());
    }

    /**
     * Registers a {@code MapChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param listener   the wrapped map change listener.
     *
     * @return a {@code MapChangeListener}.
     */
    public static  MapChangeListener uiThreadAwareMapChangeListener(@Nonnull final ObservableMap observable, @Nonnull MapChangeListener listener) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        MapChangeListener uiListener = uiThreadAwareMapChangeListener(listener);
        observable.addListener(uiListener);
        return listener;
    }

    /**
     * Creates a {@code MapChangeListener} that always handles notifications inside the UI thread.
     *
     * @param listener the wrapped map change listener.
     *
     * @return a {@code MapChangeListener}.
     */
    @Nonnull
    public static  MapChangeListener uiThreadAwareMapChangeListener(@Nonnull MapChangeListener listener) {
        requireNonNull(listener, ERROR_LISTENER_NULL);
        return listener instanceof UIThreadAware ? listener : new UIThreadAwareMapChangeListener<>(listener);
    }

    /**
     * Registers a {@code MapChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param consumer   the consumer of the {@code newValue} argument.
     *
     * @return a {@code MapChangeListener}.
     */
    public static  MapChangeListener uiThreadAwareMapChangeListener(@Nonnull final ObservableMap observable, @Nonnull final Consumer> consumer) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        MapChangeListener listener = uiThreadAwareMapChangeListener(consumer);
        observable.addListener(listener);
        return listener;
    }

    /**
     * Creates a {@code MapChangeListener} that always handles notifications inside the UI thread.
     *
     * @param consumer the consumer of the {@code change} argument.
     *
     * @return a {@code MapChangeListener}.
     */
    @Nonnull
    public static  MapChangeListener uiThreadAwareMapChangeListener(@Nonnull final Consumer> consumer) {
        requireNonNull(consumer, ERROR_CONSUMER_NULL);
        return new UIThreadAwareMapChangeListener(consumer::accept);
    }

    /**
     * Registers a {@code MapChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param runnable   the code to be executed when the listener is notified.
     *
     * @return a {@code MapChangeListener}.
     */
    public static  MapChangeListener uiThreadAwareMapChangeListener(@Nonnull final ObservableMap observable, @Nonnull final Runnable runnable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        MapChangeListener listener = uiThreadAwareMapChangeListener(runnable);
        observable.addListener(listener);
        return listener;
    }

    /**
     * Creates a {@code MapChangeListener} that always handles notifications inside the UI thread.
     *
     * @param runnable the code to be executed when the listener is notified.
     *
     * @return a {@code MapChangeListener}.
     */
    @Nonnull
    public static  MapChangeListener uiThreadAwareMapChangeListener(@Nonnull final Runnable runnable) {
        requireNonNull(runnable, ERROR_RUNNABLE_NULL);
        return new UIThreadAwareMapChangeListener<>(change -> runnable.run());
    }

    /**
     * Registers a {@code SetChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param listener   the wrapped set change listener.
     *
     * @return a {@code SetChangeListener}.
     */
    public static  SetChangeListener uiThreadAwareSetChangeListener(@Nonnull final ObservableSet observable, @Nonnull SetChangeListener listener) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        SetChangeListener uiListener = uiThreadAwareSetChangeListener(listener);
        observable.addListener(uiListener);
        return listener;
    }

    /**
     * Creates a {@code SetChangeListener} that always handles notifications inside the UI thread.
     *
     * @param listener the wrapped set change listener.
     *
     * @return a {@code SetChangeListener}.
     */
    @Nonnull
    public static  SetChangeListener uiThreadAwareSetChangeListener(@Nonnull SetChangeListener listener) {
        requireNonNull(listener, ERROR_LISTENER_NULL);
        return listener instanceof UIThreadAware ? listener : new UIThreadAwareSetChangeListener<>(listener);
    }

    /**
     * Registers a {@code SetChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param consumer   the consumer of the {@code newValue} argument.
     *
     * @return a {@code SetChangeListener}.
     */
    public static  SetChangeListener uiThreadAwareSetChangeListener(@Nonnull final ObservableSet observable, @Nonnull final Consumer> consumer) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        SetChangeListener listener = uiThreadAwareSetChangeListener(consumer);
        observable.addListener(listener);
        return listener;
    }

    /**
     * Creates a {@code SetChangeListener} that always handles notifications inside the UI thread.
     *
     * @param consumer the consumer of the {@code change} argument.
     *
     * @return a {@code SetChangeListener}.
     */
    @Nonnull
    public static  SetChangeListener uiThreadAwareSetChangeListener(@Nonnull final Consumer> consumer) {
        requireNonNull(consumer, ERROR_CONSUMER_NULL);
        return new UIThreadAwareSetChangeListener(consumer::accept);
    }

    /**
     * Registers a {@code SetChangeListener} that always handles notifications inside the UI thread.
     *
     * @param observable the observable on which the listener will be registered.
     * @param runnable   the code to be executed when the listener is notified.
     *
     * @return a {@code SetChangeListener}.
     */
    public static  SetChangeListener uiThreadAwareSetChangeListener(@Nonnull final ObservableSet observable, @Nonnull final Runnable runnable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        SetChangeListener listener = uiThreadAwareSetChangeListener(runnable);
        observable.addListener(listener);
        return listener;
    }

    /**
     * Creates a {@code SetChangeListener} that always handles notifications inside the UI thread.
     *
     * @param runnable the code to be executed when the listener is notified.
     *
     * @return a {@code SetChangeListener}.
     */
    @Nonnull
    public static  SetChangeListener uiThreadAwareSetChangeListener(@Nonnull final Runnable runnable) {
        requireNonNull(runnable, ERROR_RUNNABLE_NULL);
        return new UIThreadAwareSetChangeListener<>(change -> runnable.run());
    }

    /**
     * Creates an observable boolean property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable boolean property to wrap.
     *
     * @return an observable boolean property.
     */
    @Nonnull
    public static BooleanProperty uiThreadAwareBooleanProperty(@Nonnull BooleanProperty observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareBooleanProperty(observable);
    }

    /**
     * Creates an observable integer property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable integer property to wrap.
     *
     * @return an observable integer property.
     */
    @Nonnull
    public static IntegerProperty uiThreadAwareIntegerProperty(@Nonnull IntegerProperty observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareIntegerProperty(observable);
    }

    /**
     * Creates an observable long property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable long property to wrap.
     *
     * @return an observable long property.
     */
    @Nonnull
    public static LongProperty uiThreadAwareLongProperty(@Nonnull LongProperty observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareLongProperty(observable);
    }

    /**
     * Creates an observable float property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable float property to wrap.
     *
     * @return an observable float property.
     */
    @Nonnull
    public static FloatProperty uiThreadAwareFloatProperty(@Nonnull FloatProperty observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareFloatProperty(observable);
    }

    /**
     * Creates an observable double property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable double property to wrap.
     *
     * @return an observable double property.
     */
    @Nonnull
    public static DoubleProperty uiThreadAwareDoubleProperty(@Nonnull DoubleProperty observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareDoubleProperty(observable);
    }

    /**
     * Creates an observable string property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable string property to wrap.
     *
     * @return an observable string property.
     */
    @Nonnull
    public static StringProperty uiThreadAwareStringProperty(@Nonnull StringProperty observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareStringProperty(observable);
    }

    /**
     * Creates an observable boolean property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable boolean property to wrap.
     *
     * @return an observable boolean property.
     */
    @Nonnull
    public static Property uiThreadAwarePropertyBoolean(@Nonnull Property observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwarePropertyBoolean(observable);
    }

    /**
     * Creates an observable integer property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable integer property to wrap.
     *
     * @return an observable integer property.
     */
    @Nonnull
    public static Property uiThreadAwarePropertyInteger(@Nonnull Property observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwarePropertyInteger(observable);
    }

    /**
     * Creates an observable long property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable long property to wrap.
     *
     * @return an observable long property.
     */
    @Nonnull
    public static Property uiThreadAwarePropertyLong(@Nonnull Property observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwarePropertyLong(observable);
    }

    /**
     * Creates an observable float property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable float property to wrap.
     *
     * @return an observable float property.
     */
    @Nonnull
    public static Property uiThreadAwarePropertyFloat(@Nonnull Property observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwarePropertyFloat(observable);
    }

    /**
     * Creates an observable double property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable double property to wrap.
     *
     * @return an observable double property.
     */
    @Nonnull
    public static Property uiThreadAwarePropertyDouble(@Nonnull Property observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwarePropertyDouble(observable);
    }

    /**
     * Creates an observable string property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable string property to wrap.
     *
     * @return an observable string property.
     */
    @Nonnull
    public static Property uiThreadAwarePropertyString(@Nonnull Property observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwarePropertyString(observable);
    }

    /**
     * Creates an observable object property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable object property to wrap.
     *
     * @return an observable object property.
     */
    @Nonnull
    public static  ObjectProperty uiThreadAwareObjectProperty(@Nonnull final ObjectProperty observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareObjectProperty<>(observable);
    }

    /**
     * Creates an observable list property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable list property to wrap.
     *
     * @return an observable list property.
     */
    @Nonnull
    public static  ListProperty uiThreadAwareListProperty(@Nonnull ListProperty observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareListProperty<>(observable);
    }

    /**
     * Creates an observable set property that notifies its seteners inside the UI thread.
     *
     * @param observable the observable set property to wrap.
     *
     * @return an observable set property.
     */
    @Nonnull
    public static  SetProperty uiThreadAwareSetProperty(@Nonnull SetProperty observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareSetProperty<>(observable);
    }

    /**
     * Creates an observable map property that notifies its listeners inside the UI thread.
     *
     * @param observable the observable map property to wrap.
     *
     * @return an observable map property.
     */
    @Nonnull
    public static  MapProperty uiThreadAwareMapProperty(@Nonnull MapProperty observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareMapProperty<>(observable);
    }

    /**
     * Creates an observable value that notifies its listeners inside the UI thread.
     *
     * @param observable the observable to wrap.
     *
     * @return an observable value.
     */
    @Nonnull
    public static  ObservableValue uiThreadAwareObservable(@Nonnull final ObservableValue observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareObservableObjectValue<>(observable);
    }

    /**
     * Creates an observable string value that notifies its listeners inside the UI thread.
     *
     * @param observable the observable string to wrap.
     *
     * @return an observable string value.
     */
    @Nonnull
    public static ObservableStringValue uiThreadAwareObservableString(@Nonnull final ObservableStringValue observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareObservableStringValue(observable);
    }

    /**
     * Creates an observable boolean value that notifies its listeners inside the UI thread.
     *
     * @param observable the observable boolean to wrap.
     *
     * @return an observable boolean value.
     */
    @Nonnull
    public static ObservableBooleanValue uiThreadAwareObservableBoolean(@Nonnull final ObservableBooleanValue observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareObservableBooleanValue(observable);
    }

    /**
     * Creates an observable integer value that notifies its listeners inside the UI thread.
     *
     * @param observable the observable integer to wrap.
     *
     * @return an observable integer value.
     */
    @Nonnull
    public static ObservableIntegerValue uiThreadAwareObservableInteger(@Nonnull final ObservableIntegerValue observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareObservableIntegerValue(observable);
    }

    /**
     * Creates an observable long value that notifies its listeners inside the UI thread.
     *
     * @param observable the observable long to wrap.
     *
     * @return an observable long value.
     */
    @Nonnull
    public static ObservableLongValue uiThreadAwareObservableLong(@Nonnull final ObservableLongValue observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareObservableLongValue(observable);
    }

    /**
     * Creates an observable float value that notifies its listeners inside the UI thread.
     *
     * @param observable the observable float to wrap.
     *
     * @return an observable float value.
     */
    @Nonnull
    public static ObservableFloatValue uiThreadAwareObservableFloat(@Nonnull final ObservableFloatValue observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareObservableFloatValue(observable);
    }

    /**
     * Creates an observable double value that notifies its listeners inside the UI thread.
     *
     * @param observable the observable double to wrap.
     *
     * @return an observable double value.
     */
    @Nonnull
    public static ObservableDoubleValue uiThreadAwareObservableDouble(@Nonnull final ObservableDoubleValue observable) {
        requireNonNull(observable, ERROR_OBSERVABLE_NULL);
        return observable instanceof UIThreadAware ? observable : new UIThreadAwareObservableDoubleValue(observable);
    }

    /**
     * Creates a boolean binding that notifies its listeners inside the UI thread.
     *
     * @param binding the boolean binding to wrap.
     *
     * @return a boolean binding.
     *
     * @since 2.13.0
     */
    @Nonnull
    public static BooleanBinding uiThreadAwareBooleanBinding(@Nonnull BooleanBinding binding) {
        requireNonNull(binding, ERROR_BINDING_NULL);
        return binding instanceof UIThreadAware ? binding : new UIThreadAwareBooleanBinding(binding);
    }

    /**
     * Creates an integer binding that notifies its listeners inside the UI thread.
     *
     * @param binding the integer binding to wrap.
     *
     * @return an integer binding.
     *
     * @since 2.13.0
     */
    @Nonnull
    public static IntegerBinding uiThreadAwareIntegerBinding(@Nonnull IntegerBinding binding) {
        requireNonNull(binding, ERROR_BINDING_NULL);
        return binding instanceof UIThreadAware ? binding : new UIThreadAwareIntegerBinding(binding);
    }

    /**
     * Creates a long binding that notifies its listeners inside the UI thread.
     *
     * @param binding the long binding to wrap.
     *
     * @return a long binding.
     *
     * @since 2.13.0
     */
    @Nonnull
    public static LongBinding uiThreadAwareLongBinding(@Nonnull LongBinding binding) {
        requireNonNull(binding, ERROR_BINDING_NULL);
        return binding instanceof UIThreadAware ? binding : new UIThreadAwareLongBinding(binding);
    }

    /**
     * Creates a float binding that notifies its listeners inside the UI thread.
     *
     * @param binding the float binding to wrap.
     *
     * @return a float binding.
     *
     * @since 2.13.0
     */
    @Nonnull
    public static FloatBinding uiThreadAwareFloatBinding(@Nonnull FloatBinding binding) {
        requireNonNull(binding, ERROR_BINDING_NULL);
        return binding instanceof UIThreadAware ? binding : new UIThreadAwareFloatBinding(binding);
    }

    /**
     * Creates a double binding that notifies its listeners inside the UI thread.
     *
     * @param binding the double binding to wrap.
     *
     * @return a double binding.
     *
     * @since 2.13.0
     */
    @Nonnull
    public static DoubleBinding uiThreadAwareDoubleBinding(@Nonnull DoubleBinding binding) {
        requireNonNull(binding, ERROR_BINDING_NULL);
        return binding instanceof UIThreadAware ? binding : new UIThreadAwareDoubleBinding(binding);
    }

    /**
     * Creates a string binding that notifies its listeners inside the UI thread.
     *
     * @param binding the string binding to wrap.
     *
     * @return a string binding.
     *
     * @since 2.13.0
     */
    @Nonnull
    public static StringBinding uiThreadAwareStringBinding(@Nonnull StringBinding binding) {
        requireNonNull(binding, ERROR_BINDING_NULL);
        return binding instanceof UIThreadAware ? binding : new UIThreadAwareStringBinding(binding);
    }

    /**
     * Creates an object binding that notifies its listeners inside the UI thread.
     *
     * @param binding the object binding to wrap.
     *
     * @return an object binding.
     *
     * @since 2.13.0
     */
    @Nonnull
    public static  ObjectBinding uiThreadAwareObjectBinding(@Nonnull ObjectBinding binding) {
        requireNonNull(binding, ERROR_BINDING_NULL);
        return binding instanceof UIThreadAware ? binding : new UIThreadAwareObjectBinding<>(binding);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy