
br.com.objectos.flat.LocalDateTimeFormatMethod Maven / Gradle / Ivy
/*
* Copyright 2016 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.flat;
import java.util.Optional;
import br.com.objectos.code.AnnotationInfo;
import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.flat.pojo.WhenAbsent;
import br.com.objectos.pojo.plugin.Property;
/**
* @author [email protected] (Marcio Endo)
*/
abstract class LocalDateTimeFormatMethod extends FieldMethod {
LocalDateTimeFormatMethod(Property property, AnnotationInfo fieldAnnotation) {
super(property, fieldAnnotation);
}
public static FieldMethod code(Property property, AnnotationInfo fieldAnnotation) {
SimpleTypeInfo returnTypeInfo = property.returnTypeInfo();
return returnTypeInfo.isInfoOf(Optional.class)
? OptionalMethod.code(property, fieldAnnotation, returnTypeInfo)
: StandardMethod.code(property, fieldAnnotation, returnTypeInfo);
}
@Override
public int length() {
return pattern().length();
}
LocalDateTimePattern pattern() {
return fieldAnnotationEnumValue("value", LocalDateTimePattern.class);
}
private static class OptionalMethod extends LocalDateTimeFormatMethod {
private final Optional whenAbsentAnnotation;
private OptionalMethod(Property property,
AnnotationInfo fieldAnnotation,
Optional whenAbsentAnnotation) {
super(property, fieldAnnotation);
this.whenAbsentAnnotation = whenAbsentAnnotation;
}
private static FieldMethod code(Property property, AnnotationInfo fieldAnnotation, SimpleTypeInfo returnTypeInfo) {
AnnotationInfo nullable = property
.annotationInfo(WhenAbsent.class)
.orElse(null);
Optional whenAbsentAnnotation = Optional.ofNullable(nullable);
return new OptionalMethod(property, fieldAnnotation, whenAbsentAnnotation);
}
@Override
void recordReaderCode(Body body) {
LocalDateTimePattern pattern = pattern();
String defaultValue = defaultValue();
body.add(".localDateTime($T.$L, $S)", LocalDateTimePattern.class, pattern, defaultValue);
}
@Override
void recordWriterCode(Body body) {
LocalDateTimePattern pattern = pattern();
String defaultValue = defaultValue();
body.add(".localDateTime($L, $T.$L).or($S)", property.name(), LocalDateTimePattern.class, pattern, defaultValue);
}
private String defaultValue() {
return whenAbsentAnnotation.get()
.stringValue("value")
.get();
}
}
private static class StandardMethod extends LocalDateTimeFormatMethod {
private StandardMethod(Property property, AnnotationInfo fieldAnnotation) {
super(property, fieldAnnotation);
}
private static FieldMethod code(Property property, AnnotationInfo fieldAnnotation, SimpleTypeInfo returnTypeInfo) {
return new StandardMethod(property, fieldAnnotation);
}
@Override
void recordReaderCode(Body body) {
LocalDateTimePattern pattern = pattern();
body.add(".localDateTime($T.$L)", LocalDateTimePattern.class, pattern);
}
@Override
void recordWriterCode(Body body) {
LocalDateTimePattern pattern = pattern();
body.add(".localDateTime($L, $T.$L)", property.name(), LocalDateTimePattern.class, pattern);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy