miduo_server/jieling-dbgen/src/main/java/com/dbgen/VarDeepCopy.java

108 lines
2.3 KiB
Java

package com.dbgen;
import java.io.PrintStream;
class VarDeepCopy implements Visitor {
static void make(Variable var, PrintStream ps, String prefix) {
var.getType().accept(new VarDeepCopy(var.getName(), ps, prefix));
}
private static String getCopy(Type type, String fullvarname, String parentVarname) {
if (type instanceof Jbean) {
return "new " + type.getName() + "(" + fullvarname + ")";
} else {
return fullvarname;
}
}
private String varname;
private String this_varname;
private PrintStream ps;
private String prefix;
private VarDeepCopy(String varname, PrintStream ps, String prefix) {
this.varname = varname;
this.ps = ps;
this.prefix = prefix;
this.this_varname = "this." + varname;
}
@Override
public void visit(Jbean type) {
ps.println(prefix + this_varname + " = new " + type.getName() + "(_o_." + varname +");");
}
@Override
public void visit(TypeString type) {
simple();
}
private void simple() {
ps.println(prefix + this_varname + " = _o_." + varname + ";");
}
@Override
public void visit(TypeBoolean type) {
simple();
}
@Override
public void visit(TypeShort type) {
simple();
}
@Override
public void visit(TypeInt type) {
simple();
}
@Override
public void visit(TypeLong type) {
simple();
}
@Override
public void visit(TypeFloat type) {
simple();
}
@Override
public void visit(TypeDouble type) {
simple();
}
private void collection(Type type, Type value) {
ps.println(prefix + this_varname + " = new " + TypeName.getName(type) + "();");
ps.println(prefix + "_o_." + varname + ".forEach(_v_ -> " + this_varname + ".add("
+ getCopy(value, "_v_", varname) + "));");
}
@Override
public void visit(TypeList type) {
collection(type, type.getValueType());
}
@Override
public void visit(TypeSet type) {
collection(type, type.getValueType());
}
@Override
public void visit(TypeMap type) {
ps.println(prefix + this_varname + " = new " + TypeName.getName(type) + "();");
Type key = type.getKeyType();
Type value = type.getValueType();
String keycopy = getCopy(key, "_k_", varname);
String valuecopy =getCopy(value, "_v_", varname);
ps.println(prefix + "_o_." + varname + ".forEach((_k_, _v_) -> " + this_varname + ".put(" + keycopy + ", "
+ valuecopy + "));");
}
}