/** Auteur : Samuele Giraudo.
 *  Creation : 30/08/10
 *  Modifications : 31/08/10 01/09/10 16/10/10 */

public class Arc {

    // CONSTANTES.
    public static final int ORIENTE = 0x01;
    public static final int PONDERE = 0x10;


    // ATTRIBUTS.
    private int masque_espece;
    private Sommet depart;
    private Sommet arrivee;
    private double poids;


    // CONSTRUCTEURS.

    public Arc(int masque_espece, Sommet depart, Sommet arrivee, double poids) {
        assert masque_espece == 0x00 || masque_espece == 0x10 || masque_espece == 0x01 || masque_espece == 0x11;
        assert depart != null;
        assert arrivee != null;
        /** Arc ponderes positivement. */
        assert ((masque_espece & Arc.PONDERE) != 0) || poids >= 0;
        /** Boucles interdites dans le cas non oriente. */
        assert ((masque_espece & Arc.ORIENTE) != 0) || depart != arrivee;

        this.masque_espece = masque_espece;
        this.depart = depart;
        this.arrivee = arrivee;
        if (this.estPondere())
            this.poids = poids;
        else
            this.poids = -1;
    }


    // METHODES.

    public int masqueEspece() {
        return this.masque_espece;
    }

    public boolean estOriente() {
        return (this.masque_espece & Arc.ORIENTE) != 0;
    }

    public boolean estPondere() {
        return (this.masque_espece & Arc.PONDERE) != 0;
    }

    public Sommet depart() {
        return this.depart;
    }

    public Sommet arrivee() {
        return this.arrivee;
    }

    public double poids() {
        return this.poids;
    }

    public boolean equals(Object obj) {
        assert obj != null;

        if (!(obj instanceof Arc))
            return false;
        Arc a = (Arc) obj;
        if (a.masqueEspece() != this.masqueEspece())
            return false;
        else {
            boolean egaux_sommets = true;
            boolean egaux_poids = true;
            if (this.estOriente())
                egaux_sommets = a.depart().equals(this.depart()) && a.arrivee().equals(this.arrivee());
            else
                egaux_sommets = (a.depart().equals(this.depart()) && a.arrivee().equals(this.arrivee())) ||
                                (a.depart().equals(this.arrivee()) && a.arrivee().equals(this.depart()));
            if (this.estPondere())
                egaux_poids = a.poids() == this.poids();
            return egaux_sommets && egaux_poids;
        }
    }

    public String toString() {
        String chaine = "";
        if (this.estOriente())
            chaine += this.depart().toString() + "->" + this.arrivee().toString();
        else
            chaine += this.depart().toString() + "--"+ this.arrivee().toString();
        if (this.estPondere())
            return chaine + " [label = \"" + this.poids() + "\"]";
        return chaine;
    }

}
