001 /*
002 * Copyright 2008-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package griffon.javafx.collections;
017
018 import javafx.collections.MapChangeListener;
019 import javafx.collections.ObservableMap;
020 import javafx.collections.WeakMapChangeListener;
021
022 import javax.annotation.Nonnull;
023 import java.util.Collection;
024 import java.util.Map;
025 import java.util.Set;
026
027 import static java.util.Objects.requireNonNull;
028
029 /**
030 * @author Andres Almiray
031 * @since 2.9.0
032 */
033 public abstract class DelegatingObservableMap<K, V> extends ObservableMapBase<K, V> implements ObservableMap<K, V> {
034 private final ObservableMap<K, V> delegate;
035 private MapChangeListener<K, V> sourceListener;
036
037 public DelegatingObservableMap(@Nonnull ObservableMap<K, V> delegate) {
038 this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null");
039 this.delegate.addListener(new WeakMapChangeListener<>(getListener()));
040 }
041
042 @Nonnull
043 protected ObservableMap<K, V> getDelegate() {
044 return delegate;
045 }
046
047 private MapChangeListener<K, V> getListener() {
048 if (sourceListener == null) {
049 sourceListener = DelegatingObservableMap.this::sourceChanged;
050 }
051 return sourceListener;
052 }
053
054 protected abstract void sourceChanged(@Nonnull MapChangeListener.Change<? extends K, ? extends V> c);
055
056 // --== Delegate methods ==--
057
058 @Override
059 public int size() {
060 return getDelegate().size();
061 }
062
063 @Override
064 public boolean isEmpty() {
065 return getDelegate().isEmpty();
066 }
067
068 @Override
069 public boolean containsValue(Object value) {
070 return getDelegate().containsValue(value);
071 }
072
073 @Override
074 public boolean containsKey(Object key) {
075 return getDelegate().containsKey(key);
076 }
077
078 @Override
079 public V get(Object key) {
080 return getDelegate().get(key);
081 }
082
083 @Override
084 public V put(K key, V value) {
085 return getDelegate().put(key, value);
086 }
087
088 @Override
089 public V remove(Object key) {
090 return getDelegate().remove(key);
091 }
092
093 @Override
094 public void putAll(Map<? extends K, ? extends V> m) {
095 getDelegate().putAll(m);
096 }
097
098 @Override
099 public void clear() {
100 getDelegate().clear();
101 }
102
103 @Override
104 public Set<K> keySet() {
105 return getDelegate().keySet();
106 }
107
108 @Override
109 public Collection<V> values() {
110 return getDelegate().values();
111 }
112
113 @Override
114 public Set<Entry<K, V>> entrySet() {
115 return getDelegate().entrySet();
116 }
117
118 @Override
119 public boolean equals(Object o) {
120 return getDelegate().equals(o);
121 }
122
123 @Override
124 public int hashCode() {
125 return getDelegate().hashCode();
126 }
127
128 @Override
129 public String toString() {
130 return getDelegate().toString();
131 }
132 }
|