001package de.monochromata.anaphors.cog.memory;
002
003import static java.lang.Long.MIN_VALUE;
004
005import de.monochromata.anaphors.cog.activation.ActivationFormula;
006import de.monochromata.anaphors.cog.activation.EstimatedActivationValue;
007
008/**
009 * @param <T> The type of object that this chunk represents.
010 * @since 1.1.0
011 */
012public class DefaultChunk<T> implements Chunk<T> {
013
014    private final ActivationFormula activationFormula;
015    private final T represented;
016
017    private EstimatedActivationValue lastActivation;
018    private long lastActivationCalculatedForTimestamp = MIN_VALUE;
019
020    /**
021     * Used in contract testing.
022     */
023    @SuppressWarnings("unused")
024    protected DefaultChunk() {
025        this(null, null);
026    }
027
028    public DefaultChunk(final ActivationFormula activationFormula, final T represented) {
029        this.activationFormula = activationFormula;
030        this.represented = represented;
031    }
032
033    @Override
034    public synchronized EstimatedActivationValue getEstimatedActivationValue(final long timestamp) {
035        if (lastActivation != null && lastActivationCalculatedForTimestamp == timestamp) {
036            return lastActivation;
037        }
038        lastActivation = activationFormula.estimateActivation(this, timestamp);
039        lastActivationCalculatedForTimestamp = timestamp;
040        return lastActivation;
041    }
042
043    @Override
044    public T getRepresented() {
045        return represented;
046    }
047
048    @Override
049    public int hashCode() {
050        final int prime = 31;
051        int result = 1;
052        result = prime * result + ((represented == null) ? 0 : represented.hashCode());
053        return result;
054    }
055
056    @Override
057    public boolean equals(final Object obj) {
058        if (this == obj) {
059            return true;
060        }
061        if (obj == null) {
062            return false;
063        }
064        if (getClass() != obj.getClass()) {
065            return false;
066        }
067        final DefaultChunk<?> other = (DefaultChunk<?>) obj;
068        if (represented == null) {
069            if (other.represented != null) {
070                return false;
071            }
072        } else if (!represented.equals(other.represented)) {
073            return false;
074        }
075        return true;
076    }
077
078}