View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.datastructure;
22  
23  import java.util.*;
24  /***
25   * A map that has it's map values changes tracked. It uses an inner map to keep the unchanged value and itself to track the changes
26   * @author TiongHiang Lee (thlee@onemindsoft.org)
27   * @version $Id: TrackedMap.java,v 1.2 2004/08/26 12:33:16 thlee Exp $ $Name:  $
28   */
29  public class TrackedMap extends HashMap
30  {
31  
32      /*** the track inner map * */
33      private Map _tracked;
34  
35      /***
36       * {@inheritDoc}
37       */
38      public TrackedMap()
39      {
40          this(new HashMap());
41      }
42  
43      /***
44       * {@inheritDoc}
45       */
46      public TrackedMap(int initialCapacity)
47      {
48          super(initialCapacity);
49      }
50  
51      /***
52       * {@inheritDoc}
53       */
54      public TrackedMap(int initialCapacity, float loadFactor)
55      {
56          super(initialCapacity, loadFactor);
57      }
58  
59      /***
60       * {@inheritDoc}
61       */
62      public TrackedMap(Map map)
63      {
64          super();
65          _tracked = map;
66      }
67  
68      /***
69       * Return whether this map has been changes
70       * @return true if it has been changed
71       */
72      public boolean hasChanges()
73      {
74          return getChangedKeySet().size() > 0;
75      }
76  
77      /***
78       * Return the key set of changed values
79       * @return the key set
80       */
81      public Set getChangedKeySet()
82      {
83          return super.keySet();
84      }
85  
86      /***
87       * {@inheritDoc}
88       */
89      public Object get(Object key)
90      {
91          if (containsKey(key))
92          {
93              return super.get(key);
94          } else
95          {
96              return _tracked.get(key);
97          }
98      }
99  
100     /***
101      * Make this map as up-to-date.
102      */
103     public void makeUpToDate()
104     {
105         Iterator it = super.keySet().iterator();
106         while (it.hasNext())
107         {
108             Object key = it.next();
109             Object o = super.get(key);
110             _tracked.put(key, o);
111         }
112         super.clear();
113     }
114 
115     /***
116      * Clear all the changes
117      */
118     public void clearChanges()
119     {
120         super.clear();
121     }
122 
123     /***
124      * {@inheritDoc}
125      */
126     public void clear()
127     {
128         super.clear();
129         _tracked.clear();
130     }
131 
132     /***
133      * {@inheritDoc}
134      */
135     public boolean containsKey(Object key)
136     {
137         return super.containsKey(key) || _tracked.containsKey(key);
138     }
139 
140     /***
141      * {@inheritDoc}
142      */
143     public boolean containsValue(Object value)
144     {
145         return super.containsValue(value) || _tracked.containsValue(value);
146     }
147 
148     /***
149      * {@inheritDoc}
150      */
151     public Set entrySet()
152     {
153         Set s = new HashSet(_tracked.entrySet());
154         s.addAll(super.entrySet());
155         return s;
156     }
157 
158     /***
159      * {@inheritDoc}
160      */
161     public boolean isEmpty()
162     {
163         return super.isEmpty() && _tracked.isEmpty();
164     }
165 
166     /***
167      * {@inheritDoc}
168      */
169     public Set keySet()
170     {
171         Set s = new HashSet(_tracked.keySet());
172         s.addAll(super.keySet());
173         return s;
174     }
175 
176     /***
177      * {@inheritDoc}
178      */
179     public Object remove(Object key)
180     {
181         Object o = get(key);
182         put(key, null);
183         return o;
184     }
185 
186     /***
187      * {@inheritDoc}
188      */
189     public int size()
190     {
191         return keySet().size();
192     }
193 
194     /***
195      * {@inheritDoc}
196      */
197     public Collection values()
198     {
199         HashSet set = new HashSet(_tracked.values());
200         set.addAll(super.values());
201         return set;
202     }
203 }