|
| 1 | +package org.codefx.libfx.listener; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | +import java.util.Optional; |
| 5 | +import java.util.function.BiConsumer; |
| 6 | + |
| 7 | +/** |
| 8 | + * A builder for a {@link ListenerHandle}. |
| 9 | + * <p> |
| 10 | + * The created handle manages whether the listener is currently attached. The functions specified to |
| 11 | + * {@link #onAttach(BiConsumer)} and {@link #onDetach(BiConsumer)} are only called when necessary. This is the case |
| 12 | + * <ul> |
| 13 | + * <li>if {@link ListenerHandle#attach() attach} is called when the listener is not currently added to the observable |
| 14 | + * <li>if {@link ListenerHandle#detach() detach} is called when the listener is currently added to the observable |
| 15 | + * </ul> |
| 16 | + * This implies that they can be stateless functions which simply add and remove the listener. The functions are called |
| 17 | + * with the observable and listener specified during construction. |
| 18 | + * <p> |
| 19 | + * <h2>Example</h2> A typical use looks like this: |
| 20 | + * |
| 21 | + * <pre> |
| 22 | + * Property<String> textProperty; |
| 23 | + * ChangeListener<String> textListener; |
| 24 | + * |
| 25 | + * ListenerHandle textListenerHandle = ListenerHandleBuilder |
| 26 | + * .from(textProperty, textListener) |
| 27 | + * .onAttach((property, listener) -> property.addListener(listener)) |
| 28 | + * .onDetach((property, listener) -> property.removeListener(listener)) |
| 29 | + * .build(); |
| 30 | + * </pre> |
| 31 | + * |
| 32 | + * @param <O> |
| 33 | + * the type of the observable instance (e.g {@link javafx.beans.value.ObservableValue ObservableValue} or |
| 34 | + * {@link javafx.collections.ObservableMap ObservableMap}) to which the listener will be added |
| 35 | + * @param <L> |
| 36 | + * the type of the listener which will be added to the observable |
| 37 | + */ |
| 38 | +public final class ListenerHandleBuilder<O, L> { |
| 39 | + |
| 40 | + // #region FIELDS |
| 41 | + |
| 42 | + /** |
| 43 | + * The observable instance to which the {@link #listener} will be added. |
| 44 | + */ |
| 45 | + private final O observable; |
| 46 | + |
| 47 | + /** |
| 48 | + * The listener which will be added to the {@link #observable}. |
| 49 | + */ |
| 50 | + private final L listener; |
| 51 | + |
| 52 | + /** |
| 53 | + * Called on {@link ListenerHandle#attach()}. |
| 54 | + */ |
| 55 | + private Optional<BiConsumer<? super O, ? super L>> add; |
| 56 | + |
| 57 | + /** |
| 58 | + * Called on {@link ListenerHandle#detach()}. |
| 59 | + */ |
| 60 | + private Optional<BiConsumer<? super O, ? super L>> remove; |
| 61 | + |
| 62 | + // #end FIELDS |
| 63 | + |
| 64 | + // #region CONSTRUCTION |
| 65 | + |
| 66 | + /** |
| 67 | + * Creates a builder for a generic {@link ListenerHandle}. |
| 68 | + * |
| 69 | + * @param observable |
| 70 | + * the observable instance to which the {@code listener} will be added |
| 71 | + * @param listener |
| 72 | + * the listener which will be added to the {@code observable} |
| 73 | + */ |
| 74 | + private ListenerHandleBuilder(O observable, L listener) { |
| 75 | + Objects.requireNonNull(observable, "The argument 'observable' must not be null."); |
| 76 | + Objects.requireNonNull(listener, "The argument 'listener' must not be null."); |
| 77 | + |
| 78 | + this.observable = observable; |
| 79 | + this.listener = listener; |
| 80 | + |
| 81 | + add = Optional.empty(); |
| 82 | + remove = Optional.empty(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Creates a builder for a generic {@link ListenerHandle}. |
| 87 | + * |
| 88 | + * @param <O> |
| 89 | + * the type of the observable instance (e.g {@link javafx.beans.value.ObservableValue ObservableValue} or |
| 90 | + * {@link javafx.collections.ObservableMap ObservableMap}) to which the listener will be added |
| 91 | + * @param <L> |
| 92 | + * the type of the listener which will be added to the observable |
| 93 | + * @param observable |
| 94 | + * the observable instance to which the {@code listener} will be added |
| 95 | + * @param listener |
| 96 | + * the listener which will be added to the {@code observable} |
| 97 | + * @return a {@link ListenerHandleBuilder} for a {@link ListenerHandle}. |
| 98 | + */ |
| 99 | + public static <O, L> ListenerHandleBuilder<O, L> from(O observable, L listener) { |
| 100 | + return new ListenerHandleBuilder<>(observable, listener); |
| 101 | + } |
| 102 | + |
| 103 | + // #end CONSTRUCTION |
| 104 | + |
| 105 | + // #region SET AND BUILD |
| 106 | + |
| 107 | + /** |
| 108 | + * Sets the function which is executed when the built {@link ListenerHandle} must add the listener because |
| 109 | + * {@link ListenerHandle#attach() attach} was called. |
| 110 | + * <p> |
| 111 | + * Because the built handle manages whether the listener is currently attached, the function is only called when |
| 112 | + * necessary, i.e. when {@code attach} is called when the listener is currently not added to the observable. |
| 113 | + * |
| 114 | + * @param add |
| 115 | + * the {@link BiConsumer} called on {@code attach}; the arguments for the function are the observable and |
| 116 | + * listener specified during this builder's construction |
| 117 | + * @return this builder for fluent calls |
| 118 | + */ |
| 119 | + public ListenerHandleBuilder<O, L> onAttach(BiConsumer<? super O, ? super L> add) { |
| 120 | + Objects.requireNonNull(add, "The argument 'add' must not be null."); |
| 121 | + |
| 122 | + this.add = Optional.of(add); |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Sets the function which is executed when the built {@link ListenerHandle} must remove the listener because |
| 128 | + * {@link ListenerHandle#attach() detach} was called. |
| 129 | + * <p> |
| 130 | + * Because the built handle manages whether the listener is currently attached, the function is only called when |
| 131 | + * necessary, i.e. when {@code detach} is called when the listener is currently added to the observable. |
| 132 | + * |
| 133 | + * @param remove |
| 134 | + * the {@link BiConsumer} called on {@code detach}; the arguments for the function are the observable and |
| 135 | + * listener specified during this builder's construction |
| 136 | + * @return this builder for fluent calls |
| 137 | + */ |
| 138 | + public ListenerHandleBuilder<O, L> onDetach(BiConsumer<? super O, ? super L> remove) { |
| 139 | + Objects.requireNonNull(remove, "The argument 'remove' must not be null."); |
| 140 | + |
| 141 | + this.remove = Optional.of(remove); |
| 142 | + return this; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Creates a new listener handle. This will only succeed if {@link #onAttach(BiConsumer)} and |
| 147 | + * {@link #onDetach(BiConsumer)} have been called. |
| 148 | + * |
| 149 | + * @return a new {@link ListenerHandle}; initially detached |
| 150 | + * @throws IllegalStateException |
| 151 | + * if {@link #onAttach(BiConsumer)} or {@link #onDetach(BiConsumer)} have not been called |
| 152 | + */ |
| 153 | + public ListenerHandle build() throws IllegalStateException { |
| 154 | + verifyAddAndRemovePresent(); |
| 155 | + return new GenericListenerHandle<O, L>(observable, listener, add.get(), remove.get()); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Verifies that {@link #add} and {@link #remove} are present. |
| 160 | + * |
| 161 | + * @throws IllegalStateException |
| 162 | + * if {@link #add} or {@link #remove} is empty. |
| 163 | + */ |
| 164 | + private void verifyAddAndRemovePresent() throws IllegalStateException { |
| 165 | + boolean onAttachNotCalled = !add.isPresent(); |
| 166 | + boolean onDetachNotCalled = !remove.isPresent(); |
| 167 | + boolean canBuild = !onAttachNotCalled && !onDetachNotCalled; |
| 168 | + |
| 169 | + if (canBuild) |
| 170 | + return; |
| 171 | + else |
| 172 | + throwExceptionForMissingCall(onAttachNotCalled, onDetachNotCalled); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Throws an {@link IllegalStateException} for a missing call. |
| 177 | + * |
| 178 | + * @param onAttachNotCalled |
| 179 | + * indicates whether {@link #onAttach(BiConsumer)} has been called |
| 180 | + * @param onDetachNotCalled |
| 181 | + * indicates whether {@link #onDetach(BiConsumer)} has been called |
| 182 | + * @throws IllegalStateException |
| 183 | + * if at least one of the specified booleans is true |
| 184 | + */ |
| 185 | + private static void throwExceptionForMissingCall(boolean onAttachNotCalled, boolean onDetachNotCalled) |
| 186 | + throws IllegalStateException { |
| 187 | + |
| 188 | + if (onAttachNotCalled && onDetachNotCalled) |
| 189 | + throw new IllegalStateException( |
| 190 | + "A listener handle can not be build until 'onAttach' and 'onDetach' have been called."); |
| 191 | + |
| 192 | + if (onAttachNotCalled) |
| 193 | + throw new IllegalStateException("A listener handle can not be build until 'onAttach' has been called."); |
| 194 | + |
| 195 | + if (onDetachNotCalled) |
| 196 | + throw new IllegalStateException("A listener handle can not be build until 'onDetach' has been called."); |
| 197 | + } |
| 198 | + |
| 199 | + // #end SET AND BUILD |
| 200 | +} |
0 commit comments