Skip to content

Commit

Permalink
Backwards compatibility: aggressive java.lang.* classname substitutio…
Browse files Browse the repository at this point in the history
…ns in generated APIs are optional
  • Loading branch information
dkoszewnik committed Jun 27, 2017
1 parent 818f590 commit 842f045
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ public class HollowAPIClassJavaGenerator implements HollowJavaFileGenerator {
private final HollowDataset dataset;
private final boolean parameterizeClassNames;
private final String classPostfix;
private final boolean useAggressiveSubstitutions;


public HollowAPIClassJavaGenerator(String packageName, String apiClassname, HollowDataset dataset, boolean parameterizeClassNames, String classPostfix) {
public HollowAPIClassJavaGenerator(String packageName, String apiClassname, HollowDataset dataset, boolean parameterizeClassNames, String classPostfix, boolean useAggressiveSubstitutions) {
this.packageName = packageName;
this.className = apiClassname;
this.dataset = dataset;
this.parameterizeClassNames = parameterizeClassNames;
this.classPostfix = classPostfix;
this.useAggressiveSubstitutions = useAggressiveSubstitutions;
}

@Override
Expand Down Expand Up @@ -196,20 +198,20 @@ public String generate() {
for(int i=0;i<schemaList.size();i++) {
HollowSchema schema = schemaList.get(i);
if(parameterizeClassNames) {
builder.append(" public <T> Collection<T> getAll").append(hollowImplClassname(schema.getName(), classPostfix)).append("() {\n");
builder.append(" public <T> Collection<T> getAll").append(hollowImplClassname(schema.getName(), classPostfix, useAggressiveSubstitutions)).append("() {\n");
builder.append(" return new AllHollowRecordCollection<T>(getDataAccess().getTypeDataAccess(\"").append(schema.getName()).append("\").getTypeState()) {\n");
builder.append(" protected T getForOrdinal(int ordinal) {\n");
builder.append(" return get").append(hollowImplClassname(schema.getName(), classPostfix)).append("(ordinal);\n");
builder.append(" return get").append(hollowImplClassname(schema.getName(), classPostfix, useAggressiveSubstitutions)).append("(ordinal);\n");
builder.append(" }\n");
builder.append(" };\n");
builder.append(" }\n");

builder.append(" public <T> T get").append(hollowImplClassname(schema.getName(), classPostfix)).append("(int ordinal) {\n");
builder.append(" public <T> T get").append(hollowImplClassname(schema.getName(), classPostfix, useAggressiveSubstitutions)).append("(int ordinal) {\n");
builder.append(" objectCreationSampler.recordCreation(").append(i).append(");\n");
builder.append(" return (T) ").append(hollowObjectProviderName(schema.getName())).append(".getHollowObject(ordinal);\n");
builder.append(" }\n");
} else {
String hollowImplClassname = hollowImplClassname(schema.getName(), classPostfix);
String hollowImplClassname = hollowImplClassname(schema.getName(), classPostfix, useAggressiveSubstitutions);

builder.append(" public Collection<"+hollowImplClassname+"> getAll").append(hollowImplClassname).append("() {\n");
builder.append(" return new AllHollowRecordCollection<"+hollowImplClassname+">(getDataAccess().getTypeDataAccess(\"").append(schema.getName()).append("\").getTypeState()) {\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class HollowAPIGenerator {

private String classPostfix = "Hollow";
private String getterPrefix = "_";
private boolean useAggressiveSubstitutions = false;

/**
* @param apiClassname the class name of the generated implementation of {@link HollowAPI}
Expand Down Expand Up @@ -115,6 +116,15 @@ public void setClassPostfix(String classPostfix) {
public void setGetterPrefix(String getterPrefix) {
this.getterPrefix = getterPrefix;
}

/**
* Use this method to override generated classnames for type names corresponding to any class in the java.lang package.
*
* Defaults to false, which overrides only type names corresponding to a few select classes in java.lang.
*/
public void setUseAggressiveSubstitutions(boolean useAggressiveSubstitutions) {
this.useAggressiveSubstitutions = useAggressiveSubstitutions;
}


public void generateFiles(String directory) throws IOException {
Expand All @@ -124,9 +134,9 @@ public void generateFiles(String directory) throws IOException {
public void generateFiles(File directory) throws IOException {
directory.mkdirs();

HollowAPIClassJavaGenerator apiClassGenerator = new HollowAPIClassJavaGenerator(packageName, apiClassname, dataset, parameterizeClassNames, classPostfix);
HollowAPIClassJavaGenerator apiClassGenerator = new HollowAPIClassJavaGenerator(packageName, apiClassname, dataset, parameterizeClassNames, classPostfix, useAggressiveSubstitutions);
HollowAPIFactoryJavaGenerator apiFactoryGenerator = new HollowAPIFactoryJavaGenerator(packageName, apiClassname);
HollowHashIndexGenerator hashIndexGenerator = new HollowHashIndexGenerator(packageName, apiClassname, classPostfix, dataset);
HollowHashIndexGenerator hashIndexGenerator = new HollowHashIndexGenerator(packageName, apiClassname, classPostfix, useAggressiveSubstitutions, dataset);

generateFile(directory, apiClassGenerator);
generateFile(directory, apiFactoryGenerator);
Expand All @@ -145,7 +155,7 @@ private void generateFilesForHollowSchemas(File directory) throws IOException {
generateFile(directory, new HollowObjectDelegateInterfaceGenerator(packageName, (HollowObjectSchema)schema));
generateFile(directory, new HollowObjectDelegateCachedImplGenerator(packageName, (HollowObjectSchema)schema));
generateFile(directory, new HollowObjectDelegateLookupImplGenerator(packageName, (HollowObjectSchema)schema));
generateFile(directory, new HollowPrimaryKeyIndexGenerator(packageName, apiClassname, classPostfix, (HollowObjectSchema)schema));
generateFile(directory, new HollowPrimaryKeyIndexGenerator(packageName, apiClassname, classPostfix, useAggressiveSubstitutions, (HollowObjectSchema)schema));
}
}
}
Expand All @@ -172,20 +182,20 @@ private HollowJavaFileGenerator getStaticAPIGenerator(HollowSchema schema) {

private HollowJavaFileGenerator getHollowObjectGenerator(HollowSchema schema) {
if(schema instanceof HollowObjectSchema) {
return new HollowObjectJavaGenerator(packageName, apiClassname, (HollowObjectSchema) schema, parameterizedTypes, parameterizeClassNames, classPostfix, getterPrefix);
return new HollowObjectJavaGenerator(packageName, apiClassname, (HollowObjectSchema) schema, parameterizedTypes, parameterizeClassNames, classPostfix, getterPrefix, useAggressiveSubstitutions);
} else if(schema instanceof HollowListSchema) {
return new HollowListJavaGenerator(packageName, apiClassname, (HollowListSchema) schema, parameterizedTypes, parameterizeClassNames, classPostfix);
return new HollowListJavaGenerator(packageName, apiClassname, (HollowListSchema) schema, parameterizedTypes, parameterizeClassNames, classPostfix, useAggressiveSubstitutions);
} else if(schema instanceof HollowSetSchema) {
return new HollowSetJavaGenerator(packageName, apiClassname, (HollowSetSchema) schema, parameterizedTypes, parameterizeClassNames, classPostfix);
return new HollowSetJavaGenerator(packageName, apiClassname, (HollowSetSchema) schema, parameterizedTypes, parameterizeClassNames, classPostfix, useAggressiveSubstitutions);
} else if(schema instanceof HollowMapSchema) {
return new HollowMapJavaGenerator(packageName, apiClassname, (HollowMapSchema) schema, dataset, parameterizedTypes, parameterizeClassNames, classPostfix);
return new HollowMapJavaGenerator(packageName, apiClassname, (HollowMapSchema) schema, dataset, parameterizedTypes, parameterizeClassNames, classPostfix, useAggressiveSubstitutions);
}

throw new UnsupportedOperationException("What kind of schema is a " + schema.getClass().getName() + "?");
}

private HollowFactoryJavaGenerator getHollowFactoryGenerator(HollowSchema schema) {
return new HollowFactoryJavaGenerator(packageName, schema, classPostfix);
return new HollowFactoryJavaGenerator(packageName, schema, classPostfix, useAggressiveSubstitutions);
}

public static class Builder {
Expand All @@ -196,6 +206,7 @@ public static class Builder {
private boolean parameterizeAllClassnames = false;
private String classPostfix = "";
private String getterPrefix = "";
private boolean useAggressiveSubstitutions = false;

public Builder withAPIClassname(String apiClassname) {
this.apiClassname = apiClassname;
Expand Down Expand Up @@ -232,6 +243,11 @@ public Builder withGetterPrefix(String getterPrefix) {
return this;
}

public Builder withAggressiveSubstitutions(boolean useAggressiveSubstitutions) {
this.useAggressiveSubstitutions = useAggressiveSubstitutions;
return this;
}

public HollowAPIGenerator build() {
if(apiClassname == null)
throw new IllegalStateException("Please specify an API classname (.withAPIClassname()) before calling .build()");
Expand All @@ -243,6 +259,7 @@ public HollowAPIGenerator build() {
HollowAPIGenerator generator = new HollowAPIGenerator(apiClassname, packageName, dataset, parameterizedTypes, parameterizeAllClassnames);
generator.setClassPostfix(classPostfix);
generator.setGetterPrefix(getterPrefix);
generator.setUseAggressiveSubstitutions(useAggressiveSubstitutions);
return generator;
}

Expand Down
Loading

0 comments on commit 842f045

Please sign in to comment.