Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored code to remove codeSmells #601

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,31 @@ public HollowUpdatePlan planUpdate(long currentVersion, long desiredVersion, boo
if (currentVersion == HollowConstants.VERSION_NONE)
return snapshotPlan(desiredVersion);

HollowUpdatePlan deltaPlan = deltaPlan(currentVersion, desiredVersion, doubleSnapshotConfig.maxDeltasBeforeDoubleSnapshot());
HollowUpdatePlan updatedPlan = deltaPlan(currentVersion, desiredVersion, doubleSnapshotConfig.maxDeltasBeforeDoubleSnapshot());

long deltaDestinationVersion = deltaPlan.destinationVersion(currentVersion);
long deltaDestinationVersion = updatedPlan.destinationVersion(currentVersion);

if(deltaDestinationVersion != desiredVersion && allowSnapshot) {
HollowUpdatePlan snapshotPlan = snapshotPlan(desiredVersion);
long snapshotDestinationVersion = snapshotPlan.destinationVersion(currentVersion);
if(deltaDestinationVersion == desiredVersion || !allowSnapshot)
return updatedPlan;

if(snapshotDestinationVersion == desiredVersion
|| ((deltaDestinationVersion > desiredVersion) && (snapshotDestinationVersion < desiredVersion))
|| ((snapshotDestinationVersion < desiredVersion) && (snapshotDestinationVersion > deltaDestinationVersion)))
HollowUpdatePlan snapshotPlan = snapshotPlan(desiredVersion);
long snapshotDestinationVersion = snapshotPlan.destinationVersion(currentVersion);

return snapshotPlan;

boolean isDesiredVersionUpgradedVersion = (deltaDestinationVersion > desiredVersion) && (snapshotDestinationVersion < desiredVersion);
boolean isSnapshotVersionUpgradedVersion = (snapshotDestinationVersion < desiredVersion) && (snapshotDestinationVersion > deltaDestinationVersion);
if(isDesiredVersionUpgradedVersion){
return snapshotPlan;
}
if(isSnapshotVersionUpgradedVersion){
return snapshotPlan;
}
if(snapshotDestinationVersion == desiredVersion)
{
return snapshotPlan;
}

return deltaPlan;
return updatedPlan;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,9 @@ public String toString() {
builder.append("]");
return builder.toString();
}

public String hollowImplGetClassName(String typeName){
return HollowCodeGenerationUtils.hollowImplClassname(typeName, getClassPostfix(),
isUseAggressiveSubstitutions(), isUseHollowPrimitiveTypes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public HollowConsumerJavaFileGenerator(String packageName, String subPackageName
}

protected String hollowImplClassname(String typeName) {
return HollowCodeGenerationUtils.hollowImplClassname(typeName, config.getClassPostfix(),
config.isUseAggressiveSubstitutions(), config.isUseHollowPrimitiveTypes());
return config.hollowImplGetClassName(typeName);
}

public String getSubPackageName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.netflix.hollow.api.codegen.delegate;

import com.netflix.hollow.core.schema.HollowObjectSchema;

import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.uppercase;

public class BooleanFieldAccessor extends FieldAccessor{

private final String fieldName;
public BooleanFieldAccessor(String fieldName){
super(HollowObjectSchema.FieldType.BOOLEAN, fieldName);
this.fieldName = fieldName;
}
@Override
public String generateGetCode() {
StringBuilder builder = new StringBuilder();
builder.append(" public boolean get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" if(").append(fieldName).append(" == null)\n");
builder.append(" return false;\n");
builder.append(" return ").append(fieldName).append(".booleanValue();\n");
builder.append(" }\n\n");
builder.append(" public Boolean get").append(uppercase(fieldName)).append("Boxed(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.netflix.hollow.api.codegen.delegate;

import com.netflix.hollow.core.schema.HollowObjectSchema;

import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.uppercase;

public class BytesFieldAccessor extends FieldAccessor{
private final String fieldName;
public BytesFieldAccessor(String fieldName){
super(HollowObjectSchema.FieldType.BYTES, fieldName);
this.fieldName = fieldName;
}


@Override
public String generateGetCode() {
StringBuilder builder = new StringBuilder();
builder.append(" public byte[] get").append(uppercase(fieldName)).append("(int ordinal) {\n");
// we need the cast to get around http://findbugs.sourceforge.net/bugDescriptions.html#EI_EXPOSE_REP
builder.append(" return (byte[]) ").append(fieldName).append(";\n");
builder.append(" }\n\n");
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.netflix.hollow.api.codegen.delegate;


import com.netflix.hollow.core.schema.HollowObjectSchema;

import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.uppercase;

public class DoubleFieldAccessor extends FieldAccessor {
private final String fieldName;
public DoubleFieldAccessor(String fieldName){
super(HollowObjectSchema.FieldType.DOUBLE, fieldName);
this.fieldName = fieldName;
}

@Override
public String generateGetCode() {
StringBuilder builder = new StringBuilder();
builder.append(" public double get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" if(").append(fieldName).append(" == null)\n");
builder.append(" return Double.NaN;\n");
builder.append(" return ").append(fieldName).append(".doubleValue();\n");
builder.append(" }\n\n");
builder.append(" public Double get").append(uppercase(fieldName)).append("Boxed(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.netflix.hollow.api.codegen.delegate;

import com.netflix.hollow.core.schema.HollowObjectSchema;

public abstract class FieldAccessor {

private final HollowObjectSchema.FieldType fieldType;
private final String fieldName;

public FieldAccessor(HollowObjectSchema.FieldType fieldType, String fieldName){
this.fieldType = fieldType;
this.fieldName = fieldName;
}
public abstract String generateGetCode();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.netflix.hollow.api.codegen.delegate;

import com.netflix.hollow.core.schema.HollowObjectSchema;

import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.uppercase;

public class FloatFieldAccessor extends FieldAccessor{

private final String fieldName;
public FloatFieldAccessor(String fieldName){
super(HollowObjectSchema.FieldType.FLOAT, fieldName);
this.fieldName = fieldName;
}
@Override
public String generateGetCode() {
StringBuilder builder = new StringBuilder();
builder.append(" public float get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" if(").append(fieldName).append(" == null)\n");
builder.append(" return Float.NaN;\n");
builder.append(" return ").append(fieldName).append(".floatValue();\n");
builder.append(" }\n\n");
builder.append(" public Float get").append(uppercase(fieldName)).append("Boxed(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
*/
package com.netflix.hollow.api.codegen.delegate;

import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.delegateCachedImplName;
import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.delegateInterfaceName;
import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.substituteInvalidChars;
import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.typeAPIClassname;
import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.uppercase;

import com.netflix.hollow.api.codegen.CodeGeneratorConfig;
import com.netflix.hollow.api.codegen.HollowAPIGenerator;
import com.netflix.hollow.api.codegen.HollowCodeGenerationUtils;
Expand All @@ -36,6 +30,8 @@
import com.netflix.hollow.core.schema.HollowObjectSchema;
import com.netflix.hollow.core.schema.HollowObjectSchema.FieldType;

import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.*;

/**
* This class contains template logic for generating a {@link HollowAPI} implementation. Not intended for external consumption.
*
Expand Down Expand Up @@ -175,76 +171,33 @@ public String generate() {
}

private void addAccessor(StringBuilder builder, FieldType fieldType, String fieldName) {
FieldAccessor fieldAccessor = null;
switch(fieldType) {
case BOOLEAN:
builder.append(" public boolean get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" if(").append(fieldName).append(" == null)\n");
builder.append(" return false;\n");
builder.append(" return ").append(fieldName).append(".booleanValue();\n");
builder.append(" }\n\n");
builder.append(" public Boolean get").append(uppercase(fieldName)).append("Boxed(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
fieldAccessor = new BooleanFieldAccessor(fieldName);
break;
case BYTES:
builder.append(" public byte[] get").append(uppercase(fieldName)).append("(int ordinal) {\n");
// we need the cast to get around http://findbugs.sourceforge.net/bugDescriptions.html#EI_EXPOSE_REP
builder.append(" return (byte[]) ").append(fieldName).append(";\n");
builder.append(" }\n\n");
fieldAccessor = new BytesFieldAccessor(fieldName);
break;
case DOUBLE:
builder.append(" public double get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" if(").append(fieldName).append(" == null)\n");
builder.append(" return Double.NaN;\n");
builder.append(" return ").append(fieldName).append(".doubleValue();\n");
builder.append(" }\n\n");
builder.append(" public Double get").append(uppercase(fieldName)).append("Boxed(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
fieldAccessor = new DoubleFieldAccessor(fieldName);
break;
case FLOAT:
builder.append(" public float get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" if(").append(fieldName).append(" == null)\n");
builder.append(" return Float.NaN;\n");
builder.append(" return ").append(fieldName).append(".floatValue();\n");
builder.append(" }\n\n");
builder.append(" public Float get").append(uppercase(fieldName)).append("Boxed(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
fieldAccessor = new FloatFieldAccessor(fieldName);
break;
case INT:
builder.append(" public int get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" if(").append(fieldName).append(" == null)\n");
builder.append(" return Integer.MIN_VALUE;\n");
builder.append(" return ").append(fieldName).append(".intValue();\n");
builder.append(" }\n\n");
builder.append(" public Integer get").append(uppercase(fieldName)).append("Boxed(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
fieldAccessor = new IntFieldAccessor(fieldName);
break;
case LONG:
builder.append(" public long get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" if(").append(fieldName).append(" == null)\n");
builder.append(" return Long.MIN_VALUE;\n");
builder.append(" return ").append(fieldName).append(".longValue();\n");
builder.append(" }\n\n");
builder.append(" public Long get").append(uppercase(fieldName)).append("Boxed(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
fieldAccessor = new LongFieldAccessor(fieldName);
break;
case STRING:
builder.append(" public String get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
builder.append(" public boolean is").append(uppercase(fieldName)).append("Equal(int ordinal, String testValue) {\n");
builder.append(" if(testValue == null)\n");
builder.append(" return ").append(fieldName).append(" == null;\n");
builder.append(" return testValue.equals(").append(fieldName).append(");\n");
builder.append(" }\n\n");
fieldAccessor = new StringFieldAccessor(fieldName);
break;
case REFERENCE:
throw new IllegalArgumentException();
}
builder.append(fieldAccessor.generateGetCode());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.netflix.hollow.api.codegen.delegate;

import com.netflix.hollow.core.schema.HollowObjectSchema;

import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.uppercase;

public class IntFieldAccessor extends FieldAccessor {


private final String fieldName;
public IntFieldAccessor(String fieldName){
super(HollowObjectSchema.FieldType.INT, fieldName);
this.fieldName = fieldName;
}
@Override
public String generateGetCode() {
StringBuilder builder = new StringBuilder();
builder.append(" public int get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" if(").append(fieldName).append(" == null)\n");
builder.append(" return Integer.MIN_VALUE;\n");
builder.append(" return ").append(fieldName).append(".intValue();\n");
builder.append(" }\n\n");
builder.append(" public Integer get").append(uppercase(fieldName)).append("Boxed(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.netflix.hollow.api.codegen.delegate;

import com.netflix.hollow.core.schema.HollowObjectSchema;

import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.uppercase;

public class LongFieldAccessor extends FieldAccessor{

private final String fieldName;
public LongFieldAccessor(String fieldName){
super(HollowObjectSchema.FieldType.LONG, fieldName);
this.fieldName = fieldName;
}
@Override
public String generateGetCode() {
StringBuilder builder = new StringBuilder();
builder.append(" public long get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" if(").append(fieldName).append(" == null)\n");
builder.append(" return Long.MIN_VALUE;\n");
builder.append(" return ").append(fieldName).append(".longValue();\n");
builder.append(" }\n\n");
builder.append(" public Long get").append(uppercase(fieldName)).append("Boxed(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.netflix.hollow.api.codegen.delegate;

import com.netflix.hollow.core.schema.HollowObjectSchema;

import static com.netflix.hollow.api.codegen.HollowCodeGenerationUtils.uppercase;

public class StringFieldAccessor extends FieldAccessor{


private final String fieldName;
public StringFieldAccessor(String fieldName){
super(HollowObjectSchema.FieldType.STRING, fieldName);
this.fieldName = fieldName;
}
@Override
public String generateGetCode() {
StringBuilder builder = new StringBuilder();
builder.append(" public String get").append(uppercase(fieldName)).append("(int ordinal) {\n");
builder.append(" return ").append(fieldName).append(";\n");
builder.append(" }\n\n");
builder.append(" public boolean is").append(uppercase(fieldName)).append("Equal(int ordinal, String testValue) {\n");
builder.append(" if(testValue == null)\n");
builder.append(" return ").append(fieldName).append(" == null;\n");
builder.append(" return testValue.equals(").append(fieldName).append(");\n");
builder.append(" }\n\n");
return builder.toString();
}
}
Loading