package com.uca.flights;

import com.uca.data_validation.IdentifierValidator;

/**
 * Represent a city identifier
 * @Author Cesano Ugo, Colmerauer Clément
 * 
 */
public class CityId implements Comparable<CityId> //Pas besoinde clone car non modifiable
{
	/**City id value*/
	private final String value;

	/**
     * Constructor
     * @param value the city id value
     * @throws IllegalArgumentException on null or empty parameter
     */
	public CityId(String value)
	{
		if(!IdentifierValidator.isCityIdValid(value))
		{
			throw new IllegalArgumentException("Invalid value.");
		}

		this.value = value;
	}

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

	/**
     * Getter
     * @return city id length
     */
	public int length()
	{
		return this.value.length();
	}

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

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

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

		CityId o = (CityId)other;
		return this.value == o.getValue();
	}

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

}