-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupsRpg.java
92 lines (77 loc) · 2.54 KB
/
GroupsRpg.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package lab2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class GroupsRpg {
public Hashtable<String, Hashtable <String, Integer>> groups = new Hashtable<String, Hashtable <String, Integer>> ();
// maintain a vector for each group
private Hashtable<String, VectorClock> clockGroup = new Hashtable<String, VectorClock> ();
public GroupsRpg(){}
public GroupsRpg(Object groups, String localName){
ArrayList<Object> groupList = (ArrayList<Object>) groups;
for(Object group : groupList){
String groupName = null;
ArrayList<String> groupMem = new ArrayList<String>();
Map<String, Object> groupInfo = (Map<String, Object>) group;
for (Map.Entry<String, Object> entry : groupInfo.entrySet()){
Object value = entry.getValue();
String key = entry.getKey();
if("name".equals(key)){
groupName = (String) value;
}
else if("members".equals(key)){
groupMem = (ArrayList<String>) value;
}
else{
System.out.println("ERROR IN GROUP FILE");
}
Hashtable <String, Integer> Rpg = new Hashtable<String, Integer>();
for(String tmp : groupMem){
Rpg.put(tmp, -1);
}
this.groups.put(groupName, Rpg);
// Make clocks for each group and put it into hashtable
VectorClock clockForGroup = new VectorClock(groupMem, localName);
this.clockGroup.put(groupName, clockForGroup);
}
}
}
public GroupsRpg deepCopy(){
GroupsRpg copy = new GroupsRpg();
for(String key : this.groups.keySet()){
copy.groups.put(key, new Hashtable<String, Integer> (this.groups.get(key)));
}
return copy;
}
public Hashtable <String, Integer> getGroupByName(String groupName){
return this.groups.get(groupName);
}
public Hashtable<String, VectorClock> getClockGroup() {
return clockGroup;
}
// @Override
// public String toString() {
// String toString = "{\n";
// for (Entry<String, Hashtable<String, Integer>> entry : this.groups.entrySet()) {
// Hashtable<String, Integer> tmp = entry.getValue();
// toString += entry.getKey() + ":";
// Object [] arrMem = tmp.keySet().toArray();
// Arrays.sort(arrMem);
// for(int i = 0; i < arrMem.length; i++){
// toString += "[" + arrMem[i] + ": " + entry.getValue().get(arrMem[i]) + "] ";
// }
// //toString += tmp.keySet().toString();
// toString += "\n";
// }
// toString += "}\n";
// return toString;
// }
@Override
public String toString() {
return clockGroup.toString();
}
}