// if we are below a concrete class then we cannot declare any
// more primary key fields; thus, just use the parent invocation
if (hasConcreteSuperclass() || (hasSuperclass && _fields.length == 0))
return "";
CodeFormat code = newCodeFormat();
code.tab().append("public boolean equals").openParen(true).
append("Object obj").closeParen().openBrace(2).endl();
code.tab(2).append("if").openParen(true).
append("this == obj").closeParen().endl();
code.tab(3).append("return true;").endl();
// call super.equals() if we have a superclass
String className = getClassName();
if (hasSuperclass) {
code.tab(2).append("if").openParen(true).
append("!super.equals").openParen(true).
append("obj").closeParen().closeParen().endl();
code.tab(3).append("return false;").endl();
} else {
code.tab(2).append("if").openParen(true).
append("obj == null || obj.getClass").parens().
append(" != ").append("getClass").parens().
closeParen().endl();
code.tab(3).append("return false;").endl();
}
String name;
Class type;
for (int i = 0; i < _fields.length; i++) {
if (i == 0) {
code.endl().tab(2).append(className).append(" other = ").
openParen(false).append(className).closeParen().
append(" obj;").endl();
}
// if this is not the first field, add an &&
if (i == 0)
code.tab(2).append("return ");
else
code.endl().tab(3).append("&& ");
name = _fields[i].getName();
type = _fields[i].getObjectIdFieldType();
if (type.isPrimitive()) {
code.openParen(false).append(name).append(" == ").
append("other.").append(name).closeParen();
} else if (type == byte[].class) {
code.openParen(false).append("equals").openParen(true).
append(name).append(", ").append("other.").
append(name).closeParen().closeParen();
} else if (type == char[].class) {
// ((name == null && other.name == null)
// || (name != null && String.valueOf (name).
// equals (String.valueOf (other.name))))
code.append("(").openParen(false).append(name).
append(" == null && other.").append(name).
append(" == null").closeParen().endl();
code.tab(3).append("|| ");
code.openParen(false).append(name).append(" != null ").
append("&& String.valueOf").openParen(true).append(name).
closeParen().append(".").endl();
code.tab(3).append("equals").openParen(true).
append("String.valueOf").openParen(true).
append("other.").append(name).closeParen().closeParen().
closeParen().append(")");
} else {
// ((name == null && other.name == null)
// || (name != null && name.equals (other.name)))
code.append("(").openParen(false).append(name).
append(" == null && other.").append(name).
append(" == null").closeParen().endl();
code.tab(3).append("|| ");
code.openParen(false).append(name).append(" != null ").
append("&& ").append(name).append(".equals").
openParen(true).append("other.").append(name).
closeParen().closeParen().append(")");
}
}
// no _fields: just return true after checking instanceof
if (_fields.length == 0)
code.tab(2).append("return true;").endl();
else
code.append(";").endl();
code.closeBrace(2);
return code.toString();
}