001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2013 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.client.persistence.collection;
022    
023    import java.util.Collection;
024    import java.util.Map;
025    import java.util.Set;
026    
027    /**
028     * @author Franck WOLFF
029     */
030    public abstract class AbstractPersistentMapCollection<K, V, C extends Map<K, V>> extends AbstractPersistentCollection<C> implements Map<K, V> {
031    
032            public AbstractPersistentMapCollection() {
033            }
034    
035            public int size() {
036                    checkInitialized();
037                    return getCollection().size();
038            }
039    
040            public boolean isEmpty() {
041                    checkInitialized();
042                    return getCollection().isEmpty();
043            }
044    
045            public boolean containsKey(Object key) {
046                    checkInitialized();
047                    return getCollection().containsKey(key);
048            }
049    
050            public boolean containsValue(Object value) {
051                    checkInitialized();
052                    return getCollection().containsValue(value);
053            }
054    
055            public V get(Object key) {
056                    checkInitialized();
057                    return getCollection().get(key);
058            }
059    
060            public V put(K key, V value) {
061                    return put(key, value, true);
062            }
063            
064            protected V put(K key, V value, boolean checkInitialized) {
065                    if (checkInitialized)
066                            checkInitialized();
067                    
068                    boolean containsKey = getCollection().containsKey(key);
069                    V previousValue = getCollection().put(key, value);
070                    if (!containsKey || (previousValue == null ? value != null : !previousValue.equals(value)))
071                            dirty();
072                    return previousValue;
073            }
074    
075            public V remove(Object key) {
076                    checkInitialized();
077                    
078                    boolean containsKey = getCollection().containsKey(key);
079                    V removedValue = getCollection().remove(key);
080                    if (containsKey)
081                            dirty();
082                    return removedValue;
083            }
084    
085            public void putAll(Map<? extends K, ? extends V> m) {
086                    checkInitialized();
087                    
088                    for (Map.Entry<? extends K, ? extends V> entry : m.entrySet())
089                            put(entry.getKey(), entry.getValue(), false);
090            }
091    
092            public void clear() {
093                    checkInitialized();
094                    if (!getCollection().isEmpty()) {
095                            getCollection().clear();
096                            dirty();
097                    }
098            }
099    
100            public Set<K> keySet() {
101                    checkInitialized();
102                    return new SetProxy<K>(this, getCollection().keySet());
103            }
104    
105            public Collection<V> values() {
106                    checkInitialized();
107                    return new CollectionProxy<V>(this, getCollection().values());
108            }
109    
110            public Set<Map.Entry<K, V>> entrySet() {
111                    checkInitialized();
112                    return new SetProxy<Map.Entry<K, V>>(this, getCollection().entrySet());
113            }
114    }