    001package org.cpsolver.ifs.assignment.context;
    002
    003import org.cpsolver.ifs.assignment.Assignment;
    004import org.cpsolver.ifs.model.Model;
    005import org.cpsolver.ifs.model.Value;
    006import org.cpsolver.ifs.model.Variable;
    007
    008/**
    009 * A reference to an assignment context. A reference is created by a class requiring an assignment
    010 * context {@link HasAssignmentContext} by calling {@link Model#createReference(HasAssignmentContext)}.
    011 * This reference can be stored with the class implementing the {@link HasAssignmentContext}
    012 * interface in place of the assignment context {@link AssignmentContext}. Assignment context
    013 * can be than retrieved from an assignment by calling {@link Assignment#getAssignmentContext(AssignmentContextReference)}.
    014 * This method also create a new context if there is none associated with the assignment yet for
    015 * the given reference.
    016 *
    017 * <br><br>
    018 *
    019 * Method {@link AssignmentContextReference#getIndex()} can be used to index stored contexts.
    020 * A new assignment context can be created for a reference by calling {@link HasAssignmentContext#createAssignmentContext(Assignment)}
    021 * on the {@link AssignmentContextReference#getParent()}.
    022 *
    023 * @see AssignmentContext
    024 * @see AssignmentContextReference
    025 * @see HasAssignmentContext
    026 *
    027 * @author  Tomáš Müller
    028 * @version IFS 1.3 (Iterative Forward Search)<br>
    029 *          Copyright (C) 2014 Tomáš Müller<br>
    030 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    031 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    032 * <br>
    033 *          This library is free software; you can redistribute it and/or modify
    034 *          it under the terms of the GNU Lesser General Public License as
    035 *          published by the Free Software Foundation; either version 3 of the
    036 *          License, or (at your option) any later version. <br>
    037 * <br>
    038 *          This library is distributed in the hope that it will be useful, but
    039 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    040 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    041 *          Lesser General Public License for more details. <br>
    042 * <br>
    043 *          You should have received a copy of the GNU Lesser General Public
    044 *          License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>.
    045 * @param <V> Variable
    046 * @param <T> Value
    047 * @param <C> Assignment Context
    048 **/
    049public class AssignmentContextReference <V extends Variable<V, T>, T extends Value<V, T>, C extends AssignmentContext> {
    050    private HasAssignmentContext<V, T, C> iParent;
    051    private int iIndex;
    052
    053    /**
    054     * Create a reference context
    055     * @param parent a class implementing the {@link HasAssignmentContext} interface
    056     * @param index a unique index associated with the reference by the {@link Model}
    057     **/
    058    public AssignmentContextReference(HasAssignmentContext<V, T, C> parent, int index) {
    059        iParent = parent;
    060        iIndex = index;
    061    }
    062
    063    /**
    064     * Return index, a unique number associated with the reference by the {@link Model}.
    065     * @return an index
    066     */
    067    public int getIndex() {
    068        return iIndex;
    069    }
    070
    071    @Override
    072    public int hashCode() {
    073        return iIndex;
    074    }
    075
    076    @Override
    077    public boolean equals(Object o) {
    078        if (o == null || !(o instanceof AssignmentContextReference<?,?,?>))
    079            return false;
    080        return getIndex() == ((AssignmentContextReference<?,?,?>)o).getIndex();
    081    }
    082
    083    /**
    084     * Return parent class, i.e., the class implementing the {@link HasAssignmentContext} interface.
    085     * @return parent
    086     */
    087    public HasAssignmentContext<V,T,C> getParent() {
    088        return iParent;
    089    }
    090}