package org.darkhelm.dragonchess.server.piece;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.darkhelm.dragonchess.shared.enums.Boards;
import org.darkhelm.dragonchess.shared.enums.Pieces;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;
/**
* Schema fragment(s) for this class:
*
* <pre>
* <xs:complexType xmlns:ns="http://dragonchess.darkhelm.org/xml/schema/piece_data.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" name="piece">
* <xs:sequence>
* <xs:element type="ns:board" name="air"/>
* <xs:element type="ns:board" name="land"/>
* <xs:element type="ns:board" name="under"/>
* </xs:sequence>
* <xs:attribute type="ns:piece-class" use="required" name="name"/>
* <xs:attribute type="xs:string" use="required" name="id"/>
* <xs:attribute type="xs:int" use="required" name="value"/>
* </xs:complexType>
* </pre>
*/
public class PieceDef implements Cloneable {
public static final String XML_PATH = "/xml/pieces/";
public static final String XML_EXT = ".xml";
private static Map<Pieces, PieceDef> pieceMap;
private PieceMoveSet air;
private PieceMoveSet land;
private PieceMoveSet under;
private Pieces name;
private String id;
private int value;
private Pieces promote;
static {
pieceMap = new HashMap<Pieces, PieceDef>();
}
/**
* Get the 'air' element value.
*
* @return value
*/
public PieceMoveSet getAir() {
return air.clone();
}
/**
* Set the 'air' element value.
*
* @param air
*/
public void setAir(PieceMoveSet air) {
this.air = air;
}
/**
* Get the 'land' element value.
*
* @return value
*/
public PieceMoveSet getLand() {
return land.clone();
}
/**
* Set the 'land' element value.
*
* @param land
*/
public void setLand(PieceMoveSet land) {
this.land = land;
}
/**
* Get the 'under' element value.
*
* @return value
*/
public PieceMoveSet getUnder() {
return under.clone();
}
/**
* Set the 'under' element value.
*
* @param under
*/
public void setUnder(PieceMoveSet under) {
this.under = under;
}
/**
* Get the 'name' attribute value.
*
* @return value
*/
public Pieces getName() {
return name;
}
/**
* Set the 'name' attribute value.
*
* @param name
*/
public void setName(Pieces name) {
this.name = name;
}
/**
* Get the 'id' attribute value.
*
* @return value
*/
public String getId() {
return id;
}
/**
* Set the 'id' attribute value.
*
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* Get the 'value' attribute value.
*
* @return value
*/
public int getValue() {
return value;
}
/**
* Set the 'value' attribute value.
*
* @param value
*/
public void setValue(int value) {
this.value = value;
}
public Pieces getPromote() {
return promote;
}
public void setPromote(Pieces promote) {
this.promote = promote;
}
public static PieceDef get(Pieces piece) {
try {
if (!pieceMap.containsKey(piece)) {
URL piecePath = Pieces.class.getResource(XML_PATH
+ piece.name().toLowerCase() + XML_EXT);
IBindingFactory bfact = BindingDirectory
.getFactory(PieceDef.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
PieceDef unmarshalledPiece = (PieceDef) uctx.unmarshalDocument(
new BufferedReader(new InputStreamReader(piecePath
.openStream())), null);
pieceMap.put(piece, unmarshalledPiece);
}
return pieceMap.get(piece).clone();
} catch (JiBXException e) {
return null;
} catch (IOException e) {
return null;
}
}
public PieceMoveSet getMoves(Boards board) {
PieceMoveSet moveList = null;
switch (board) {
case AIR:
moveList = getAir();
break;
case LAND:
moveList = getLand();
break;
case UNDER:
moveList = getUnder();
break;
}
return moveList;
}
public PieceDef clone() {
PieceDef ret = new PieceDef();
ret.setName(getName());
ret.setPromote(getPromote());
ret.setValue(getValue());
ret.setId(getId());
ret.setAir(getAir().clone());
ret.setLand(getLand().clone());
ret.setUnder(getUnder().clone());
return ret;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((air == null) ? 0 : air.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((land == null) ? 0 : land.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((promote == null) ? 0 : promote.hashCode());
result = prime * result + ((under == null) ? 0 : under.hashCode());
result = prime * result + value;
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PieceDef other = (PieceDef) obj;
if (air == null) {
if (other.air != null)
return false;
} else if (!air.equals(other.air))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (land == null) {
if (other.land != null)
return false;
} else if (!land.equals(other.land))
return false;
if (name != other.name)
return false;
if (promote != other.promote)
return false;
if (under == null) {
if (other.under != null)
return false;
} else if (!under.equals(other.under))
return false;
if (value != other.value)
return false;
return true;
}
}