View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.framework;
35  
36  
37  import java.io.Serializable;
38  import java.util.ArrayList;
39  import java.util.List;
40  import java.util.Objects;
41  import java.util.Optional;
42  import java.util.function.Consumer;
43  import java.util.function.UnaryOperator;
44  
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  import io.reactivex.BackpressureStrategy;
49  import io.reactivex.Flowable;
50  import io.reactivex.disposables.Disposable;
51  import io.reactivex.subjects.BehaviorSubject;
52  import io.reactivex.subjects.Subject;
53  
54  /**
55   * Observable property. Provides two flavours of observation:
56   * <ul>
57   * <li>null-safe, which yields optionals to the observers</li>
58   * <li>nullable, which may broadcast nulls</li>
59   * </ul>
60   * <p>
61   * Also provides the mutation capabilities in imperative and functional styles. Imperative is
62   * useful when we merely want to swap one value with another, whereas the functional one can be
63   * used when e.g. another element needs to be added to a collection.
64   * </p>
65   * @param <T> item type.
66   */
67  public interface ContextProperty<T> extends Serializable {
68  
69      Logger log = LoggerFactory.getLogger(ContextProperty.class);
70  
71      Disposable observeNullable(Consumer<T> action);
72  
73      Disposable observe(Consumer<Optional<T>> action);
74  
75      Optional<T> value();
76  
77      void interceptWith(UnaryOperator<T> valueTransformer);
78  
79      default void set(T value) {
80          set(value, false);
81      }
82  
83      void set(T value, boolean shouldNotifyOnSameItem);
84  
85      void update(UnaryOperator<T> updateOperation);
86  
87      void update(Consumer<T> updateOperation);
88  
89      default T nullableValue() {
90          return value().orElse(null);
91      }
92  
93      /**
94       * A wrapper around {@link ContextProperty}.
95       * @param <T> item type.
96       */
97      class Wrapper<T> implements ContextProperty<T> {
98  
99          private final ContextProperty<T> delegate;
100 
101         public Wrapper(ContextProperty<T> delegate) {
102             this.delegate = delegate;
103         }
104 
105         @Override
106         public Disposable observeNullable(Consumer<T> action) {
107             return this.delegate.observeNullable(action);
108         }
109 
110         @Override
111         public Disposable observe(Consumer<Optional<T>> action) {
112             return this.delegate.observe(action);
113         }
114 
115         @Override
116         public Optional<T> value() {
117             return this.delegate.value();
118         }
119 
120         @Override
121         public void interceptWith(UnaryOperator<T> valueTransformer) {
122             this.delegate.interceptWith(valueTransformer);
123         }
124 
125         @Override
126         public void set(T value, boolean shouldNotifyOnSameItem) {
127             this.delegate.set(value, shouldNotifyOnSameItem);
128         }
129 
130         @Override
131         public void update(UnaryOperator<T> updateOperation) {
132             this.delegate.update(updateOperation);
133         }
134 
135         @Override
136         public void update(Consumer<T> updateOperation) {
137             this.delegate.update(updateOperation);
138         }
139     }
140 
141     /**
142      * Default implementation of {@link ContextProperty}.
143      *
144      * @param <T>
145      *     property value type
146      */
147     class Impl<T> implements ContextProperty<T> {
148 
149         private static final Logger log = LoggerFactory.getLogger(Impl.class);
150 
151         private T lastValue = null;
152 
153         private T pendingValueUpdate = null;
154 
155         private Subject<Optional<T>> subject = BehaviorSubject.createDefault(Optional.empty());
156 
157         private boolean updatesMuted;
158 
159         private List<UnaryOperator<T>> valueTransformers = new ArrayList<>();
160 
161         Impl() {
162             subject.onNext(Optional.empty());
163         }
164 
165         public Disposable observeNullable(Consumer<T> action) {
166             return asFlowable()
167                     .subscribe(
168                             optional -> {
169                                 muteUpdates();
170                                 try {
171                                     action.accept(optional.orElse(null));
172                                 } finally {
173                                     unmuteUpdates();
174                                 }
175                             },
176                             e -> log.error("Failed to dispatch context property change: {}", e.getMessage(), e));
177         }
178 
179         public Flowable<Optional<T>> asFlowable() {
180             return subject.toFlowable(BackpressureStrategy.LATEST).map(optionalValue -> Optional.ofNullable(applyValueTransformers(optionalValue.orElse(null))));
181         }
182 
183         public T applyValueTransformers(T value) {
184             return valueTransformers.stream().reduce(
185                     value,
186                     (current, mapper) -> mapper.apply(current),
187                     (f, s) -> s);
188         }
189 
190         @Override
191         public Disposable observe(Consumer<Optional<T>> action) {
192             return asFlowable().subscribe(value -> {
193                 muteUpdates();
194                 try {
195                     action.accept(value);
196                 } finally {
197                     unmuteUpdates();
198                 }
199             }, e -> log.error("Failed to dispatch context property change: {}", e.getMessage(), e));
200 
201         }
202 
203         @Override
204         public void update(UnaryOperator<T> updateOperation) {
205             value().ifPresent(value -> {
206                 try {
207                     doSet(updateOperation.apply(value), true);
208                 } catch (Exception e) {
209                     log.error("Failed to update context property value", e);
210                 }
211             });
212         }
213 
214         @Override
215         public void update(Consumer<T> updateOperation) {
216             update(value -> {
217                     updateOperation.accept(value);
218                     return value;
219             });
220         }
221 
222         @Override
223         public Optional<T> value() {
224             return Optional.ofNullable(applyValueTransformers(this.lastValue));
225         }
226 
227         @Override
228         public void interceptWith(UnaryOperator<T> valueTransformer) {
229             this.valueTransformers.add(valueTransformer);
230         }
231 
232         @Override
233         public void set(T value, boolean shouldNotifyOnSameItem) {
234             doSet(value, shouldNotifyOnSameItem);
235         }
236 
237         public void doSet(T value, boolean shouldNotifyOnSameItem) {
238             if (updatesMuted) {
239                 pendingValueUpdate = value;
240                 return;
241             }
242 
243             if (!shouldNotifyOnSameItem && Objects.equals(value, this.lastValue)) {
244                 return;
245             }
246 
247             this.lastValue = value;
248             muteUpdates();
249             try {
250                 this.subject.onNext(Optional.ofNullable(value));
251             } finally {
252                unmuteUpdates();
253             }
254         }
255 
256         private void muteUpdates() {
257             this.updatesMuted = true;
258         }
259 
260         private void unmuteUpdates() {
261             if (pendingValueUpdate != null) {
262                 doSet(pendingValueUpdate, false);
263                 pendingValueUpdate = null;
264             }
265 
266             this.updatesMuted = false;
267         }
268     }
269 }