    001package org.cpsolver.ifs.assignment.context;
    002
    003import org.cpsolver.ifs.assignment.Assignment;
    004import org.cpsolver.ifs.assignment.DefaultSingleAssignment;
    005import org.cpsolver.ifs.model.Value;
    006import org.cpsolver.ifs.model.Variable;
    007
    008/**
    009 * A simple assignment context holder implementation used by the {@link DefaultSingleAssignment} class.
    010 * {@link CanHoldContext} are used when possible, storing contexts in arrays of length 1 (one context per
    011 * {@link HasAssignmentContext} class).
    012 *
    013 * @see AssignmentContext
    014 * @see AssignmentContextReference
    015 * @see AssignmentContextHolder
    016 *
    017 * @version IFS 1.3 (Iterative Forward Search)<br>
    018 *          Copyright (C) 2014 Tomáš Müller<br>
    019 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    020 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    021 * <br>
    022 *          This library is free software; you can redistribute it and/or modify
    023 *          it under the terms of the GNU Lesser General Public License as
    024 *          published by the Free Software Foundation; either version 3 of the
    025 *          License, or (at your option) any later version. <br>
    026 * <br>
    027 *          This library is distributed in the hope that it will be useful, but
    028 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    029 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    030 *          Lesser General Public License for more details. <br>
    031 * <br>
    032 *          You should have received a copy of the GNU Lesser General Public
    033 *          License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>.
    034 * @param <V> Variable
    035 * @param <T> Value
    036 **/
    037public class DefaultSingleAssignmentContextHolder<V extends Variable<V, T>, T extends Value<V, T>> extends AssignmentContextHolderMap<V, T> {
    038
    039    public DefaultSingleAssignmentContextHolder() {
    040    }
    041
    042    @Override
    043    @SuppressWarnings("unchecked")
    044    public <U extends AssignmentContext> U getAssignmentContext(Assignment<V, T> assignment, AssignmentContextReference<V, T, U> reference) {
    045        if (reference.getParent() instanceof CanHoldContext) {
    046            AssignmentContext[] contexts = ((CanHoldContext)reference.getParent()).getContext();
    047            if (contexts[0] == null)
    048                contexts[0] = reference.getParent().createAssignmentContext(assignment);
    049            return (U)contexts[0];
    050        } else {
    051            return super.getAssignmentContext(assignment, reference);
    052        }
    053    }
    054
    055    @Override
    056    public <C extends AssignmentContext> void clearContext(AssignmentContextReference<V, T, C> reference) {
    057        if (reference.getParent() instanceof CanHoldContext) {
    058            AssignmentContext[] contexts = ((CanHoldContext)reference.getParent()).getContext();
    059            contexts[0] = null;
    060        } else {
    061            super.clearContext(reference);
    062        }
    063    }
    064}