1
2
3
4 package org.inigma.utopia;
5
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
8
9
10
11
12 public final class Coordinate {
13 public static final Coordinate UNKNOWN = new Coordinate(0, 0);
14 private static final transient Pattern PATTERN = Pattern.compile("\\((\\d+):(\\d+)\\)");
15
16 private int kingdom;
17 private int island;
18
19 public Coordinate() {
20 kingdom = 0;
21 island = 0;
22 }
23
24 public Coordinate(String txt) {
25 Matcher matcher = PATTERN.matcher(txt);
26 if (matcher.find()) {
27 kingdom = Integer.parseInt(matcher.group(1));
28 island = Integer.parseInt(matcher.group(2));
29 } else {
30 kingdom = 0;
31 island = 0;
32 }
33 }
34
35 public Coordinate(int k, int i) {
36 this.kingdom = k;
37 this.island = i;
38 }
39
40 public int getKingdom() {
41 return kingdom;
42 }
43
44 public int getIsland() {
45 return island;
46 }
47
48 @Override
49 public boolean equals(Object obj) {
50 if (obj instanceof Coordinate) {
51 Coordinate c = (Coordinate) obj;
52 return (kingdom == c.kingdom) && (island == c.island);
53 }
54 return false;
55 }
56
57 @Override
58 public int hashCode() {
59 return kingdom ^ island;
60 }
61
62 @Override
63 public String toString() {
64 StringBuilder sb = new StringBuilder();
65 sb.append("(").append(kingdom);
66 sb.append(":").append(island);
67 sb.append(")");
68 return sb.toString();
69 }
70
71 public void setIsland(int island) {
72 this.island = island;
73 }
74
75 public void setKingdom(int kingdom) {
76 this.kingdom = kingdom;
77 }
78 }