/** * Returns true if this is equal to that. This is true iff that is a structured type with the same * fields as this. * * @param that type to compare to * @return true if this and that are the same type */ public boolean equals(Object that) { if (!(that instanceof TypeStruct)) return false; TypeStruct ts = (TypeStruct) that; // that must have exactly as many fields as this. if (that.getNumFields() != this.getNumFields()) return false; // loop through: for (int i = 0; i < getNumFields(); i++) { if (!(getName(i).equals(ts.getName(i)))) return false; if (!(getType(i).equals(ts.getType(i)))) return false; } return true; }
/** * Return true if this can be converted to that without penalty. This is true if that is also a * structured type, and that's fields are a superset of this's and in the same order. * * @param that type to compare to * @return true if this can be converted to that without penalty */ public boolean isConvertibleTo(Type that) { // Two cases that are interesting: either (1) that is also // a TypeStruct that is a superset of this, or (2) that is a // TypeLaidOut that is a valid layout of this. if (that instanceof TypeStruct) { TypeStruct ts = (TypeStruct) that; // that must have at least as many fields as this. if (that.getNumFields() < this.getNumFields()) return false; // loop through: for (int i = 0; i < getNumFields(); i++) { if (!(getName(i).equals(ts.getName(i)))) return false; if (!(getType(i).isConvertibleTo(ts.getType(i)))) return false; } return true; } else return false; }