summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/replica2/CoordinatorID.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/derms/replica2/CoordinatorID.java')
-rw-r--r--src/main/java/derms/replica2/CoordinatorID.java39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/main/java/derms/replica2/CoordinatorID.java b/src/main/java/derms/replica2/CoordinatorID.java
new file mode 100644
index 0000000..523ebcf
--- /dev/null
+++ b/src/main/java/derms/replica2/CoordinatorID.java
@@ -0,0 +1,39 @@
+package derms.replica2;
+
+import java.io.Serializable;
+
+class CoordinatorID implements Serializable {
+ String city;
+ short num;
+
+ CoordinatorID(String city, short num) {
+ this.city = city;
+ this.num = num;
+ }
+
+ static CoordinatorID parse(String str) throws IllegalArgumentException {
+ if (str.length() != City.codeLen+ID.nDigits)
+ throw new IllegalArgumentException("illegal coordinator ID: " + str);
+
+ try {
+ String city = str.substring(0, City.codeLen);
+ short num = Short.parseShort(str.substring(City.codeLen));
+ return new CoordinatorID(city, num);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("illegal coordinator ID '" + str + "': " + e.getMessage());
+ }
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj.getClass() != this.getClass())
+ return false;
+ CoordinatorID other = (CoordinatorID) obj;
+ return other.city.equals(this.city) && other.num == this.num;
+ }
+
+ @Override
+ public String toString() {
+ return city+"C"+num;
+ }
+}