Skip to content

Instantly share code, notes, and snippets.

@emmanueltouzery
Last active September 10, 2017 14:09
Show Gist options
  • Save emmanueltouzery/200edb5d4611441584c501161fd2e622 to your computer and use it in GitHub Desktop.
Save emmanueltouzery/200edb5d4611441584c501161fd2e622 to your computer and use it in GitHub Desktop.
package com.lecip.core.deployment;
import cz.habarta.typescript.generator.Settings;
import cz.habarta.typescript.generator.TsType;
import cz.habarta.typescript.generator.emitter.EmitterExtension;
import cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures;
import cz.habarta.typescript.generator.emitter.TsBeanModel;
import cz.habarta.typescript.generator.emitter.TsPropertyModel;
public class TypeScriptEqualsHashcodeEmitter extends EmitterExtension {
@Override
public EmitterExtensionFeatures getFeatures() {
final EmitterExtensionFeatures features = new EmitterExtensionFeatures();
features.addsCodeToGeneratedClasses = true;
return features;
}
private static boolean hasEquals(TsType tsType) {
if (tsType.equals(TsType.Number)
|| tsType.equals(TsType.String)
|| tsType.equals(TsType.Date)
|| tsType.equals(TsType.Boolean)) {
return false;
}
return true;
}
@Override
public void beanGenerationAddCode(EmitterExtension.Writer writer, Settings settings, TsBeanModel bean) {
if (bean.getOrigin() == null) {
return;
}
if (bean.getOrigin().getAnnotation(EqualityById.class) != null) {
writer.writeIndentedLine("equals(other: " + bean.getName().getSimpleName() + ") {");
writer.writeIndentedLine(settings.indentString + "return this.id === other.id;");
writer.writeIndentedLine("}");
writer.writeIndentedLine("hashCode(): number {");
writer.writeIndentedLine(settings.indentString + "return getHashCode(this.id);");
writer.writeIndentedLine("}");
} else if (bean.getOrigin().getAnnotation(EqualityByValue.class) != null) {
writer.writeIndentedLine("equals(other: " + bean.getName().getSimpleName() + ") {");
boolean isFirst = true;
for (TsPropertyModel property : bean.getProperties()) {
final String prefix = isFirst ? "return " : (settings.indentString + "&& ");
if (hasEquals(property.tsType)) {
writer.writeIndentedLine(settings.indentString + prefix + "this." + property.name + "equals(other." + property.name + ")");
} else {
writer.writeIndentedLine(settings.indentString + prefix + "this." + property.name + " === other." + property.name);
}
isFirst = false;
}
writer.writeIndentedLine(settings.indentString + ";");
writer.writeIndentedLine("}");
writer.writeIndentedLine("hashCode(): number {");
writer.writeIndentedLine(settings.indentString + "return getHashCode(");
for (TsPropertyModel property : bean.getProperties()) {
final String prefix = isFirst ? "" : (settings.indentString + ", ");
writer.writeIndentedLine(settings.indentString + prefix + "this." + property.name);
isFirst = false;
}
writer.writeIndentedLine(settings.indentString + ");");
writer.writeIndentedLine("}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment