    001package org.cpsolver.ifs.model;
    002
    003/**
    004 * Binary constraint. <br>
    005 * <br>
    006 * Extension of {@link Constraint} that links exactly two variables.
    007 *
    008 * @see Variable
    009 * @see Constraint
    010 * @see Model
    011 *
    012 * @author  Tomáš Müller
    013 * @version IFS 1.3 (Iterative Forward Search)<br>
    014 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    015 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    016 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    017 * <br>
    018 *          This library is free software; you can redistribute it and/or modify
    019 *          it under the terms of the GNU Lesser General Public License as
    020 *          published by the Free Software Foundation; either version 3 of the
    021 *          License, or (at your option) any later version. <br>
    022 * <br>
    023 *          This library is distributed in the hope that it will be useful, but
    024 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    025 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    026 *          Lesser General Public License for more details. <br>
    027 * <br>
    028 *          You should have received a copy of the GNU Lesser General Public
    029 *          License along with this library; if not see
    030 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    031 * @param <V> Variable
    032 * @param <T> Value
    033 */
    034
    035public abstract class BinaryConstraint<V extends Variable<V, T>, T extends Value<V, T>> extends Constraint<V, T> {
    036    private V iFirst = null, iSecond = null;
    037
    038    public BinaryConstraint() {
    039        super();
    040    }
    041
    042    @Override
    043    public void addVariable(V var) {
    044        if (iFirst == null)
    045            iFirst = var;
    046        else
    047            iSecond = var;
    048        super.addVariable(var);
    049    }
    050
    051    /** First variable
    052     * @return first variable of the constraint
    053     **/
    054    public V first() {
    055        return iFirst;
    056    }
    057
    058    /** Second variable
    059     * @return second variable of the constraint
    060     **/
    061    public V second() {
    062        return iSecond;
    063    }
    064
    065    /** True, id the given variable is the first one
    066     * @param variable given variable
    067     * @return true if the given variable is the first variable of the constraint
    068     **/
    069    public boolean isFirst(V variable) {
    070        return variable.equals(first());
    071    }
    072
    073    /**
    074     * Returns the variable out of the constraints variables which is different
    075     * from the given variable.
    076     * @param variable given variable
    077     * @return the other variable of the constraint
    078     */
    079    public V another(V variable) {
    080        return (first() != null && variable.equals(first()) ? second() : first());
    081    }
    082}