• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package autotest.common.table;
2 
3 import com.google.gwt.json.client.JSONObject;
4 
5 import java.util.AbstractSet;
6 import java.util.Collection;
7 import java.util.HashMap;
8 import java.util.Iterator;
9 import java.util.Map;
10 
11 /**
12  * Set that hashes JSONObjects by their ID, so that identical objects get
13  * matched together.
14  */
15 public class JSONObjectSet<T extends JSONObject> extends AbstractSet<T> {
16     protected Map<String, T> backingMap = new HashMap<String, T>();
17 
JSONObjectSet()18     public JSONObjectSet() {
19         super();
20     }
21 
JSONObjectSet(Collection<T> items)22     public JSONObjectSet(Collection<T> items) {
23         super();
24         addAll(items);
25     }
26 
getKey(Object obj)27     protected String getKey(Object obj) {
28         return ((JSONObject) obj).get("id").toString();
29     }
30 
31     @Override
add(T arg0)32     public boolean add(T arg0) {
33         return backingMap.put(getKey(arg0), arg0) == null;
34     }
35 
36     @Override
iterator()37     public Iterator<T> iterator() {
38         return backingMap.values().iterator();
39     }
40 
41     @Override
remove(Object arg0)42     public boolean remove(Object arg0) {
43         return backingMap.remove(getKey(arg0)) != null;
44     }
45 
46     @Override
contains(Object o)47     public boolean contains(Object o) {
48         return backingMap.containsKey(getKey(o));
49     }
50 
51     @Override
size()52     public int size() {
53         return backingMap.size();
54     }
55 }
56