Stuff
[iotcloud.git] / version2 / src / java / iotcloud / ArbitrationRound.java
1 package iotcloud;
2
3 import java.util.Set;
4 import java.util.HashSet;
5
6 import java.util.List;
7 import java.util.ArrayList;
8
9
10 class ArbitrationRound {
11
12     public static final int MAX_PARTS = 10;
13
14     Set<Abort> abortsBefore = null;
15     List<Entry> parts = null;
16     Commit commit = null;
17     int currentSize = 0;
18     boolean didSendPart = false;
19     boolean didGenerateParts = false;
20
21     public ArbitrationRound(Commit _commit, Set<Abort> _abortsBefore) {
22
23         parts = new ArrayList<Entry>();
24
25         commit = _commit;
26         abortsBefore = _abortsBefore;
27
28
29         if (commit != null) {
30             commit.createCommitParts();
31             currentSize += commit.getNumberOfParts();
32         }
33
34         currentSize += abortsBefore.size();
35     }
36
37     public void generateParts() {
38         if (didGenerateParts) {
39             return;
40         }
41         parts = new ArrayList<Entry>(abortsBefore);
42         if (commit != null) {
43             parts.addAll(commit.getParts().values());
44         }
45     }
46
47
48     public List<Entry> getParts() {
49         return parts;
50     }
51
52     public void removeParts(List<Entry> removeParts) {
53         parts.removeAll(removeParts);
54         didSendPart = true;
55     }
56
57     public boolean isDoneSending() {
58         if ((commit == null) && abortsBefore.isEmpty()) {
59             return true;
60         }
61
62         return parts.isEmpty();
63     }
64
65     public Commit getCommit() {
66         return commit;
67     }
68
69     public void setCommit(Commit _commit) {
70         if (commit != null) {
71             currentSize -= commit.getNumberOfParts();
72         }
73         commit = _commit;
74
75         if (commit != null) {
76             currentSize += commit.getNumberOfParts();
77         }
78     }
79
80     public void addAbort(Abort abort) {
81         abortsBefore.add(abort);
82         currentSize++;
83     }
84
85     public void addAborts(Set<Abort> aborts) {
86         abortsBefore.addAll(aborts);
87         currentSize += aborts.size();
88     }
89
90
91     public Set<Abort> getAborts() {
92         return abortsBefore;
93     }
94
95     public int getAbortsCount() {
96         return abortsBefore.size();
97     }
98
99     public int getCurrentSize() {
100         return currentSize;
101     }
102
103     public boolean isFull() {
104         return currentSize >= MAX_PARTS;
105     }
106
107     public boolean didSendPart() {
108         return didSendPart;
109     }
110 }