|
| 1 | +package org.codefx.libfx.nesting; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | +import java.util.function.BiConsumer; |
| 5 | +import java.util.function.Consumer; |
| 6 | + |
| 7 | +import javafx.beans.Observable; |
| 8 | + |
| 9 | +/** |
| 10 | + * Usability class which observes a {@link Nesting} and executes some methods when the nesting's |
| 11 | + * {@link Nesting#innerObservableProperty() innerObservable} changes (in that order): |
| 12 | + * <ol> |
| 13 | + * <li>if the old inner observable was present, a method is called with that observable as its argument; the method is |
| 14 | + * specified during building (see {@link NestingObserverBuilder#withOldInnerObservable(Consumer) withOldInnerObservable}) |
| 15 | + * <li>if the new inner observable is present, a method is called with that observable as its argument; the method is |
| 16 | + * specified during building (see {@link NestingObserverBuilder#withNewInnerObservable(Consumer) withNewInnerObservable}) |
| 17 | + * <li>in every case, another method is called with two Booleans as its arguments which indicate whether the old and the |
| 18 | + * new observable were/are present; the method is specified during building (see |
| 19 | + * {@link NestingObserverBuilder#whenInnerObservableChanges(BiConsumer) whenInnerObservableChanges}) |
| 20 | + * </ol> |
| 21 | + * These methods are also called once after construction. At this point, there is of course no old inner observable |
| 22 | + * present. |
| 23 | + * <p> |
| 24 | + * The observer is created with a {@link NestingObserverBuilder} which can be obtained from |
| 25 | + * {@link NestingObserver#observe(Nesting) observe}. After setting some of the methods mentioned above, the observer is |
| 26 | + * built by calling {@link NestingObserverBuilder#build()}. |
| 27 | + * |
| 28 | + * @param <O> |
| 29 | + * the type of the nesting hierarchy's innermost {@link Observable} |
| 30 | + */ |
| 31 | +public final class NestingObserver<O extends Observable> { |
| 32 | + |
| 33 | + // #region PROPERTIES |
| 34 | + |
| 35 | + /** |
| 36 | + * The observed {@link Nesting}. |
| 37 | + */ |
| 38 | + private final Nesting<? extends O> nesting; |
| 39 | + |
| 40 | + /** |
| 41 | + * Called when the inner observable changes and an old inner observable was present. That observable is also the |
| 42 | + * argument. |
| 43 | + */ |
| 44 | + private final Consumer<? super O> oldInnerObservableConsumer; |
| 45 | + |
| 46 | + /** |
| 47 | + * Called when the inner observable changes and the new inner observable is present. That observable is also the |
| 48 | + * argument. |
| 49 | + */ |
| 50 | + private final Consumer<? super O> newInnerObservableConsumer; |
| 51 | + |
| 52 | + /** |
| 53 | + * Called when the inner observable changes. The arguments are two Booleans indicating whether the old and new inner |
| 54 | + * observables are present. |
| 55 | + */ |
| 56 | + private final BiConsumer<Boolean, Boolean> innerObservableChanges; |
| 57 | + |
| 58 | + //#end PROPERTIES |
| 59 | + |
| 60 | + // #region CONSTRUCTION |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates a new {@link NestingObserver} from the specified {@link NestingObserverBuilder builder}. |
| 64 | + * |
| 65 | + * @param builder |
| 66 | + * the {@link NestingObserverBuilder} from which the observer is created |
| 67 | + */ |
| 68 | + private NestingObserver(NestingObserverBuilder<O> builder) { |
| 69 | + nesting = builder.nesting; |
| 70 | + oldInnerObservableConsumer = builder.oldInnerObservableConsumer; |
| 71 | + newInnerObservableConsumer = builder.newInnerObservableConsumer; |
| 72 | + innerObservableChanges = builder.innerObservableChanges; |
| 73 | + |
| 74 | + initializeObserver(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Starts building a new {@link NestingObserver} which observes the specified nesting. |
| 79 | + * |
| 80 | + * @param <O> |
| 81 | + * the type of the nesting hierarchy's innermost {@link Observable} |
| 82 | + * @param nesting |
| 83 | + * the observed {@link Nesting} |
| 84 | + * @return a new {@link NestingObserverBuilder} |
| 85 | + */ |
| 86 | + public static <O extends Observable> NestingObserverBuilder<O> observe(Nesting<O> nesting) { |
| 87 | + return new NestingObserverBuilder<O>(nesting); |
| 88 | + } |
| 89 | + |
| 90 | + //#end CONSTRUCTION |
| 91 | + |
| 92 | + // #region OBSERVE |
| 93 | + |
| 94 | + /** |
| 95 | + * Initializes the observer by observing the initial status and any changes made to it. |
| 96 | + */ |
| 97 | + private void initializeObserver() { |
| 98 | + // observe the initial status |
| 99 | + observeInnerObservableChange(Optional.empty(), nesting.innerObservableProperty().getValue()); |
| 100 | + |
| 101 | + // add a listener to the nesting which observes changes |
| 102 | + nesting.innerObservableProperty().addListener( |
| 103 | + (o, oldInnerObservable, newInnerObservable) |
| 104 | + -> observeInnerObservableChange(oldInnerObservable, newInnerObservable)); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Calls {@link #oldInnerObservableConsumer}, {@link #newInnerObservableConsumer} and |
| 109 | + * {@link #innerObservableChanges} when the inner observable changes. |
| 110 | + * |
| 111 | + * @param oldInnerObservable |
| 112 | + * the old {@link Nesting#innerObservableProperty() innerObservable} |
| 113 | + * @param newInnerObservable |
| 114 | + * the new {@link Nesting#innerObservableProperty() innerObservable} |
| 115 | + */ |
| 116 | + private void observeInnerObservableChange( |
| 117 | + Optional<? extends O> oldInnerObservable, Optional<? extends O> newInnerObservable) { |
| 118 | + |
| 119 | + oldInnerObservable.ifPresent(oldObservable -> oldInnerObservableConsumer.accept(oldObservable)); |
| 120 | + newInnerObservable.ifPresent(newObservable -> newInnerObservableConsumer.accept(newObservable)); |
| 121 | + |
| 122 | + boolean oldInnerObservablePresent = oldInnerObservable.isPresent(); |
| 123 | + boolean newInnerObservablePresent = newInnerObservable.isPresent(); |
| 124 | + innerObservableChanges.accept(oldInnerObservablePresent, newInnerObservablePresent); |
| 125 | + } |
| 126 | + |
| 127 | + //#end BIND |
| 128 | + |
| 129 | + // #region INNER CLASSES |
| 130 | + |
| 131 | + /** |
| 132 | + * Builds a {@link NestingObserver}. |
| 133 | + * |
| 134 | + * @param <O> |
| 135 | + * the type of the nesting hierarchy's innermost {@link Observable} |
| 136 | + */ |
| 137 | + public static class NestingObserverBuilder<O extends Observable> { |
| 138 | + |
| 139 | + /** |
| 140 | + * The future value for {@link NestingObserver#nesting}. |
| 141 | + */ |
| 142 | + private final Nesting<? extends O> nesting; |
| 143 | + |
| 144 | + /** |
| 145 | + * The future value for {@link NestingObserver#oldInnerObservableConsumer}. |
| 146 | + */ |
| 147 | + private Consumer<? super O> oldInnerObservableConsumer; |
| 148 | + |
| 149 | + /** |
| 150 | + * The future value for {@link NestingObserver#newInnerObservableConsumer}. |
| 151 | + */ |
| 152 | + private Consumer<? super O> newInnerObservableConsumer; |
| 153 | + |
| 154 | + /** |
| 155 | + * The future value for {@link NestingObserver#innerObservableChanges}. |
| 156 | + */ |
| 157 | + private BiConsumer<Boolean, Boolean> innerObservableChanges; |
| 158 | + |
| 159 | + /** |
| 160 | + * Creates a new builder for a nesting observer which observes the specified nesting. |
| 161 | + * |
| 162 | + * @param nesting |
| 163 | + * the nesting which will be observed by the created {@link NestingObserver} |
| 164 | + */ |
| 165 | + private NestingObserverBuilder(Nesting<? extends O> nesting) { |
| 166 | + this.nesting = nesting; |
| 167 | + oldInnerObservableConsumer = observable -> {/* by default do nothing */}; |
| 168 | + newInnerObservableConsumer = observable -> {/* by default do nothing */}; |
| 169 | + innerObservableChanges = (oldObservablePresent, newObservablePresent) -> {/* by default do nothing */}; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * The specified method will be executed when the {@link Nesting#innerObservableProperty() innerObservable} |
| 174 | + * changes <b>and</b> the old observable was present. |
| 175 | + * |
| 176 | + * @param oldInnerObservableConsumer |
| 177 | + * the executed method; its argument is the old inner observable |
| 178 | + * @return this builder for fluent calls |
| 179 | + */ |
| 180 | + public NestingObserverBuilder<O> withOldInnerObservable(Consumer<? super O> oldInnerObservableConsumer) { |
| 181 | + this.oldInnerObservableConsumer = oldInnerObservableConsumer; |
| 182 | + return this; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * The specified method will be executed when the {@link Nesting#innerObservableProperty() innerObservable} |
| 187 | + * changes <b>and</b> the new observable is present. |
| 188 | + * |
| 189 | + * @param newInnerObservableConsumer |
| 190 | + * the executed method; its argument is the new inner observable |
| 191 | + * @return this builder for fluent calls |
| 192 | + */ |
| 193 | + public NestingObserverBuilder<O> withNewInnerObservable(Consumer<? super O> newInnerObservableConsumer) { |
| 194 | + this.newInnerObservableConsumer = newInnerObservableConsumer; |
| 195 | + return this; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * The specified method will be executed when the {@link Nesting#innerObservableProperty() innerObservable} |
| 200 | + * changes. |
| 201 | + * |
| 202 | + * @param innerObservableChanges |
| 203 | + * the executed method; its argument are two Booleans indicating whether the old and new inner |
| 204 | + * observables are present. |
| 205 | + * @return this builder for fluent calls |
| 206 | + */ |
| 207 | + public NestingObserverBuilder<O> whenInnerObservableChanges(BiConsumer<Boolean, Boolean> innerObservableChanges) { |
| 208 | + this.innerObservableChanges = innerObservableChanges; |
| 209 | + return this; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Builds a observer from this builder's settings. |
| 214 | + * |
| 215 | + * @return a new {@link NestingObserver} |
| 216 | + */ |
| 217 | + public NestingObserver<O> build() { |
| 218 | + return new NestingObserver<O>(this); |
| 219 | + } |
| 220 | + |
| 221 | + } |
| 222 | + |
| 223 | + //#end region |
| 224 | + |
| 225 | +} |
0 commit comments