public HollowListJavaGenerator(String packageName, String apiClassname, HollowListSchema schema, Set<String> parameterizedTypes, boolean parameterizeClassNames, HollowDataset dataset, CodeGeneratorConfig config) { super(packageName, apiClassname, schema, dataset, config); this.schema = schema; this.elementClassName = hollowImplClassname(schema.getElementType()); this.parameterize = parameterizeClassNames || parameterizedTypes.contains(schema.getElementType()); }
@Override public boolean equals(Object other) { if(!(other instanceof HollowListSchema)) return false; HollowListSchema otherSchema = (HollowListSchema)other; if(!getName().equals(otherSchema.getName())) return false; return getElementType().equals(otherSchema.getElementType()); }
@Override public String toString() { return getName() + " List<" + getElementType() + ">;"; }
@Override public void writeTo(OutputStream os) throws IOException { DataOutputStream dos = new DataOutputStream(os); dos.write(SchemaType.LIST.getTypeId()); dos.writeUTF(getName()); dos.writeUTF(getElementType()); }
@Override public boolean equalsElement(int elementOrdinal, Object testObject) { return GenericHollowRecordHelper.equalObject(getSchema().getElementType(), elementOrdinal, testObject); }
@Override public String generate() { StringBuilder builder = new StringBuilder(); appendPackageAndCommonImports(builder, apiClassname); builder.append("import " + HollowListTypeAPI.class.getName() + ";\n\n"); builder.append("import " + HollowListTypeDataAccess.class.getName() + ";\n"); builder.append("import " + HollowListLookupDelegate.class.getName() + ";\n"); builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class ").append(className).append(" extends HollowListTypeAPI {\n\n"); builder.append(" private final ").append(delegateLookupClassname(schema)).append(" delegateLookupImpl;\n\n"); builder.append(" public ").append(className).append("(").append(apiClassname).append(" api, HollowListTypeDataAccess dataAccess) {\n"); builder.append(" super(api, dataAccess);\n"); builder.append(" this.delegateLookupImpl = new ").append(delegateLookupClassname(schema)).append("(this);\n"); builder.append(" }\n\n"); builder.append(" public ").append(typeAPIClassname(schema.getElementType())).append(" getElementAPI() {\n"); builder.append(" return getAPI().get").append(typeAPIClassname(schema.getElementType())).append("();\n"); builder.append(" }\n\n"); builder.append(" public ").append(delegateLookupClassname(schema)).append(" getDelegateLookupImpl() {\n"); builder.append(" return delegateLookupImpl;\n"); builder.append(" }\n\n"); builder.append(" public ").append(apiClassname).append(" getAPI() {\n"); builder.append(" return (").append(apiClassname).append(")api;\n"); builder.append(" }\n\n"); builder.append("}"); return builder.toString(); }
private void appendListStringify(Writer writer, HollowDataAccess dataAccess, HollowListTypeDataAccess typeDataAccess, int ordinal, int indentation) throws IOException { HollowListSchema schema = typeDataAccess.getSchema(); if(showTypes) writer.append("(").append(schema.getName()).append(")"); if(showOrdinals) writer.append(" (ordinal ").append(Integer.toString(ordinal)).append(")"); indentation++; int size = typeDataAccess.size(ordinal); String elementType = schema.getElementType(); for(int i=0;i<size;i++) { writer.append(NEWLINE); int elementOrdinal = typeDataAccess.getElementOrdinal(ordinal, i); appendIndentation(writer, indentation); writer.append("e" + i + ": "); appendStringify(writer, dataAccess, elementType, elementOrdinal, indentation); } }
private void generateChainableAddForSetAndList(StringBuilder classBodyBuilder) { for (int i = 0; i < schema.numFields(); i++) { if (schema.getFieldType(i) != FieldType.REFERENCE) { continue; } HollowSchema referencedSchema = dataset.getSchema(schema.getReferencedType(i)); if (referencedSchema instanceof HollowListSchema || referencedSchema instanceof HollowSetSchema) { HollowSchema elementSchema = dataset.getSchema(referencedSchema instanceof HollowListSchema ? ((HollowListSchema) referencedSchema).getElementType() : ((HollowSetSchema) referencedSchema).getElementType()); String elementType = buildFieldType(elementSchema); Class fieldImplementationType = referencedSchema instanceof HollowListSchema ? ArrayList.class : HashSet.class; importClasses.add(fieldImplementationType); classBodyBuilder.append(" public ").append(getClassName()).append(" addTo") .append(uppercase(getFieldName(i))).append("(") .append(elementType).append(" ").append(lowercase(elementType)).append(") {\n"); classBodyBuilder.append(" if (this.").append(getFieldName(i)).append(" == null) {\n"); classBodyBuilder.append(" this.").append(getFieldName(i)).append(" = new ") .append(fieldImplementationType.getSimpleName()).append("<").append(elementType).append(">();\n"); classBodyBuilder.append(" }\n"); classBodyBuilder.append(" this.").append(getFieldName(i)).append(".add(") .append(lowercase(elementType)).append(");\n"); classBodyBuilder.append(" return this;\n"); classBodyBuilder.append(" }\n"); } } }
private void appendListStringify(Writer writer, HollowDataAccess dataAccess, HollowListTypeDataAccess typeDataAccess, int ordinal, int indentation) throws IOException { HollowListSchema schema = typeDataAccess.getSchema(); indentation++; int size = typeDataAccess.size(ordinal); if(size == 0) { writer.append("[]"); } else { writer.append("[\n"); String elementType = schema.getElementType(); for(int i=0;i<size;i++) { int elementOrdinal = typeDataAccess.getElementOrdinal(ordinal, i); if(prettyPrint) appendIndentation(writer, indentation); appendStringify(writer, dataAccess, elementType, elementOrdinal, indentation); if(i < size - 1) writer.append(",\n"); } if(prettyPrint) { writer.append(NEWLINE); appendIndentation(writer, indentation - 1); } writer.append("]"); } }
@Override public HollowRecord instantiateElement(int elementOrdinal) { return GenericHollowRecordHelper.instantiate(getTypeDataAccess().getDataAccess(), getSchema().getElementType(), elementOrdinal); }
private String buildFieldType(HollowSchema referencedSchema) { if (referencedSchema instanceof HollowObjectSchema) { return buildClassName(referencedSchema.getName(), classNameSuffix); } else if (referencedSchema instanceof HollowListSchema) { importClasses.add(List.class); HollowSchema elementSchema = dataset.getSchema(((HollowListSchema)referencedSchema).getElementType()); return "List<" + buildFieldType(elementSchema) + ">"; } else if (referencedSchema instanceof HollowSetSchema) { importClasses.add(Set.class); HollowSchema elementSchema = dataset.getSchema(((HollowSetSchema)referencedSchema).getElementType()); return "Set<" + buildFieldType(elementSchema) + ">"; } else if (referencedSchema instanceof HollowMapSchema) { importClasses.add(Map.class); HollowSchema keySchema = dataset.getSchema(((HollowMapSchema)referencedSchema).getKeyType()); HollowSchema valueSchema = dataset.getSchema(((HollowMapSchema)referencedSchema).getValueType()); return "Map<" + buildFieldType(keySchema) + ", " + buildFieldType(valueSchema) + ">"; } throw new IllegalArgumentException("Schema is unrecognized type " + referencedSchema.getClass().getSimpleName()); }
private String expectedCollectionClassName(HollowSchema referencedSchema) { if (referencedSchema instanceof HollowObjectSchema) { return referencedSchema.getName(); } else if (referencedSchema instanceof HollowListSchema) { importClasses.add(List.class); HollowSchema elementSchema = dataset.getSchema(((HollowListSchema)referencedSchema).getElementType()); return "ListOf" + expectedCollectionClassName(elementSchema); } else if (referencedSchema instanceof HollowSetSchema) { importClasses.add(Set.class); HollowSchema elementSchema = dataset.getSchema(((HollowSetSchema)referencedSchema).getElementType()); return "SetOf" + expectedCollectionClassName(elementSchema); } else if (referencedSchema instanceof HollowMapSchema) { importClasses.add(Map.class); HollowSchema keySchema = dataset.getSchema(((HollowMapSchema)referencedSchema).getKeyType()); HollowSchema valueSchema = dataset.getSchema(((HollowMapSchema)referencedSchema).getValueType()); return "MapOf" + expectedCollectionClassName(keySchema) + "To" + expectedCollectionClassName(valueSchema); } throw new IllegalArgumentException("Expected HollowCollectionSchema or HollowMapSchema but got " + referencedSchema.getClass().getSimpleName()); }
void wireTypeStatesToSchemas() { for(HollowTypeReadState state : typeStates.values()) { switch(state.getSchema().getSchemaType()) { case OBJECT: HollowObjectSchema objSchema = (HollowObjectSchema)state.getSchema(); for(int i=0;i<objSchema.numFields();i++) { if(objSchema.getReferencedType(i) != null) objSchema.setReferencedTypeState(i, typeStates.get(objSchema.getReferencedType(i))); } break; case LIST: HollowListSchema listSchema = (HollowListSchema)state.getSchema(); listSchema.setElementTypeState(typeStates.get(listSchema.getElementType())); break; case SET: HollowSetSchema setSchema = (HollowSetSchema)state.getSchema(); setSchema.setElementTypeState(typeStates.get(setSchema.getElementType())); ((HollowSetTypeReadState)state).buildKeyDeriver(); break; case MAP: HollowMapSchema mapSchema = (HollowMapSchema)state.getSchema(); mapSchema.setKeyTypeState(typeStates.get(mapSchema.getKeyType())); mapSchema.setValueTypeState(typeStates.get(mapSchema.getValueType())); ((HollowMapTypeReadState)state).buildKeyDeriver(); break; } } }
case LIST: addToSetIfNotPrimitiveOrCollection(schemaNameSet, ((HollowListSchema) schema).getElementType()); break; case MAP:
@Override public HollowWriteRecord copy(int ordinal) { HollowListWriteRecord rec = rec(); rec.reset(); String elementType = readState().getSchema().getElementType(); int size = readState().size(ordinal); for(int i=0;i<size;i++) { int elementOrdinal = readState().getElementOrdinal(ordinal, i); int remappedElementOrdinal = ordinalRemapper.getMappedOrdinal(elementType, elementOrdinal); rec.addElement(remappedElementOrdinal); } return rec; }
@Test public void parsesListSchema() throws IOException { String listSchema = "ListOfTypeA List<TypeA>;\n"; HollowListSchema schema = (HollowListSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("ListOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }