UIThreadAwareInvalidationListener.java
01 /*
02  * Copyright 2008-2017 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package griffon.javafx.beans.binding;
17 
18 import javafx.beans.InvalidationListener;
19 import javafx.beans.Observable;
20 
21 import javax.annotation.Nonnull;
22 
23 import static java.util.Objects.requireNonNull;
24 import static javafx.application.Platform.isFxApplicationThread;
25 import static javafx.application.Platform.runLater;
26 
27 /**
28  @author Andres Almiray
29  @since 2.9.0
30  */
31 class UIThreadAwareInvalidationListener implements InvalidationListener, UIThreadAware {
32     private final InvalidationListener delegate;
33 
34     UIThreadAwareInvalidationListener(@Nonnull InvalidationListener delegate) {
35         this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null");
36     }
37 
38     @Override
39     public void invalidated(final Observable observable) {
40         if (isFxApplicationThread()) {
41             delegate.invalidated(observable);
42         else {
43             runLater(() -> invalidated(observable));
44         }
45     }
46 
47     @Override
48     public boolean equals(Object o) {
49         return this == o || delegate.equals(o);
50     }
51 
52     @Override
53     public int hashCode() {
54         return delegate.hashCode();
55     }
56 
57     @Override
58     public String toString() {
59         return getClass().getName() ":" + delegate.toString();
60     }
61 }