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 public PieceCount(int count) {
31 known = true;
32 this.count = count;
33 }
34
35 private PieceCount() {
36 known = false;
37 }
38
39 public static PieceCount createUnknownCount() {
40 return new PieceCount();
41 }
42
43
44 public PieceCount add(PieceCount other) {
45 if (!this.known || !other.known) {
46 known = false;
47 } else {
48 count += other.count;
49 }
50 return this;
51 }
52
53 public PieceCount multiply(int value) {
54 count *= value;
55 return this;
56 }
57
58 public int getCount() {
59 if (known) {
60 return count;
61 } else {
62 throw new IllegalStateException(
63 "Can't get superstep count when it's unknown");
64 }
65 }
66
67 public boolean isKnown() {
68 return known;
69 }
70
71 @Override
72 public boolean equals(Object obj) {
73 if (this == obj) {
74 return true;
75 }
76 if (obj instanceof PieceCount) {
77 PieceCount other = (PieceCount) obj;
78 if (known) {
79 return other.known && other.count == count;
80 } else {
81 return !other.known;
82 }
83 }
84 return false;
85 }
86
87 @Override
88 public int hashCode() {
89 return Objects.hashCode(known, count);
90 }
91 }