root / src / gr / grnet / pithos / web / client / catalog / UserCatalogs.java @ 1bb330dc
History | View | Annotate | Download (1.8 kB)
1 |
package gr.grnet.pithos.web.client.catalog; |
---|---|
2 |
|
3 |
import gr.grnet.pithos.web.client.Helpers; |
4 |
|
5 |
import java.util.HashMap; |
6 |
import java.util.Iterator; |
7 |
import java.util.Map; |
8 |
|
9 |
/**
|
10 |
* Holds one-to-one mappings from user IDs to respective user display names.
|
11 |
*
|
12 |
* The iterator returns map entries with id as the key and name as the value.
|
13 |
*/
|
14 |
public class UserCatalogs implements Iterable<Map.Entry<String, String>>{ |
15 |
private HashMap<String, String> id2name = new HashMap<String, String>(); |
16 |
private HashMap<String, String> name2id = new HashMap<String, String>(); |
17 |
|
18 |
public UserCatalogs() {
|
19 |
this(new HashMap<String, String>(), new HashMap<String, String>()); |
20 |
} |
21 |
|
22 |
private UserCatalogs(HashMap<String, String> id2name, HashMap<String, String> name2id) { |
23 |
this.id2name = id2name;
|
24 |
this.name2id = name2id;
|
25 |
} |
26 |
|
27 |
public UserCatalogs updateWithIDAndName(String id, String name) { |
28 |
this.id2name.put(id, name);
|
29 |
this.name2id.put(name, id);
|
30 |
|
31 |
return this; |
32 |
} |
33 |
|
34 |
public UserCatalogs updateFrom(UserCatalogs other) {
|
35 |
for(Map.Entry<String, String> otherIDAndName : other) { |
36 |
this.updateWithIDAndName(otherIDAndName.getKey(), otherIDAndName.getValue());
|
37 |
} |
38 |
return this; |
39 |
} |
40 |
|
41 |
public UserCatalogs copy() {
|
42 |
return new UserCatalogs( |
43 |
Helpers.copyHashMap(id2name), |
44 |
Helpers.copyHashMap(name2id) |
45 |
); |
46 |
} |
47 |
|
48 |
public boolean hasID(String id) { |
49 |
return id2name.containsKey(id);
|
50 |
} |
51 |
|
52 |
public boolean hasName(String email) { |
53 |
return name2id.containsKey(email);
|
54 |
} |
55 |
|
56 |
public String getDisplayName(String id) { |
57 |
return id2name.get(id);
|
58 |
} |
59 |
|
60 |
@Override
|
61 |
public Iterator<Map.Entry<String, String>> iterator() { |
62 |
return id2name.entrySet().iterator();
|
63 |
} |
64 |
} |