Skip to content

Commit

Permalink
Generate a basic HollowAPIFactory implementation as part of a generat…
Browse files Browse the repository at this point in the history
…ed API
  • Loading branch information
dkoszewnik committed Dec 13, 2016
1 parent df6d109 commit 8486833
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,14 @@
import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.typeAPIClassname;

import com.netflix.hollow.api.custom.HollowAPI;

import com.netflix.hollow.core.util.AllHollowRecordCollection;
import com.netflix.hollow.core.schema.HollowSchemaSorter;
import com.netflix.hollow.core.schema.HollowListSchema;
import com.netflix.hollow.core.schema.HollowMapSchema;
import com.netflix.hollow.core.schema.HollowObjectSchema;
import com.netflix.hollow.core.schema.HollowSchema;
import com.netflix.hollow.core.schema.HollowSetSchema;
import com.netflix.hollow.core.HollowDataset;
import com.netflix.hollow.api.objects.provider.HollowFactory;
import com.netflix.hollow.api.objects.provider.HollowObjectCacheProvider;
import com.netflix.hollow.api.objects.provider.HollowObjectFactoryProvider;
import com.netflix.hollow.api.objects.provider.HollowObjectProvider;
import com.netflix.hollow.api.sampling.HollowObjectCreationSampler;
import com.netflix.hollow.api.sampling.HollowSamplingDirector;
import com.netflix.hollow.api.sampling.SampleResult;
import com.netflix.hollow.core.HollowDataset;
import com.netflix.hollow.core.read.dataaccess.HollowDataAccess;
import com.netflix.hollow.core.read.dataaccess.HollowListTypeDataAccess;
import com.netflix.hollow.core.read.dataaccess.HollowMapTypeDataAccess;
Expand All @@ -50,6 +42,9 @@
import com.netflix.hollow.core.read.dataaccess.missing.HollowMapMissingDataAccess;
import com.netflix.hollow.core.read.dataaccess.missing.HollowObjectMissingDataAccess;
import com.netflix.hollow.core.read.dataaccess.missing.HollowSetMissingDataAccess;
import com.netflix.hollow.core.schema.HollowSchema;
import com.netflix.hollow.core.schema.HollowSchemaSorter;
import com.netflix.hollow.core.util.AllHollowRecordCollection;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -60,9 +55,6 @@
* This class contains template logic for generating a {@link HollowAPI} implementation. Not intended for external consumption.
*
* @see HollowAPIGenerator
*
* @author dkoszewnik
*
*/
public class HollowAPIClassJavaGenerator implements HollowJavaFileGenerator {

Expand Down Expand Up @@ -120,7 +112,7 @@ public String generate() {


builder.append("\n@SuppressWarnings(\"all\")\n");
builder.append("\npublic class ").append(className).append(" extends HollowAPI {\n\n");
builder.append("public class ").append(className).append(" extends HollowAPI {\n\n");

builder.append(" private final HollowObjectCreationSampler objectCreationSampler;\n\n");

Expand Down Expand Up @@ -247,15 +239,18 @@ public String generate() {
}

private String schemaType(HollowSchema schema) {
if(schema instanceof HollowObjectSchema)
switch(schema.getSchemaType()) {
case OBJECT:
return "Object";
if(schema instanceof HollowMapSchema)
return "Map";
if(schema instanceof HollowSetSchema)
return "Set";
if(schema instanceof HollowListSchema)
case LIST:
return "List";
throw new IllegalArgumentException();
case SET:
return "Set";
case MAP:
return "Map";
default:
throw new IllegalArgumentException();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
*
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.hollow.api.codegen;

import com.netflix.hollow.api.client.HollowAPIFactory;
import com.netflix.hollow.api.custom.HollowAPI;
import com.netflix.hollow.api.objects.provider.HollowFactory;
import com.netflix.hollow.core.read.dataaccess.HollowDataAccess;
import java.util.Collections;
import java.util.Set;

/**
* This class contains template logic for generating a {@link HollowAPIFactory} implementation. Not intended for external consumption.
*
* @see HollowAPIGenerator
*
*/
public class HollowAPIFactoryJavaGenerator implements HollowJavaFileGenerator {

private final String packageName;
private final String className;
private final String apiClassname;

public HollowAPIFactoryJavaGenerator(String packageName, String apiClassname) {
this.packageName = packageName;
this.apiClassname = apiClassname;
this.className = apiClassname + "Factory";
}

@Override
public String getClassName() {
return className;
}

@Override
public String generate() {
StringBuilder builder = new StringBuilder();

if(!"".equals(packageName)) {
builder.append("package ").append(packageName).append(";\n\n");
}

builder.append("import ").append(HollowAPIFactory.class.getName()).append(";\n");
builder.append("import ").append(HollowAPI.class.getName()).append(";\n");
builder.append("import ").append(HollowFactory.class.getName()).append(";\n");
builder.append("import ").append(HollowDataAccess.class.getName()).append(";\n");
builder.append("import ").append(Collections.class.getName()).append(";\n");
builder.append("import ").append(Set.class.getName()).append(";\n");


builder.append("\npublic class ").append(className).append(" implements HollowAPIFactory {\n\n");

builder.append(" private final Set<String> cachedTypes;\n\n");

builder.append(" public ").append(className).append("() {\n");
builder.append(" this(Collections.<String>emptySet());\n");
builder.append(" }\n\n");

builder.append(" public ").append(className).append("(Set<String> cachedTypes) {\n");
builder.append(" this.cachedTypes = cachedTypes;\n");
builder.append(" }\n\n");

builder.append(" @Override\n");
builder.append(" public HollowAPI createAPI(HollowDataAccess dataAccess) {\n");
builder.append(" return new ").append(apiClassname).append("(dataAccess);\n");
builder.append(" }\n\n");

builder.append(" @Override\n");
builder.append(" public HollowAPI createAPI(HollowDataAccess dataAccess, HollowAPI previousCycleAPI) {\n");
builder.append(" return new ").append(apiClassname).append("(dataAccess, cachedTypes, Collections.<String, HollowFactory<?>>emptyMap(), (").append(apiClassname).append(") previousCycleAPI);\n");
builder.append(" }\n\n");

builder.append("}");

return builder.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ public void generateFiles(String directory) throws IOException {

public void generateFiles(File directory) throws IOException {
HollowAPIClassJavaGenerator apiClassGenerator = new HollowAPIClassJavaGenerator(packageName, apiClassname, dataset, parameterizeClassNames);
HollowAPIFactoryJavaGenerator apiFactoryGenerator = new HollowAPIFactoryJavaGenerator(packageName, apiClassname);

generateFile(directory, apiClassGenerator);
generateFile(directory, apiFactoryGenerator);

generateFilesForHollowSchemas(directory);
}
Expand Down

0 comments on commit 8486833

Please sign in to comment.