in hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowObjectJavaGenerator.java [394:462]
private void appendPrimaryKey(StringBuilder classBuilder, PrimaryKey pk) {
if (pk.numFields() == 1) {
String fieldPath = pk.getFieldPath(0);
FieldType fieldType = pk.getFieldType(dataset, 0);
String type, boxedType;
if (FieldType.REFERENCE.equals(fieldType)) {
HollowObjectSchema refSchema = pk.getFieldSchema(dataset, 0);
type = boxedType = hollowImplClassname(refSchema.getName());
} else {
type = HollowCodeGenerationUtils.getJavaScalarType(fieldType);
boxedType = HollowCodeGenerationUtils.getJavaBoxedType(fieldType);
}
appendPrimaryKeyDoc(classBuilder, fieldType, type);
classBuilder.append(" public static UniqueKeyIndex<" + className + ", " + boxedType + "> uniqueIndex(HollowConsumer consumer) {\n");
classBuilder.append(" return UniqueKeyIndex.from(consumer, " + className + ".class)\n");
classBuilder.append(" .bindToPrimaryKey()\n");
classBuilder.append(" .usingPath(\"" + fieldPath + "\", " + type + ".class);\n");
classBuilder.append(" }\n\n");
} else {
appendPrimaryKeyDoc(classBuilder, FieldType.REFERENCE, className + ".Key");
classBuilder.append(" public static UniqueKeyIndex<" + className + ", " + className + ".Key> uniqueIndex(HollowConsumer consumer) {\n");
classBuilder.append(" return UniqueKeyIndex.from(consumer, " + className + ".class)\n");
classBuilder.append(" .bindToPrimaryKey()\n");
classBuilder.append(" .usingBean(" + className + ".Key.class);\n");
classBuilder.append(" }\n\n");
classBuilder.append(" public static class Key {\n");
Map<String, String> parameterList = new LinkedHashMap<>();
for (int i = 0; i < pk.numFields(); i++) {
if (i > 0) {
classBuilder.append("\n");
}
String fieldPath = pk.getFieldPath(i);
String name = HollowCodeGenerationUtils.normalizeFieldPathToParamName(fieldPath);
FieldType fieldType = pk.getFieldType(dataset, i);
String type;
if (FieldType.REFERENCE.equals(fieldType)) {
HollowObjectSchema refSchema = pk.getFieldSchema(dataset, i);
type = hollowImplClassname(refSchema.getName());
} else {
type = HollowCodeGenerationUtils.getJavaScalarType(fieldType);
}
parameterList.put(name, type);
classBuilder.append(" @FieldPath(\"" + fieldPath + "\")\n");
classBuilder.append(" public final " + type + " " + name + ";\n");
}
classBuilder.append("\n");
String parameters = parameterList.entrySet().stream()
.map(e -> e.getValue() + " " + e.getKey())
.collect(joining(", "));
classBuilder.append(" public Key(" + parameters + ") {\n");
parameterList.forEach((n, t) -> {
if (t.equals("byte[]")) {
classBuilder.append(" this." + n + " = " + n + " == null ? null : " + n + ".clone();\n");
} else {
classBuilder.append(" this." + n + " = " + n + ";\n");
}
});
classBuilder.append(" }\n");
classBuilder.append(" }\n\n");
}
}