wutzebaer :
I try to reuse my constructor like this:
public final long x;
public final long y;
public final int level;
private final int hash;
public final Point tileCenter;
public TileCoordinate(long x, long y, int level) {
this.x = x;
this.y = y;
this.level = level;
this.hash = Objects.hash(x, y, level);
this.tileCenter = getTileCenter();
}
public TileCoordinate(String key) {
String[] bits = key.split("-");
// this.level = Integer.parseInt(bits[0]);
// this.x = Long.parseLong(bits[1]);
// this.y = Long.parseLong(bits[2]);
this(Long.parseInt(bits[0]),Long.parseLong(bits[1]),Integer.parseLong(bits[2]));
this.hash = Objects.hash(x, y, level);
this.tileCenter = getTileCenter();
}
Since I don't want to write this(Integer.parseInt(key.split("-")[0]),Long.parseLong(key.split("-")[1]),Long.parseLong(key.split("-")));
, what are my options?
Karol Dowbecki :
The call to this()
or super()
must be the first statement in the constructor. One way to fix it would be to convert the second constructor into a Factory Method:
public static TileCoordinate parseTitleCoordinate(String key) {
String[] bits = key.split("-");
long x = Long.parseLong(bits[0]);
long y = Long.parseLong(bits[1]);
long level = Long.parseLong(bits[2]);
return new TitleCoordinate(x, y, level);
}
This would allow to keep the fields final
.