package org.scotlandyard.impl.engine.boardmap;
import org.scotlandyard.engine.GameException;
import org.scotlandyard.engine.boardmap.BoardMap;
import org.scotlandyard.engine.boardmap.Coordinate;
import org.scotlandyard.engine.boardmap.Link;
import org.scotlandyard.engine.constants.TransportationMethod;
/**
* implementation of link object
*
* @author Hussain Al-Mutawa
* @version 1.0
* @since Sun Sep 18 13:28:08 NZST 2011
*/
public class LinkImpl implements Link {
private transient final Coordinate coordinateA,coordinateB;
private transient final BoardMap boardMap;
private transient final TransportationMethod transportation;
/**
* Initialises a link segment on the board map
*
* @param boardMap the board map object which hold the link and the positions
* @param transportationMethod the method of transportation that can be used on the link
* @param coordinateA coordinate point on the map
* @param coordinateB coordinate point on the map
* @throws GameException if any of the coordinate board map is not the same
* as the link board map, or if any of the coordinates is null, throws an
* GameException also if the boardMap is null or the transportation method is not specified
*/
public LinkImpl(
final BoardMap boardMap,
final TransportationMethod transportation,
final Coordinate coordinateA,
final Coordinate coordinateB) throws GameException{
super();
if(boardMap==null){
throw new GameException("BoardMap must be specified");
}
if(transportation==null){
throw new GameException("TransportationMethod must be specified");
}
if(coordinateA==null){
throw new GameException("coordinateA must be specified");
}
if(coordinateB==null){
throw new GameException("coordinateB must be specified");
}
if(!coordinateA.getBoardMap().equals(coordinateB.getBoardMap())){
// System.out.println(coordinateA.getBoardMap());
// System.out.println(coordinateB.getBoardMap());
throw new GameException("coordinateA and coordinateB must be from the same Board Map ");
}
if(!boardMap.equals(coordinateB.getBoardMap())){
throw new GameException("coordinateA and coordinateB Board Map has to be the same as the link BoardMap");
}
this.boardMap=boardMap;
this.transportation = transportation;
if(coordinateA.compareTo(coordinateB)<0){
this.coordinateA=coordinateA;
this.coordinateB=coordinateB;
}else{
this.coordinateB=coordinateA;
this.coordinateA=coordinateB;
}
}
@Override
public boolean equals(final Object other) {
return toString().equals(other.toString());
}
@Override
public BoardMap getBoardMap() {
return boardMap;
}
@Override
public Coordinate getCoordinateA() {
return coordinateA;
}
@Override
public Coordinate getCoordinateB() {
return coordinateB;
}
@Override
public Coordinate getOtherEnd(final Coordinate coordinate) {
Coordinate temp = null;
if(coordinate.equals(coordinateA)) {
temp = coordinateB;
} else if(coordinate.equals(coordinateB)){
temp = coordinateA;
}
return temp;
}
@Override
public TransportationMethod getTransportationMethod() {
return transportation;
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public String toString() {
return boardMap.toString()+"\n\tLink : ("+getTransportationMethod()+")\n\t\t" +
getCoordinateA() +" , " + getCoordinateB();
}
}