AbstractObservable.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.codehaus.griffon.runtime.core;
19 
20 import griffon.core.Observable;
21 
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import java.beans.PropertyChangeEvent;
25 import java.beans.PropertyChangeListener;
26 import java.beans.PropertyChangeSupport;
27 
28 import static griffon.util.GriffonNameUtils.requireNonBlank;
29 import static java.util.Objects.requireNonNull;
30 
31 /**
32  @author Andres Almiray
33  @since 2.0.0
34  */
35 public abstract class AbstractObservable implements Observable {
36     protected final PropertyChangeSupport pcs;
37 
38     public AbstractObservable() {
39         pcs = new PropertyChangeSupport(this);
40     }
41 
42     public void addPropertyChangeListener(@Nullable PropertyChangeListener listener) {
43         pcs.addPropertyChangeListener(listener);
44     }
45 
46     public void addPropertyChangeListener(@Nullable String propertyName, @Nullable PropertyChangeListener listener) {
47         pcs.addPropertyChangeListener(propertyName, listener);
48     }
49 
50     public void removePropertyChangeListener(@Nullable PropertyChangeListener listener) {
51         pcs.removePropertyChangeListener(listener);
52     }
53 
54     public void removePropertyChangeListener(@Nullable String propertyName, @Nullable PropertyChangeListener listener) {
55         pcs.removePropertyChangeListener(propertyName, listener);
56     }
57 
58     @Nonnull
59     public PropertyChangeListener[] getPropertyChangeListeners() {
60         return pcs.getPropertyChangeListeners();
61     }
62 
63     @Nonnull
64     public PropertyChangeListener[] getPropertyChangeListeners(@Nullable String propertyName) {
65         return pcs.getPropertyChangeListeners(propertyName);
66     }
67 
68     protected void firePropertyChange(@Nonnull PropertyChangeEvent event) {
69         pcs.firePropertyChange(requireNonNull(event, "Argument 'event' must not be null"));
70     }
71 
72     protected void firePropertyChange(@Nonnull String propertyName, @Nullable Object oldValue, @Nullable Object newValue) {
73         pcs.firePropertyChange(requireNonBlank(propertyName, "Argument 'propertyName' must not be blank"), oldValue, newValue);
74     }
75 }