All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ggp.base.util.gdl.model.AbstractSentenceForm Maven / Gradle / Ivy

There is a newer version: 0.0.15
Show newest version
package org.ggp.base.util.gdl.model;

import java.util.List;

import org.ggp.base.util.gdl.grammar.GdlConstant;
import org.ggp.base.util.gdl.grammar.GdlPool;
import org.ggp.base.util.gdl.grammar.GdlSentence;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.Lists;

/**
 * Defines the hashCode, equals, and toString methods for SentenceForms so
 * different SentenceForms can be compatible in terms of how they treat these
 * methods. SentenceForm implementations should extend this class and should
 * not reimplement hashCode, equals, or toString.
 */
public abstract class AbstractSentenceForm implements SentenceForm {
    private final Supplier underscoreSentence =
            Suppliers.memoize(new Supplier() {
                @Override
                public GdlSentence get() {
                    List underscores = getNUnderscores(getTupleSize());
                    return getSentenceFromTuple(underscores);
                }
            });

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof SentenceForm)) {
            return false;
        }
        SentenceForm o = (SentenceForm) obj;
        if (this.getName() != o.getName()) {
            return false;
        }
        if (this.getTupleSize() != o.getTupleSize()) {
            return false;
        }
        return o.matches(underscoreSentence.get());
    }

    private static List getNUnderscores(int numTerms) {
        GdlConstant underscore = GdlPool.UNDERSCORE;
        List terms = Lists.newArrayListWithCapacity(numTerms);
        for (int i = 0; i < numTerms; i++) {
            terms.add(underscore);
        }
        return terms;
    }

    private volatile int hashCode = 0;
    @Override
    public int hashCode() {
        if (hashCode == 0) {
            hashCode = toString().hashCode();
        }
        return hashCode;
    }

    //TODO: Memoize this! (Important!)
    @Override
    public String toString() {
        return underscoreSentence.get().toString();
    }

    @Override
    public GdlSentence getSampleSentence() {
        return underscoreSentence.get();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy