001package de.monochromata.anaphors.ast.relatedexp.strategy;
002
003import java.util.List;
004
005import de.monochromata.AbstractStrategy;
006import de.monochromata.anaphors.ast.relatedexp.RelatedExpression;
007import de.monochromata.anaphors.ast.spi.RelatedExpressionsSpi;
008import de.monochromata.anaphors.preferences.Preferences;
009
010/**
011 * An abstract base class for strategies used to generate (potential) related
012 * expressions from AST nodes.
013 *
014 * @param <N>  The node type in the AST
015 * @param <E>  The expression type
016 * @param <T>  The type type
017 * @param <B>  The binding type
018 * @param <MB> The method binding type
019 * @param <TB> The type binding type
020 * @param <S>  The scope type (optional)
021 * @param <I>  The type used to represent identifiers
022 * @param <QI> The type used to represent qualified identifiers
023 * @param <EV> The type of the event contained in the condition that is
024 *             evaluated to check when the perspectivations shall be applied.
025 * @param <PP> The type used for positions that carry perspectivations
026 * @param <R>  The sub-type of related expression to use
027 */
028public abstract class AbstractRelatedExpressionStrategy<N, E, T, B, MB extends B, TB extends B, S, I, QI, EV, PP, R extends RelatedExpression<N, T, B, TB, S, QI, R>>
029        extends AbstractStrategy implements RelatedExpressionStrategy<N, T, B, TB, S, QI, R> {
030
031    private List<N> collection;
032    protected final RelatedExpressionsSpi<N, E, T, B, MB, TB, S, I, QI, EV, PP, R> relatedExpressionsSpi;
033    protected final Preferences preferences;
034
035    /**
036     * Used in contract testing.
037     */
038    @SuppressWarnings("unused")
039    protected AbstractRelatedExpressionStrategy() {
040        this(null, null);
041    }
042
043    public AbstractRelatedExpressionStrategy(
044            final RelatedExpressionsSpi<N, E, T, B, MB, TB, S, I, QI, EV, PP, R> relatedExpressionsSpi,
045            final Preferences preferences) {
046        this.relatedExpressionsSpi = relatedExpressionsSpi;
047        this.preferences = preferences;
048    }
049
050    @Override
051    public void collectTo(final List<N> potentialRelatedExpressions) {
052        this.collection = potentialRelatedExpressions;
053    }
054
055    @Override
056    public void stopCollection() {
057        this.collection = null;
058    }
059
060    /**
061     * Add the given potential related expression to the current collection.
062     *
063     * @param potentialRelatedExpression Adds the given potential related expression
064     *                                   to the collection of potential related
065     *                                   expressions.
066     * @see #collectTo(List)
067     * @see #stopCollection()
068     */
069    protected void addToCollection(final N potentialRelatedExpression) {
070        if (this.collection == null) {
071            throw new IllegalStateException("No collection set, need to invoke collectTo(List<ASTNode>)");
072        }
073        this.collection.add(potentialRelatedExpression);
074    }
075}