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 * @version IFS 1.3 (Iterative Forward Search)<br>
028 *          Copyright (C) 2014 Tomáš Müller<br>
029 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
030 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
031 * <br>
032 *          This library is free software; you can redistribute it and/or modify
033 *          it under the terms of the GNU Lesser General Public License as
034 *          published by the Free Software Foundation; either version 3 of the
035 *          License, or (at your option) any later version. <br>
036 * <br>
037 *          This library is distributed in the hope that it will be useful, but
038 *          WITHOUT ANY WARRANTY; without even the implied warranty of
039 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
040 *          Lesser General Public License for more details. <br>
041 * <br>
042 *          You should have received a copy of the GNU Lesser General Public
043 *          License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>.
044 * @param <V> Variable
045 * @param <T> Value
046 * @param <C> Assignment Context
047 **/
048public class AssignmentContextReference <V extends Variable<V, T>, T extends Value<V, T>, C extends AssignmentContext> {
049    private HasAssignmentContext<V, T, C> iParent;
050    private int iIndex;
051
052    /**
053     * Create a reference context
054     * @param parent a class implementing the {@link HasAssignmentContext} interface
055     * @param index a unique index associated with the reference by the {@link Model}
056     **/
057    public AssignmentContextReference(HasAssignmentContext<V, T, C> parent, int index) {
058        iParent = parent;
059        iIndex = index;
060    }
061    
062    /**
063     * Return index, a unique number associated with the reference by the {@link Model}.
064     * @return an index
065     */
066    public int getIndex() {
067        return iIndex;
068    }
069    
070    @Override
071    public int hashCode() {
072        return iIndex;
073    }
074    
075    @Override
076    public boolean equals(Object o) {
077        if (o == null || !(o instanceof AssignmentContextReference<?,?,?>))
078            return false;
079        return getIndex() == ((AssignmentContextReference<?,?,?>)o).getIndex();
080    }
081    
082    /**
083     * Return parent class, i.e., the class implementing the {@link HasAssignmentContext} interface.
084     * @return parent
085     */
086    public HasAssignmentContext<V,T,C> getParent() {
087        return iParent;
088    }
089}