View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.sys.util;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  /**
30   * A Map which wraps another Map, presumably one which is read-only (such as org.kuali.kfs.sys.util.ReflectionMap).  This Map holds a secondary Map which can be written to and read from; but if, in get, the key
31   * is not present in the internally maintained Map, then the get falls back to the wrapped read-only Map to find the value
32   */
33  public class FallbackMap<K, V> implements Map<K, V> {
34      protected Map<K, V> frontMap;
35      protected Map<K, V> backMap;
36  
37      public FallbackMap(Map<K,V> backMap) {
38          frontMap = new HashMap<>();
39          this.backMap = backMap;
40      }
41  
42      @Override
43      public int size() {
44          return frontMap.size() + backMap.size();
45      }
46  
47      @Override
48      public boolean isEmpty() {
49          return frontMap.isEmpty() && backMap.isEmpty();
50      }
51  
52      @Override
53      public boolean containsKey(Object key) {
54          if (frontMap.containsKey(key)) {
55              return true;
56          }
57          return backMap.containsKey(key);
58      }
59  
60      @Override
61      public boolean containsValue(Object value) {
62          if (frontMap.containsValue(value)) {
63              return true;
64          }
65          return backMap.containsValue(value);
66      }
67  
68      @Override
69      public V get(Object key) {
70          if (frontMap.containsKey(key)) {
71              return frontMap.get(key);
72          }
73          return backMap.get(key);
74      }
75  
76      @Override
77      public V put(K key, V value) {
78          return frontMap.put(key, value);
79      }
80  
81      @Override
82      public V remove(Object key) {
83          if (frontMap.containsKey(key)) {
84              return frontMap.remove(key);
85          }
86          return backMap.remove(key);
87      }
88  
89      @Override
90      public void putAll(Map<? extends K, ? extends V> m) {
91          frontMap.putAll(m);
92      }
93  
94      @Override
95      public void clear() {
96          frontMap.clear();
97          backMap.clear();
98      }
99  
100     @Override
101     public Set<K> keySet() {
102         Set<K> keys = new HashSet<>();
103         keys.addAll(frontMap.keySet());
104         keys.addAll(backMap.keySet());
105         return keys;
106     }
107 
108     @Override
109     public Collection<V> values() {
110         List<V> values = new ArrayList<>();
111         values.addAll(frontMap.values());
112         values.addAll(backMap.values());
113         return values;
114     }
115 
116     @Override
117     public Set<java.util.Map.Entry<K, V>> entrySet() {
118         Set<java.util.Map.Entry<K, V>> entries = new HashSet<>();
119         entries.addAll(frontMap.entrySet());
120         entries.addAll(backMap.entrySet());
121         return entries;
122     }
123 
124 }