summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/replica2/CoordinatorID.java
blob: a4667964b03a1a0f3fe7fd19e84de493264b2d35 (plain) (blame)
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
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;
  }

  CoordinatorID() {
    this("XXX", (short) 0);
  }

  static CoordinatorID parse(String str) throws IllegalArgumentException {
    if (str.length() != City.codeLen+1+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+1));
      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;
  }
}