AbstractObservable.java
01 /*
02  * Copyright 2008-2016 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 org.codehaus.griffon.runtime.core;
17 
18 import griffon.core.Observable;
19 
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.beans.PropertyChangeSupport;
25 
26 import static griffon.util.GriffonNameUtils.requireNonBlank;
27 import static java.util.Objects.requireNonNull;
28 
29 /**
30  @author Andres Almiray
31  @since 2.0.0
32  */
33 public abstract class AbstractObservable implements Observable {
34     protected final PropertyChangeSupport pcs;
35 
36     public AbstractObservable() {
37         pcs = new PropertyChangeSupport(this);
38     }
39 
40     public void addPropertyChangeListener(@Nullable PropertyChangeListener listener) {
41         pcs.addPropertyChangeListener(listener);
42     }
43 
44     public void addPropertyChangeListener(@Nullable String propertyName, @Nullable PropertyChangeListener listener) {
45         pcs.addPropertyChangeListener(propertyName, listener);
46     }
47 
48     public void removePropertyChangeListener(@Nullable PropertyChangeListener listener) {
49         pcs.removePropertyChangeListener(listener);
50     }
51 
52     public void removePropertyChangeListener(@Nullable String propertyName, @Nullable PropertyChangeListener listener) {
53         pcs.removePropertyChangeListener(propertyName, listener);
54     }
55 
56     @Nonnull
57     public PropertyChangeListener[] getPropertyChangeListeners() {
58         return pcs.getPropertyChangeListeners();
59     }
60 
61     @Nonnull
62     public PropertyChangeListener[] getPropertyChangeListeners(@Nullable String propertyName) {
63         return pcs.getPropertyChangeListeners(propertyName);
64     }
65 
66     protected void firePropertyChange(@Nonnull PropertyChangeEvent event) {
67         pcs.firePropertyChange(requireNonNull(event, "Argument 'event' must not be null"));
68     }
69 
70     protected void firePropertyChange(@Nonnull String propertyName, @Nullable Object oldValue, @Nullable Object newValue) {
71         pcs.firePropertyChange(requireNonBlank(propertyName, "Argument 'propertyName' must not be blank"), oldValue, newValue);
72     }
73 }