    001    package ifs.model;
    002
    003    /**
    004     * Binary constraint.
    005     * <br><br>
    006     * Extension of {@link Constraint} that links exactly two variables.
    007     *
    008     * @see Variable
    009     * @see Constraint
    010     * @see Model
    011     *
    012     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
    013     * @version 1.0
    014     */
    015
    016    public abstract class BinaryConstraint extends Constraint {
    017        private Variable iFirst = null, iSecond = null;
    018        public BinaryConstraint() {
    019            super();
    020        }
    021
    022        public void addVariable(Variable var) {
    023            if (iFirst == null)
    024                iFirst = (Variable)var;
    025            else
    026                iSecond = (Variable)var;
    027            super.addVariable(var);
    028        }
    029
    030        /** First variable */
    031        public Variable first() {
    032            return iFirst;
    033        }
    034
    035        /** Second variable */
    036        public Variable second() {
    037            return iSecond;
    038        }
    039
    040        /** True, id the given variable is the first one */
    041        public boolean isFirst(Variable variable) {
    042            return variable.equals(first());
    043        }
    044
    045        /** Returns the variable out of the constraints variables which is different from the given variable.*/
    046        public Variable another(Variable variable) {
    047            return (
    048                    first() != null && variable.equals(first()) ? second() : first());
    049        }
    050    }