1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.block_app.framework.block; |
20 | |
|
21 | |
import com.google.common.base.Objects; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class PieceCount { |
27 | |
private boolean known; |
28 | |
private int count; |
29 | |
|
30 | 0 | public PieceCount(int count) { |
31 | 0 | known = true; |
32 | 0 | this.count = count; |
33 | 0 | } |
34 | |
|
35 | 0 | private PieceCount() { |
36 | 0 | known = false; |
37 | 0 | } |
38 | |
|
39 | |
public static PieceCount createUnknownCount() { |
40 | 0 | return new PieceCount(); |
41 | |
} |
42 | |
|
43 | |
|
44 | |
public PieceCount add(PieceCount other) { |
45 | 0 | if (!this.known || !other.known) { |
46 | 0 | known = false; |
47 | |
} else { |
48 | 0 | count += other.count; |
49 | |
} |
50 | 0 | return this; |
51 | |
} |
52 | |
|
53 | |
public PieceCount multiply(int value) { |
54 | 0 | count *= value; |
55 | 0 | return this; |
56 | |
} |
57 | |
|
58 | |
public int getCount() { |
59 | 0 | if (known) { |
60 | 0 | return count; |
61 | |
} else { |
62 | 0 | throw new IllegalStateException( |
63 | |
"Can't get superstep count when it's unknown"); |
64 | |
} |
65 | |
} |
66 | |
|
67 | |
public boolean isKnown() { |
68 | 0 | return known; |
69 | |
} |
70 | |
|
71 | |
@Override |
72 | |
public boolean equals(Object obj) { |
73 | 0 | if (this == obj) { |
74 | 0 | return true; |
75 | |
} |
76 | 0 | if (obj instanceof PieceCount) { |
77 | 0 | PieceCount other = (PieceCount) obj; |
78 | 0 | if (known) { |
79 | 0 | return other.known && other.count == count; |
80 | |
} else { |
81 | 0 | return !other.known; |
82 | |
} |
83 | |
} |
84 | 0 | return false; |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
public int hashCode() { |
89 | 0 | return Objects.hashCode(known, count); |
90 | |
} |
91 | |
} |