Class EasyBind

java.lang.Object
com.tobiasdiez.easybind.EasyBind

public class EasyBind extends Object
Methods for easy creation of bindings.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
     
    static interface 
     
    static interface 
     
    static interface 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Subscription
    bindConditionally(javafx.beans.property.Property<T> target, javafx.beans.value.ObservableValue<? extends T> source, javafx.beans.value.ObservableValue<Boolean> condition)
    Deprecated.
    Since 1.0.2.
    static <T> Subscription
    bindContent(List<? super T> target, javafx.collections.ObservableList<? extends T> source)
    Synchronizes the content of the target list with the source list.
    static <A, B, R> EasyBinding<R>
    combine(javafx.beans.value.ObservableValue<A> src1, javafx.beans.value.ObservableValue<B> src2, BiFunction<A,B,R> f)
     
    static <A, B, C, R> EasyBinding<R>
    combine(javafx.beans.value.ObservableValue<A> src1, javafx.beans.value.ObservableValue<B> src2, javafx.beans.value.ObservableValue<C> src3, EasyBind.TriFunction<A,B,C,R> f)
     
    static <A, B, C, D, R>
    EasyBinding<R>
    combine(javafx.beans.value.ObservableValue<A> src1, javafx.beans.value.ObservableValue<B> src2, javafx.beans.value.ObservableValue<C> src3, javafx.beans.value.ObservableValue<D> src4, EasyBind.TetraFunction<A,B,C,D,R> f)
     
    static <A, B, C, D, E, R>
    EasyBinding<R>
    combine(javafx.beans.value.ObservableValue<A> src1, javafx.beans.value.ObservableValue<B> src2, javafx.beans.value.ObservableValue<C> src3, javafx.beans.value.ObservableValue<D> src4, javafx.beans.value.ObservableValue<E> src5, EasyBind.PentaFunction<A,B,C,D,E,R> f)
     
    static <A, B, C, D, E, F, R>
    EasyBinding<R>
    combine(javafx.beans.value.ObservableValue<A> src1, javafx.beans.value.ObservableValue<B> src2, javafx.beans.value.ObservableValue<C> src3, javafx.beans.value.ObservableValue<D> src4, javafx.beans.value.ObservableValue<E> src5, javafx.beans.value.ObservableValue<F> src6, EasyBind.HexaFunction<A,B,C,D,E,F,R> f)
     
    static <T, R> EasyBinding<R>
    combine(javafx.collections.ObservableList<? extends javafx.beans.value.ObservableValue<? extends T>> list, Function<? super Stream<T>,? extends R> f)
     
    static <T> EasyObservableList<T>
    concat(javafx.collections.ObservableList<? extends T>... sources)
     
    static <T> EasyObservableList<T>
    flatten(javafx.collections.ObservableList<javafx.collections.ObservableList<? extends T>> sources)
     
    static <T> Subscription
    includeWhen(Collection<T> collection, T element, javafx.beans.value.ObservableValue<Boolean> condition)
    Adds element to collection when condition is true and removes it from collection when condition is false.
    static <T> Subscription
    listen(javafx.beans.value.ObservableValue<T> observable, javafx.beans.InvalidationListener listener)
    Adds an invalidation listener and returns a Subscription that can be used to remove that listener.
    static <T> Subscription
    listen(javafx.beans.value.ObservableValue<T> observable, javafx.beans.value.ChangeListener<? super T> listener)
    Adds a change listener and returns a Subscription that can be used to remove that listener.
    static <T, U> EasyBinding<U>
    map(javafx.beans.value.ObservableValue<T> source, Function<? super T,? extends U> mapper)
    Returns an observable consisting of the result of applying the given function to the given observable value.
    static <T, U> EasyObservableList<U>
    map(javafx.collections.ObservableList<? extends T> sourceList, Function<? super T,? extends U> f)
     
    static <A, B> EasyObservableList<B>
    mapBacked(javafx.collections.ObservableList<A> source, Function<A,B> mapper)
    Creates a new list in which each element is converted using the provided mapping.
    static <A, B> EasyObservableList<B>
    mapBacked(javafx.collections.ObservableList<A> source, Function<A,B> mapper, boolean mapOnUpdate)
    Similar to mapBacked(ObservableList, Function), but allows specifying if new objects should be created on update.
    static <T, O, R extends javafx.beans.value.ObservableValue<O>>
    EasyBinding<O>
    mapObservable(javafx.beans.value.ObservableValue<T> source, Function<? super T,R> mapper)
    Returns an observable that, when the given observable value holds value x, holds the value held by the observable f(x).
    static <T, R> EasyBinding<R>
    reduce(javafx.collections.ObservableList<? extends T> list, Function<? super Stream<? extends T>,? extends R> accumulator)
    Creates a new binding that performs a reduction on the elements of this list, using the provided accumulation function.
    static <T> SelectBuilder<T>
    select(javafx.beans.value.ObservableValue<T> selectionRoot)
    Starts a selection chain.
    static <T, U> PropertyBinding<U>
    selectProperty(javafx.beans.value.ObservableValue<T> source, Function<? super T,? extends javafx.beans.property.Property<U>> mapper)
    Similar to mapObservable(ObservableValue, Function), except the returned binding is also a property.
    static <T> Subscription
    subscribe(javafx.beans.value.ObservableValue<T> observable, Consumer<? super T> subscriber)
    Invokes subscriber for the current and every new value of observable.
    static <E> OptionalBinding<E>
    valueAt(javafx.collections.ObservableList<E> list, int index)
    Creates a new OptionalBinding that contains the element of an ObservableList at the specified position.
    static <K, V> OptionalBinding<V>
    valueAt(javafx.collections.ObservableMap<K,V> map, K key)
    Creates a new OptionalBinding that contains the mapping of a specific key in an ObservableMap.
    static When
    when(javafx.beans.value.ObservableValue<Boolean> condition)
    Entry point for creating conditional bindings.
    static <T> EasyObservableValue<T>
    wrap(javafx.beans.value.ObservableValue<T> value)
    Creates a wrapper around the given observable to provide access to convenient helper methods (for fluent style)
    static <E> EasyObservableList<E>
    wrapList(javafx.collections.ObservableList<E> list)
    Creates a wrapper around the given observable list to provide access to convenient helper methods (for fluent style)
    wrapNullable(javafx.beans.value.ObservableValue<T> value)
    Creates a wrapper around the given observable value that provides functionality similar to an Optional.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • EasyBind

      public EasyBind()
  • Method Details

    • wrap

      public static <T> EasyObservableValue<T> wrap(javafx.beans.value.ObservableValue<T> value)
      Creates a wrapper around the given observable to provide access to convenient helper methods (for fluent style)
      Parameters:
      value - the observable to wrap
      Returns:
      a thin wrapper around the given observable
    • wrapNullable

      public static <T> ObservableOptionalValue<T> wrapNullable(javafx.beans.value.ObservableValue<T> value)
      Creates a wrapper around the given observable value that provides functionality similar to an Optional.
      Parameters:
      value - the observable to wrap
      Returns:
      a thin wrapper around the given observable
    • wrapList

      public static <E> EasyObservableList<E> wrapList(javafx.collections.ObservableList<E> list)
      Creates a wrapper around the given observable list to provide access to convenient helper methods (for fluent style)
      Parameters:
      list - the observable list to wrap
      Returns:
      a thin wrapper around the given observable list
    • map

      public static <T, U> EasyBinding<U> map(javafx.beans.value.ObservableValue<T> source, Function<? super T,? extends U> mapper)
      Returns an observable consisting of the result of applying the given function to the given observable value.

      The value passed to the mapper may be null. If this is not desired, use wrapNullable(source).map(mapper) instead.

      Parameters:
      source - the original observable value serving as input
      mapper - the function to apply
      Returns:
      the new observable value
      See Also:
    • mapObservable

      public static <T, O, R extends javafx.beans.value.ObservableValue<O>> EasyBinding<O> mapObservable(javafx.beans.value.ObservableValue<T> source, Function<? super T,R> mapper)
      Returns an observable that, when the given observable value holds value x, holds the value held by the observable f(x).

      Note that the mapper is also invoked if x is null. A better null-handling is possible with ObservableOptionalValue.mapObservable(Function) )}.

      Parameters:
      source - the original observable value serving as input
      mapper - the function to apply, returning an observable value
      See Also:
    • selectProperty

      public static <T, U> PropertyBinding<U> selectProperty(javafx.beans.value.ObservableValue<T> source, Function<? super T,? extends javafx.beans.property.Property<U>> mapper)
      Similar to mapObservable(ObservableValue, Function), except the returned binding is also a property. This means you can call setValue() and bind() methods on the returned property, which delegates to the currently selected property.

      As the value of the source changes, so does the currently selected property. When the property returned from this method is bound, as the selected property changes, the previously selected property is unbound and the newly selected property is bound.

      Note that if the currently selected property is null, then calling getValue() on the returned value will return null regardless of any prior call to setValue() or bind().

      Note that the mapper is also invoked if source holds a null value. A better null-handling is possible with ObservableOptionalValue.selectProperty(Function) )}.

      Note that you need to retain a reference to the returned value to prevent it from being garbage collected.

    • map

      public static <T, U> EasyObservableList<U> map(javafx.collections.ObservableList<? extends T> sourceList, Function<? super T,? extends U> f)
    • flatten

      public static <T> EasyObservableList<T> flatten(javafx.collections.ObservableList<javafx.collections.ObservableList<? extends T>> sources)
    • concat

      @SafeVarargs public static <T> EasyObservableList<T> concat(javafx.collections.ObservableList<? extends T>... sources)
    • mapBacked

      public static <A, B> EasyObservableList<B> mapBacked(javafx.collections.ObservableList<A> source, Function<A,B> mapper)
      Creates a new list in which each element is converted using the provided mapping. All changes to the underlying list are propagated to the converted list.

      If the change event indicates that an item was updated, as determined by ListChangeListener.Change.wasUpdated(), the mapping function is called to create a new object reflecting the updated value.

      In contrast to map(ObservableList, Function), the items are converted when they are inserted instead of when they are accessed. Thus, the initial CPU overhead and memory consumption is higher but the access to list items is quicker.

    • mapBacked

      public static <A, B> EasyObservableList<B> mapBacked(javafx.collections.ObservableList<A> source, Function<A,B> mapper, boolean mapOnUpdate)
      Similar to mapBacked(ObservableList, Function), but allows specifying if new objects should be created on update.

      If mapOnUpdate is true, new objects are created when items in the source list are updated.

      If mapOnUpdate is false, updates do not create new objects. This can be useful in scenarios where the mapped objects already have bindings or listeners that reflect changes from the source objects.

    • combine

      public static <A, B, R> EasyBinding<R> combine(javafx.beans.value.ObservableValue<A> src1, javafx.beans.value.ObservableValue<B> src2, BiFunction<A,B,R> f)
    • combine

      public static <A, B, C, R> EasyBinding<R> combine(javafx.beans.value.ObservableValue<A> src1, javafx.beans.value.ObservableValue<B> src2, javafx.beans.value.ObservableValue<C> src3, EasyBind.TriFunction<A,B,C,R> f)
    • combine

      public static <A, B, C, D, R> EasyBinding<R> combine(javafx.beans.value.ObservableValue<A> src1, javafx.beans.value.ObservableValue<B> src2, javafx.beans.value.ObservableValue<C> src3, javafx.beans.value.ObservableValue<D> src4, EasyBind.TetraFunction<A,B,C,D,R> f)
    • combine

      public static <A, B, C, D, E, R> EasyBinding<R> combine(javafx.beans.value.ObservableValue<A> src1, javafx.beans.value.ObservableValue<B> src2, javafx.beans.value.ObservableValue<C> src3, javafx.beans.value.ObservableValue<D> src4, javafx.beans.value.ObservableValue<E> src5, EasyBind.PentaFunction<A,B,C,D,E,R> f)
    • combine

      public static <A, B, C, D, E, F, R> EasyBinding<R> combine(javafx.beans.value.ObservableValue<A> src1, javafx.beans.value.ObservableValue<B> src2, javafx.beans.value.ObservableValue<C> src3, javafx.beans.value.ObservableValue<D> src4, javafx.beans.value.ObservableValue<E> src5, javafx.beans.value.ObservableValue<F> src6, EasyBind.HexaFunction<A,B,C,D,E,F,R> f)
    • combine

      public static <T, R> EasyBinding<R> combine(javafx.collections.ObservableList<? extends javafx.beans.value.ObservableValue<? extends T>> list, Function<? super Stream<T>,? extends R> f)
    • reduce

      public static <T, R> EasyBinding<R> reduce(javafx.collections.ObservableList<? extends T> list, Function<? super Stream<? extends T>,? extends R> accumulator)
      Creates a new binding that performs a reduction on the elements of this list, using the provided accumulation function.
      Parameters:
      list - the source list
      accumulator - the accumulation function to apply
      See Also:
    • select

      public static <T> SelectBuilder<T> select(javafx.beans.value.ObservableValue<T> selectionRoot)
      Starts a selection chain. A selection chain is just a more efficient equivalent to a chain of flatMaps.
    • bindConditionally

      @Deprecated public static <T> Subscription bindConditionally(javafx.beans.property.Property<T> target, javafx.beans.value.ObservableValue<? extends T> source, javafx.beans.value.ObservableValue<Boolean> condition)
      Deprecated.
      Since 1.0.2. Use when(condition).bind(target, source) instead.
      Sets up automatic binding and unbinding of target to/from source, based on the changing value of condition. In other words, this method starts watching condition for changes. When condition changes to true, target is bound to source. When condition changes to false, target is unbound. This keeps happening until either unsubscribe() is called on the returned subscription, or target is garbage collected.
      Parameters:
      target - target of the conditional binding
      source - source of the conditional binding
      condition - controls when to bind and unbind target to/from source
      Returns:
      a subscription that can be used to dispose the conditional binding set up by this method, i.e. stop observing condition and unbind target from source.
    • bindContent

      public static <T> Subscription bindContent(List<? super T> target, javafx.collections.ObservableList<? extends T> source)
      Synchronizes the content of the target list with the source list.

      Calling this method ensures that the target contains the same elements as the source. If the content of the target changes, the source will be updated automatically.

      Once source is bound to an ObservableList, it must not be changed directly anymore.

      The binding can be removed by calling Subscription.unsubscribe() on the returned subscription. This is the only difference to Bindings.bindContent(List, ObservableList).

      Type Parameters:
      T - the type of the source elements
      Parameters:
      target - the target list
      source - the source list
      Returns:
      a subscription that can be used to stop syncing the lists.
    • when

      public static When when(javafx.beans.value.ObservableValue<Boolean> condition)
      Entry point for creating conditional bindings.
    • includeWhen

      public static <T> Subscription includeWhen(Collection<T> collection, T element, javafx.beans.value.ObservableValue<Boolean> condition)
      Adds element to collection when condition is true and removes it from collection when condition is false.
      Returns:
      a subscription that can be used to stop observing condition and manipulating collection.
    • subscribe

      public static <T> Subscription subscribe(javafx.beans.value.ObservableValue<T> observable, Consumer<? super T> subscriber)
      Invokes subscriber for the current and every new value of observable.
      Parameters:
      observable - observable value to subscribe to
      subscriber - action to invoke for values of observable.
      Returns:
      a subscription that can be used to stop invoking subscriber for any further observable changes.
    • listen

      public static <T> Subscription listen(javafx.beans.value.ObservableValue<T> observable, javafx.beans.InvalidationListener listener)
      Adds an invalidation listener and returns a Subscription that can be used to remove that listener.
       
       Subscription s = observable.listen(obs -> doSomething());
      
       // later
       s.unsubscribe();
       

      is equivalent to

       
       InvalidationListener l = obs -> doSomething();
       observable.addListener(l);
      
       // later
       observable.removeListener();
       
    • listen

      public static <T> Subscription listen(javafx.beans.value.ObservableValue<T> observable, javafx.beans.value.ChangeListener<? super T> listener)
      Adds a change listener and returns a Subscription that can be used to remove that listener. See the example at listen(ObservableValue, InvalidationListener).
    • valueAt

      public static <E> OptionalBinding<E> valueAt(javafx.collections.ObservableList<E> list, int index)
      Creates a new OptionalBinding that contains the element of an ObservableList at the specified position. The binding will be empty if the index points behind the ObservableList or to a null element.
      Parameters:
      list - the ObservableList
      index - the position in the List
      Returns:
      the new OptionalBinding
      Throws:
      NullPointerException - if the ObservableList is null
      IllegalArgumentException - if (@code index < 0)
    • valueAt

      public static <K, V> OptionalBinding<V> valueAt(javafx.collections.ObservableMap<K,V> map, K key)
      Creates a new OptionalBinding that contains the mapping of a specific key in an ObservableMap. The binding will be empty if the key is not contained in the map or points to a null element.
      Parameters:
      map - the ObservableMap
      key - the key in the Map
      Returns:
      the new ObjectBinding
      Throws:
      NullPointerException - if the ObservableMap is null