001package de.monochromata.anaphors.ast.reference; 002 003import de.monochromata.anaphors.ast.ASTBasedAnaphora; 004import de.monochromata.anaphors.ast.feature.DefaultFeatureContainer; 005import de.monochromata.anaphors.ast.relatedexp.RelatedExpression; 006import de.monochromata.anaphors.ast.spi.AnaphoraResolutionSpi; 007import de.monochromata.anaphors.ast.spi.RelatedExpressionsSpi; 008 009/** 010 * An abstract base class for referents. 011 * 012 * @param <N> The node type in the AST 013 * @param <E> The expression type 014 * @param <T> The type type 015 * @param <B> The binding type 016 * @param <VB> The variable binding type 017 * @param <FB> The field 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 <R> The sub-type of related expression to use 024 * @param <A> The sub-type of AST-based anaphora to use 025 */ 026public abstract class AbstractReferent<N, E, T, B, VB extends B, FB extends B, MB extends B, TB extends B, S, I, QI, R extends RelatedExpression<N, T, B, TB, S, QI, R>, A extends ASTBasedAnaphora<N, E, T, B, TB, S, I, QI, R, A>> 027 extends DefaultFeatureContainer<QI> implements Referent<TB, S, I, QI> { 028 029 private final R relatedExpression; 030 private final String description; 031 protected final AnaphoraResolutionSpi<N, E, T, B, VB, FB, MB, TB, S, I, QI, R, A> anaphorResolutionSpi; 032 033 /** 034 * Used in contract testing. 035 */ 036 @SuppressWarnings("unused") 037 protected AbstractReferent() { 038 relatedExpression = null; 039 description = null; 040 anaphorResolutionSpi = null; 041 } 042 043 public <EV, PP> AbstractReferent(final R relatedExpression, final String description, 044 final RelatedExpressionsSpi<N, E, T, B, MB, TB, S, I, QI, EV, PP, R> relatedExpressionsSpi, 045 final AnaphoraResolutionSpi<N, E, T, B, VB, FB, MB, TB, S, I, QI, R, A> anaphorResolutionSpi) { 046 this.relatedExpression = relatedExpression; 047 this.description = description; 048 this.anaphorResolutionSpi = anaphorResolutionSpi; 049 addAll(relatedExpressionsSpi.getFeatures(relatedExpression.getRelatedExpression())); 050 } 051 052 protected R getRelatedExpression() { 053 return relatedExpression; 054 } 055 056 @Override 057 public String getDescription() { 058 return description; 059 } 060 061 @Override 062 public int hashCode() { 063 final int prime = 31; 064 int result = super.hashCode(); 065 result = prime * result + ((description == null) ? 0 : description.hashCode()); 066 result = prime * result + ((relatedExpression == null) ? 0 : relatedExpression.hashCode()); 067 return result; 068 } 069 070 @Override 071 public boolean equals(final Object obj) { 072 if (this == obj) { 073 return true; 074 } 075 if (!super.equals(obj)) { 076 return false; 077 } 078 if (getClass() != obj.getClass()) { 079 return false; 080 } 081 final AbstractReferent other = (AbstractReferent) obj; 082 if (description == null) { 083 if (other.description != null) { 084 return false; 085 } 086 } else if (!description.equals(other.description)) { 087 return false; 088 } 089 if (relatedExpression == null) { 090 if (other.relatedExpression != null) { 091 return false; 092 } 093 } else if (!relatedExpression.equals(other.relatedExpression)) { 094 return false; 095 } 096 return true; 097 } 098 099 @Override 100 public String toString() { 101 return "AbstractReferent [relatedExpression=" + relatedExpression + ", description=" + description 102 + ", getFeatures()=" + getFeatures() + "]"; 103 } 104 105}