Optimize singleton nullables
This commit is contained in:
parent
5f29fbafa2
commit
2c9e69dba2
2
pom.xml
2
pom.xml
@ -245,7 +245,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>it.cavallium</groupId>
|
<groupId>it.cavallium</groupId>
|
||||||
<artifactId>data-generator-runtime</artifactId>
|
<artifactId>data-generator-runtime</artifactId>
|
||||||
<version>[1.0.32,)</version>
|
<version>1.0.44</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
@ -874,6 +874,9 @@ public class SourcesGenerator {
|
|||||||
"INullableIType"
|
"INullableIType"
|
||||||
));
|
));
|
||||||
nullableTypeClass.addSuperinterface(IGenericNullable.class);
|
nullableTypeClass.addSuperinterface(IGenericNullable.class);
|
||||||
|
var nullInstance = FieldSpec.builder(nullableTypeType, "NULL", Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL);
|
||||||
|
nullInstance.initializer("new $T(null)", nullableTypeType);
|
||||||
|
nullableTypeClass.addField(nullInstance.build());
|
||||||
var valueField = ParameterSpec.builder(typeType, "value");
|
var valueField = ParameterSpec.builder(typeType, "value");
|
||||||
nullableTypeClass.addRecordComponent(valueField.build());
|
nullableTypeClass.addRecordComponent(valueField.build());
|
||||||
var ofMethod = MethodSpec.methodBuilder("of");
|
var ofMethod = MethodSpec.methodBuilder("of");
|
||||||
@ -883,10 +886,10 @@ public class SourcesGenerator {
|
|||||||
ofMethod.addException(NullPointerException.class);
|
ofMethod.addException(NullPointerException.class);
|
||||||
ofMethod.returns(nullableClassType);
|
ofMethod.returns(nullableClassType);
|
||||||
ofMethod.addParameter(ParameterSpec.builder(typeType, "value").build());
|
ofMethod.addParameter(ParameterSpec.builder(typeType, "value").build());
|
||||||
ofMethod.beginControlFlow("if (value == null)");
|
ofMethod.beginControlFlow("if (value != null)");
|
||||||
ofMethod.addStatement("throw new $T()", NullPointerException.class);
|
|
||||||
ofMethod.nextControlFlow("else");
|
|
||||||
ofMethod.addStatement("return new $T(value)", nullableTypeType);
|
ofMethod.addStatement("return new $T(value)", nullableTypeType);
|
||||||
|
ofMethod.nextControlFlow("else");
|
||||||
|
ofMethod.addStatement("throw new $T()", NullPointerException.class);
|
||||||
ofMethod.endControlFlow();
|
ofMethod.endControlFlow();
|
||||||
nullableTypeClass.addMethod(ofMethod.build());
|
nullableTypeClass.addMethod(ofMethod.build());
|
||||||
var ofNullableMethod = MethodSpec.methodBuilder("ofNullable");
|
var ofNullableMethod = MethodSpec.methodBuilder("ofNullable");
|
||||||
@ -895,14 +898,18 @@ public class SourcesGenerator {
|
|||||||
ofNullableMethod.addModifiers(Modifier.FINAL);
|
ofNullableMethod.addModifiers(Modifier.FINAL);
|
||||||
ofNullableMethod.returns(nullableClassType);
|
ofNullableMethod.returns(nullableClassType);
|
||||||
ofNullableMethod.addParameter(ParameterSpec.builder(typeType, "value").build());
|
ofNullableMethod.addParameter(ParameterSpec.builder(typeType, "value").build());
|
||||||
|
ofNullableMethod.beginControlFlow("if (value != null)");
|
||||||
ofNullableMethod.addStatement("return new $T(value)", nullableTypeType);
|
ofNullableMethod.addStatement("return new $T(value)", nullableTypeType);
|
||||||
|
ofNullableMethod.nextControlFlow("else");
|
||||||
|
ofNullableMethod.addStatement("return NULL");
|
||||||
|
ofNullableMethod.endControlFlow();
|
||||||
nullableTypeClass.addMethod(ofNullableMethod.build());
|
nullableTypeClass.addMethod(ofNullableMethod.build());
|
||||||
var emptyMethod = MethodSpec.methodBuilder("empty");
|
var emptyMethod = MethodSpec.methodBuilder("empty");
|
||||||
emptyMethod.addModifiers(Modifier.PUBLIC);
|
emptyMethod.addModifiers(Modifier.PUBLIC);
|
||||||
emptyMethod.addModifiers(Modifier.STATIC);
|
emptyMethod.addModifiers(Modifier.STATIC);
|
||||||
emptyMethod.addModifiers(Modifier.FINAL);
|
emptyMethod.addModifiers(Modifier.FINAL);
|
||||||
emptyMethod.returns(nullableClassType);
|
emptyMethod.returns(nullableClassType);
|
||||||
emptyMethod.addStatement("return new $T(null)", nullableTypeType);
|
emptyMethod.addStatement("return NULL");
|
||||||
nullableTypeClass.addMethod(emptyMethod.build());
|
nullableTypeClass.addMethod(emptyMethod.build());
|
||||||
var isEmptyMethod = MethodSpec.methodBuilder("isEmpty");
|
var isEmptyMethod = MethodSpec.methodBuilder("isEmpty");
|
||||||
isEmptyMethod.addModifiers(Modifier.PUBLIC);
|
isEmptyMethod.addModifiers(Modifier.PUBLIC);
|
||||||
@ -1340,6 +1347,7 @@ public class SourcesGenerator {
|
|||||||
.getData()
|
.getData()
|
||||||
.get(newDataTransformation.to);
|
.get(newDataTransformation.to);
|
||||||
TypeName newType = nextVersionTypeTypes.get(newTypeName);
|
TypeName newType = nextVersionTypeTypes.get(newTypeName);
|
||||||
|
Objects.requireNonNull(newType, () -> "Type \"" + newTypeName + "\" is not present from next version " + version + " to version " + nextVersion.get() + " in upgrader " + newDataTransformation.transformClass + "." + newDataTransformation.to);
|
||||||
TypeName newTypeBoxed = newType.isPrimitive() ? newType.box() : newType;
|
TypeName newTypeBoxed = newType.isPrimitive() ? newType.box() : newType;
|
||||||
{
|
{
|
||||||
currentVarNumber.addTo(newDataTransformation.to, 1);
|
currentVarNumber.addTo(newDataTransformation.to, 1);
|
||||||
@ -2237,8 +2245,17 @@ public class SourcesGenerator {
|
|||||||
returnOfNewInstanceStamentBuilder.add(")");
|
returnOfNewInstanceStamentBuilder.add(")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (first) {
|
||||||
|
typeClass.addField(FieldSpec
|
||||||
|
.builder(typeTypes.get(type), "INSTANCE", Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
|
||||||
|
.initializer("new $T()", typeTypes.get(type))
|
||||||
|
.build());
|
||||||
|
mapConstructor.addStatement("return INSTANCE");
|
||||||
|
ofConstructor.addStatement("return INSTANCE");
|
||||||
|
} else {
|
||||||
mapConstructor.addStatement(returnMapNewInstanceStamentBuilder.add(")").build());
|
mapConstructor.addStatement(returnMapNewInstanceStamentBuilder.add(")").build());
|
||||||
ofConstructor.addStatement(returnOfNewInstanceStamentBuilder.add(")").build());
|
ofConstructor.addStatement(returnOfNewInstanceStamentBuilder.add(")").build());
|
||||||
|
}
|
||||||
typeClass.addMethod(mapConstructor.build());
|
typeClass.addMethod(mapConstructor.build());
|
||||||
typeClass.addMethod(ofConstructor.build());
|
typeClass.addMethod(ofConstructor.build());
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user