package org.spoofax;

import org.spoofax.interpreter.terms.IStrategoList;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.ITermPrinter;
import org.spoofax.terms.TermFactory;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


/**
 * A dummy Stratego term.
 */
public class DummyStrategoTerm extends DummySimpleTerm implements IStrategoTerm {

    @Override
    public IStrategoTerm getSubterm(int i) {
        throw new IndexOutOfBoundsException();
    }

    @Override
    public IStrategoTerm[] getAllSubterms() {
        return TermFactory.EMPTY_TERM_ARRAY;
    }

    @Override
    public List<IStrategoTerm> getSubterms() {
        return Collections.emptyList();
    }

    @Override
    public int getTermType() {
        return 9;
    }

    @Override
    public IStrategoList getAnnotations() {
        throw new IllegalStateException("Not supported.");
    }

    @Override
    public boolean match(IStrategoTerm second) {
        return true;
    }

    @Override
    public void prettyPrint(ITermPrinter pp) {
        try {
            pp.append("<dummy>");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean equals(Object obj) {
        // Identity
        return this == obj;
    }

    @Override
    public int hashCode() {
        // Identity
        return System.identityHashCode(this);
    }

    @Override
    public String toString(int maxDepth) {
        return "<dummy>";
    }

    @Override
    public void writeAsString(Appendable output, int maxDepth) throws IOException {
        output.append("<dummy>");
    }

    @Override
    public Iterator<IStrategoTerm> iterator() {
        return Arrays.asList(getAllSubterms()).iterator();
    }

}

@@ -1071,11 +1071,21 @@
 rn t
-rue
+his == second
 ;%0A  
@@ -1182,33 +1182,39 @@
   pp.append(
-%22%3Cdummy%3E%22
+this.toString()
 );%0A        %7D
@@ -1464,61 +1464,277 @@
  // 
-Identity%0A        return System.identityHashCode(this)
+Force hash collisions.%0A        // This is a valid (but inefficient) implementation of hashCode(),%0A        // to ensure code that bails out on hash code inequality will still run.%0A        return 0;%0A    %7D%0A%0A    @Override public String toString() %7B%0A        return %22%3Cdummy%3E%22
 ;%0A  
@@ -1806,33 +1806,39 @@
      return 
-%22%3Cdummy%3E%22
+this.toString()
 ;%0A    %7D%0A%0A   
@@ -1954,25 +1954,31 @@
 .append(
-%22%3Cdummy%3E%22
+this.toString()
 );%0A    %7D

Fix DummyStrategoTerm match and force hash collisions