summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/replica2/ResourceID.java
blob: 82ca2daa1fbeef1de394edc88dec357e3ae9775f (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
44
45
46
47
48
49
50
51
package derms.replica2;

import derms.City;

import java.io.Serializable;

class ResourceID implements Serializable {
  String city;
  short num;

  ResourceID (String city, short num) {
    this.city = city;
    this.num = num;
  }

  ResourceID() {
    this("XXX", (short) 1111);
  }

  static ResourceID parse(String s) throws IllegalArgumentException {
    if (s.length() != City.codeLen+ID.nDigits) {
      throw new IllegalArgumentException("invalid resource ID: "+s);
    }
    try {
      String cityCode = s.substring(0, City.codeLen);
      short num = Short.parseShort(s.substring(City.codeLen));
      return new ResourceID(cityCode, num);
    } catch (NumberFormatException e) {
      throw new IllegalArgumentException("invalid resource ID: "+e.getMessage());
    }
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null || obj.getClass() != this.getClass()) {
      return false;
    }
    ResourceID other = (ResourceID) obj;
    return (this.city.equals(other.city)) && (this.num == other.num);
  }

  @Override
  public int hashCode() {
    return city.hashCode() * num;
  }

  @Override
  public String toString() {
    return city+num;
  }
}