package com.uca.flights;

import com.uca.data_validation.IdentifierValidator;

/**
 * Represent an airport identifier, a 3 letter code
 * @Author Cesano Ugo, Colmerauer Clément
 * 
 */
public class AirportId implements Comparable<AirportId>
{
	/**Airport id value*/
	private final String value;

	/**
     * Constructor
     * @param value the airport id value
     * @throws IllegalArgumentException on null or wrong length parameter
     */
	public AirportId(String value)
	{
		if(!IdentifierValidator.isAirportIdValid(value))
		{
			throw new IllegalArgumentException("Invalid value.");
		}
		else
		{
			this.value = value;
		}
	}

	/**
     * Getter
     * @return airport id value
     */
	public String getValue()
	{
		return this.value;
	}

	/**
     * Getter
     * @return airport length (always equals 3)
     */
	public int length()
	{
		return 3;
	}

	/**
     * Redefinition of toString
     * @return the value of the airport id
     */
	@Override 
	public String toString()
	{
		return this.value;
	}

	/**
     * Redefinition of compareTo
     * @param other the airportId to compare to
     * @return 0 if other has the same value, 1 else
     * @throws IllegalArgumentException on null parameter
     */
	@Override
	public int compareTo(AirportId other)
	{
		if(other == null)
		{
			throw new IllegalArgumentException("Airport id can't be null");
		}

		if(this.value == other.getValue())
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}

	/**
     * Redefinition of equals
     * @param other the airportId to compare to
     * @return true if other has the same value, false else
     * @throws IllegalArgumentException on null or not AirportId parameter
     */
	@Override
	public boolean equals(Object other)
	{
		if(other == null)
		{
			throw new IllegalArgumentException("AirportId can't be null.");
		}
		if(!(other instanceof AirportId))
		{
			throw new IllegalArgumentException("Must be compared with airport id.");			
		}
		
		AirportId o = (AirportId)other;
		return this.value == o.getValue();
	}

	/**
     * Redefinition of hashCode
     * @return the hashcode
     */
	@Override
	public int hashCode()
	{
		return this.value == null ? 0 : this.value.length();
	}

}