
br.com.objectos.io.compiler.LocalDateFormatMethod Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2015 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.io.compiler;
import java.util.Optional;
import br.com.objectos.code.AnnotationInfo;
import br.com.objectos.code.MethodInfo;
import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.io.flat.annotation.LocalDatePattern;
import br.com.objectos.io.flat.annotation.WhenAbsent;
/**
* @author [email protected] (Marcio Endo)
*/
abstract class LocalDateFormatMethod extends FieldMethod {
LocalDateFormatMethod(MethodInfo methodInfo, AnnotationInfo fieldAnnotation) {
super(methodInfo, fieldAnnotation);
}
public static FieldMethod code(MethodInfo methodInfo, AnnotationInfo fieldAnnotation) {
SimpleTypeInfo returnTypeInfo = methodInfo.returnTypeInfo();
return returnTypeInfo.isInfoOf(Optional.class)
? OptionalMethod.code(methodInfo, fieldAnnotation, returnTypeInfo)
: StandardMethod.code(methodInfo, fieldAnnotation, returnTypeInfo);
}
LocalDatePattern pattern() {
return fieldAnnotationEnumValue("value", LocalDatePattern.class);
}
private static class OptionalMethod extends LocalDateFormatMethod {
private final Optional whenAbsentAnnotation;
private OptionalMethod(MethodInfo methodInfo,
AnnotationInfo fieldAnnotation,
Optional whenAbsentAnnotation) {
super(methodInfo, fieldAnnotation);
this.whenAbsentAnnotation = whenAbsentAnnotation;
}
private static FieldMethod code(MethodInfo methodInfo, AnnotationInfo fieldAnnotation, SimpleTypeInfo returnTypeInfo) {
AnnotationInfo nullable = methodInfo
.annotationInfo(WhenAbsent.class)
.orElse(null);
Optional whenAbsentAnnotation = Optional.ofNullable(nullable);
return new OptionalMethod(methodInfo, fieldAnnotation, whenAbsentAnnotation);
}
@Override
void recordReaderCode(Body body) {
LocalDatePattern pattern = pattern();
String defaultValue = defaultValue();
body.add(".localDate($T.$L, $S)", LocalDatePattern.class, pattern, defaultValue);
}
@Override
void recordWriterCode(Body body) {
LocalDatePattern pattern = pattern();
String defaultValue = defaultValue();
body.add(".localDate($L, $T.$L).or($S)", methodInfo.fieldName(), LocalDatePattern.class, pattern, defaultValue);
}
private String defaultValue() {
return whenAbsentAnnotation.get()
.stringValue("value")
.get();
}
}
private static class StandardMethod extends LocalDateFormatMethod {
private StandardMethod(MethodInfo methodInfo, AnnotationInfo fieldAnnotation) {
super(methodInfo, fieldAnnotation);
}
private static FieldMethod code(MethodInfo methodInfo, AnnotationInfo fieldAnnotation, SimpleTypeInfo returnTypeInfo) {
return new StandardMethod(methodInfo, fieldAnnotation);
}
@Override
void recordReaderCode(Body body) {
LocalDatePattern pattern = pattern();
body.add(".localDate($T.$L)", LocalDatePattern.class, pattern);
}
@Override
void recordWriterCode(Body body) {
LocalDatePattern pattern = pattern();
body.add(".localDate($L, $T.$L)", methodInfo.fieldName(), LocalDatePattern.class, pattern);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy