001 /**
002 * GRANITE DATA SERVICES
003 * Copyright (C) 2006-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 package org.granite.client.persistence.collection;
021
022 import java.util.Collection;
023 import java.util.Collections;
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 if (!checkInitializedRead())
037 return 0;
038 return getCollection().size();
039 }
040
041 public boolean isEmpty() {
042 if (!checkInitializedRead())
043 return true;
044 return getCollection().isEmpty();
045 }
046
047 public boolean containsKey(Object key) {
048 if (!checkInitializedRead())
049 return false;
050 return getCollection().containsKey(key);
051 }
052
053 public boolean containsValue(Object value) {
054 if (!checkInitializedRead())
055 return false;
056 return getCollection().containsValue(value);
057 }
058
059 public V get(Object key) {
060 if (!checkInitializedRead())
061 return null;
062 return getCollection().get(key);
063 }
064
065 public V put(K key, V value) {
066 return put(key, value, true);
067 }
068
069 protected V put(K key, V value, boolean checkInitialized) {
070 if (checkInitialized)
071 checkInitializedWrite();
072
073 boolean containsKey = getCollection().containsKey(key);
074 V previousValue = getCollection().put(key, value);
075 if (!containsKey || (previousValue == null ? value != null : !previousValue.equals(value)))
076 dirty();
077 return previousValue;
078 }
079
080 public V remove(Object key) {
081 checkInitializedWrite();
082
083 boolean containsKey = getCollection().containsKey(key);
084 V removedValue = getCollection().remove(key);
085 if (containsKey)
086 dirty();
087 return removedValue;
088 }
089
090 public void putAll(Map<? extends K, ? extends V> m) {
091 checkInitializedWrite();
092
093 for (Map.Entry<? extends K, ? extends V> entry : m.entrySet())
094 put(entry.getKey(), entry.getValue(), false);
095 }
096
097 public void clear() {
098 checkInitializedWrite();
099 if (!getCollection().isEmpty()) {
100 getCollection().clear();
101 dirty();
102 }
103 }
104
105 public Set<K> keySet() {
106 if (!checkInitializedRead())
107 return Collections.emptySet();
108 return new SetProxy<K>(getCollection().keySet());
109 }
110
111 public Collection<V> values() {
112 if (!checkInitializedRead())
113 return Collections.emptySet();
114 return new CollectionProxy<V>(getCollection().values());
115 }
116
117 public Set<Map.Entry<K, V>> entrySet() {
118 if (!checkInitializedRead())
119 return Collections.emptySet();
120 return new SetProxy<Map.Entry<K, V>>(getCollection().entrySet());
121 }
122 }