Skip to content

Instantly share code, notes, and snippets.

@volgar1x
Created November 30, 2013 12:30
Show Gist options
  • Save volgar1x/7718463 to your computer and use it in GitHub Desktop.
Save volgar1x/7718463 to your computer and use it in GitHub Desktop.
package org.photon.staticdata;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.NumericNode;
import java.io.IOException;
import java.util.List;
@JsonIdentityInfo(property = "id", generator = ObjectIdGenerators.PropertyGenerator.class, scope = MapData.class)
public class MapData {
private int id;
private Position pos;
private int width;
private int height;
private String key;
private String date;
private boolean premium;
@JsonManagedReference
private List<Cell> cells;
public MapData() {
}
public MapData(int id, Position pos, int width, int height, String key, String date, boolean premium, List<Cell> cells) {
this.id = id;
this.pos = pos;
this.width = width;
this.height = height;
this.key = key;
this.date = date;
this.premium = premium;
this.cells = cells;
}
@JsonSerialize(using = PositionSerializer.class)
@JsonDeserialize(using = PositionDeserializer.class)
public static class Position {
private int x, y;
public Position() {
}
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void setX(int x) {
this.x = x;
}
}
public static class Cell {
private short id;
@JsonBackReference
private MapData map;
private boolean los;
private int groundLevel;
private int groundSlope;
private MovementType movementType;
/**
* nullable
*/
private Integer interactiveObjectId;
/**
* nullable
*/
private Trigger trigger;
/**
* nullable
*/
private Integer startCellTeamId;
public Cell() {
}
public Cell(short id, MapData map, boolean los, int groundLevel, int groundSlope, MovementType movementType, Integer interactiveObjectId, Trigger trigger, Integer startCellTeamId) {
this.id = id;
this.map = map;
this.los = los;
this.groundLevel = groundLevel;
this.groundSlope = groundSlope;
this.movementType = movementType;
this.interactiveObjectId = interactiveObjectId;
this.trigger = trigger;
this.startCellTeamId = startCellTeamId;
}
public short getId() {
return id;
}
public MapData getMap() {
return map;
}
public boolean isLos() {
return los;
}
public int getGroundLevel() {
return groundLevel;
}
public int getGroundSlope() {
return groundSlope;
}
public MovementType getMovementType() {
return movementType;
}
public Integer getInteractiveObjectId() {
return interactiveObjectId;
}
public Trigger getTrigger() {
return trigger;
}
public Integer getStartCellTeamId() {
return startCellTeamId;
}
public void setId(short id) {
this.id = id;
}
public void setMap(MapData map) {
this.map = map;
}
public void setLos(boolean los) {
this.los = los;
}
public void setGroundLevel(int groundLevel) {
this.groundLevel = groundLevel;
}
public void setGroundSlope(int groundSlope) {
this.groundSlope = groundSlope;
}
public void setMovementType(MovementType movementType) {
this.movementType = movementType;
}
public void setInteractiveObjectId(Integer interactiveObjectId) {
this.interactiveObjectId = interactiveObjectId;
}
public void setTrigger(Trigger trigger) {
this.trigger = trigger;
}
public void setStartCellTeamId(Integer startCellTeamId) {
this.startCellTeamId = startCellTeamId;
}
}
@JsonSerialize(using = MovementTypeSerializer.class)
@JsonDeserialize(using = MovementTypeDeserializer.class)
public static enum MovementType {
Unwalkable(1),
Door(2),
Trigger(3),
Walkable(4),
Paddock(5),
Road(6);
public int value;
private MovementType(int value) {
this.value = value;
}
public static MovementType valueOf(int value) {
for (MovementType v : values()) {
if (v.value == value) {
return v;
}
}
return null;
}
}
public static class Trigger {
private MapData origin;
private Cell originCell;
private MapData next;
private Cell nextCell;
public Trigger() {
}
public Trigger(MapData origin, Cell originCell, MapData next, Cell nextCell) {
this.origin = origin;
this.originCell = originCell;
this.next = next;
this.nextCell = nextCell;
}
public MapData getOrigin() {
return origin;
}
public Cell getOriginCell() {
return originCell;
}
public MapData getNext() {
return next;
}
public Cell getNextCell() {
return nextCell;
}
public void setOrigin(MapData origin) {
this.origin = origin;
}
public void setOriginCell(Cell originCell) {
this.originCell = originCell;
}
public void setNext(MapData next) {
this.next = next;
}
public void setNextCell(Cell nextCell) {
this.nextCell = nextCell;
}
}
public int getId() {
return id;
}
public Position getPos() {
return pos;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public String getKey() {
return key;
}
public String getDate() {
return date;
}
public boolean isPremium() {
return premium;
}
public List<Cell> getCells() {
return cells;
}
public void setId(int id) {
this.id = id;
}
public void setPos(Position pos) {
this.pos = pos;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public void setKey(String key) {
this.key = key;
}
public void setDate(String date) {
this.date = date;
}
public void setPremium(boolean premium) {
this.premium = premium;
}
public void setCells(List<Cell> cells) {
this.cells = cells;
}
public static class PositionSerializer extends JsonSerializer<Position> {
@Override
public void serialize(Position value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeStartArray();
jgen.writeNumber(value.x);
jgen.writeNumber(value.y);
jgen.writeEndArray();
}
}
public static class PositionDeserializer extends JsonDeserializer<Position> {
@Override
public Position deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
ArrayNode node = jp.getCodec().readTree(jp);
if (node.size() != 2) {
throw new IllegalStateException("a Position object should be represented as 2-fixed-length array");
}
JsonNode x = node.get(0),
y = node.get(1);
if (!x.canConvertToInt() || !y.canConvertToInt()) {
throw new IllegalStateException("a Position should only contains numbers");
}
return new Position(x.asInt(), y.asInt());
}
}
public static class MovementTypeSerializer extends JsonSerializer<MovementType> {
@Override
public void serialize(MovementType value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeNumber(value.value);
}
}
public static class MovementTypeDeserializer extends JsonDeserializer<MovementType> {
@Override
public MovementType deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
NumericNode node = jp.getCodec().readTree(jp);
if (!node.canConvertToInt()) {
throw new IllegalStateException("a MovementType should be represented as a number");
}
return MovementType.valueOf(node.asInt());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment