public HollowSetJavaGenerator(String packageName, String apiClassname, HollowSetSchema 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()); }
private boolean isDefinedHashCode(HollowSchema schema) { if(schema instanceof HollowSetSchema) return typeNamesWithDefinedHashCodes.contains(((HollowSetSchema)schema).getElementType()); if(schema instanceof HollowMapSchema) return typeNamesWithDefinedHashCodes.contains(((HollowMapSchema)schema).getKeyType()); return false; }
@Override public boolean equals(Object other) { if(!(other instanceof HollowSetSchema)) return false; HollowSetSchema otherSchema = (HollowSetSchema)other; if(!getName().equals(otherSchema.getName())) return false; if(!getElementType().equals(otherSchema.getElementType())) return false; return isNullableObjectEquals(hashKey, otherSchema.getHashKey()); }
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");
@Override public String toString() { StringBuilder builder = new StringBuilder(getName()); builder.append(" Set<").append(getElementType()).append(">"); if(hashKey != null) { builder.append(" @HashKey("); if(hashKey.numFields() > 0) { builder.append(hashKey.getFieldPath(0)); for(int i=1;i<hashKey.numFields();i++) { builder.append(", ").append(hashKey.getFieldPath(i)); } } builder.append(")"); } builder.append(";"); return builder.toString(); }
@Override public boolean equalsElement(int elementOrdinal, Object testObject) { return GenericHollowRecordHelper.equalObject(getSchema().getElementType(), elementOrdinal, testObject); }
private boolean shouldPreserveHashPositions(HollowSchema schema) { switch(schema.getSchemaType()) { case MAP: return readEngine.getTypesWithDefinedHashCodes().contains(((HollowMapSchema)schema).getKeyType()); case SET: return readEngine.getTypesWithDefinedHashCodes().contains(((HollowSetSchema)schema).getElementType()); default: return false; } }
private boolean shouldPreserveHashPositions(HollowSchema schema) { switch(schema.getSchemaType()) { case MAP: return from.getTypesWithDefinedHashCodes().contains(((HollowMapSchema)schema).getKeyType()); case SET: return from.getTypesWithDefinedHashCodes().contains(((HollowSetSchema)schema).getElementType()); default: return false; } }
private void appendSetStringify(Writer writer, HollowDataAccess dataAccess, HollowSetTypeDataAccess typeDataAccess, int ordinal, int indentation) throws IOException { HollowSetSchema schema = typeDataAccess.getSchema(); if(showTypes) writer.append("(").append(schema.getName()).append(")"); if(showOrdinals) writer.append(" (ordinal ").append(Integer.toString(ordinal)).append(")"); indentation++; String elementType = schema.getElementType(); HollowOrdinalIterator iter = typeDataAccess.ordinalIterator(ordinal); int elementOrdinal = iter.next(); while(elementOrdinal != NO_MORE_ORDINALS) { writer.append(NEWLINE); appendIndentation(writer, indentation); writer.append("e: "); appendStringify(writer, dataAccess, elementType, elementOrdinal, indentation); elementOrdinal = iter.next(); } }
private boolean isDefinedHashCode(HollowTypeReadState typeState) { if(typeState instanceof HollowSetTypeReadState) return input.getTypesWithDefinedHashCodes().contains(((HollowSetSchema)typeState.getSchema()).getElementType()); if(typeState instanceof HollowMapTypeReadState) return input.getTypesWithDefinedHashCodes().contains(((HollowMapSchema)typeState.getSchema()).getKeyType()); return false; }
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"); } } }
@Override public HollowRecord instantiateElement(int elementOrdinal) { return GenericHollowRecordHelper.instantiate(getTypeDataAccess().getDataAccess(), getSchema().getElementType(), elementOrdinal); }
@Override public void writeTo(OutputStream os) throws IOException { DataOutputStream dos = new DataOutputStream(os); if(getHashKey() != null) dos.write(SchemaType.SET.getTypeIdWithPrimaryKey()); else dos.write(SchemaType.SET.getTypeId()); dos.writeUTF(getName()); dos.writeUTF(getElementType()); if(getHashKey() != null) { VarInt.writeVInt(dos, getHashKey().numFields()); for(int i=0;i<getHashKey().numFields();i++) { dos.writeUTF(getHashKey().getFieldPath(i)); } } }
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()); }
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()); }
public static HollowSchema withoutKeys(HollowSchema schema) { switch(schema.getSchemaType()) { case SET: HollowSetSchema setSchema = (HollowSetSchema)schema; if(setSchema.getHashKey() != null) setSchema = new HollowSetSchema(setSchema.getName(), setSchema.getElementType()); return setSchema; case MAP: HollowMapSchema mapSchema = (HollowMapSchema)schema; if(mapSchema.getHashKey() != null) mapSchema = new HollowMapSchema(mapSchema.getName(), mapSchema.getKeyType(), mapSchema.getValueType()); return mapSchema; default: return schema; } }
@Override public HollowWriteRecord copy(int ordinal) { HollowSetWriteRecord rec = rec(); rec.reset(); String elementType = readState().getSchema().getElementType(); HollowOrdinalIterator ordinalIterator = readState().ordinalIterator(ordinal); int elementOrdinal = ordinalIterator.next(); while(elementOrdinal != HollowOrdinalIterator.NO_MORE_ORDINALS) { int remappedElementOrdinal = ordinalRemapper.getMappedOrdinal(elementType, elementOrdinal); int hashCode = preserveHashPositions ? ((HollowSetOrdinalIterator)ordinalIterator).getCurrentBucket() : remappedElementOrdinal; rec.addElement(remappedElementOrdinal, hashCode); elementOrdinal = ordinalIterator.next(); } return rec; }
@Test public void parsesSetSchema() throws IOException { String listSchema = "SetOfTypeA Set<TypeA>;\n"; HollowSetSchema schema = (HollowSetSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("SetOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesSetSchemaWithMultiFieldKey() throws IOException { String listSchema = "SetOfTypeA Set<TypeA> @HashKey(id.value, region.country.id, key);\n"; HollowSetSchema schema = (HollowSetSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("SetOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(new PrimaryKey("TypeA", "id.value", "region.country.id", "key"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesSetSchemaWithKey() throws IOException { String listSchema = "SetOfTypeA Set<TypeA> @HashKey(id.value);\n"; HollowSetSchema schema = (HollowSetSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("SetOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(new PrimaryKey("TypeA", "id.value"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }