All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.flat.LocalDateFormatMethod Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show 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.way.flat;

import java.util.Optional;

import br.com.objectos.way.code.AnnotationInfo;
import br.com.objectos.way.code.SimpleTypeInfo;
import br.com.objectos.way.flat.LocalDatePattern;
import br.com.objectos.way.flat.WhenAbsent;
import br.com.objectos.way.pojo.plugin.Property;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class LocalDateFormatMethod extends FieldMethod {

  LocalDateFormatMethod(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();
  }

  LocalDatePattern pattern() {
    return fieldAnnotationEnumValue("value", LocalDatePattern.class);
  }

  private static class OptionalMethod extends LocalDateFormatMethod {

    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 flatEntityConstructor(FlatEntityConstructor constructor) {
      LocalDatePattern pattern = pattern();
      String defaultValue = defaultValue();
      constructor.add(".localDate($T.$L, $S)", LocalDatePattern.class, pattern, defaultValue);
    }

    @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, $S)", property.name(), LocalDatePattern.class, pattern, defaultValue);
    }

    private String defaultValue() {
      return whenAbsentAnnotation.get()
          .stringValue("value")
          .get();
    }

  }

  private static class StandardMethod extends LocalDateFormatMethod {

    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 flatEntityConstructor(FlatEntityConstructor constructor) {
      LocalDatePattern pattern = pattern();
      constructor.add(".localDate($T.$L)", LocalDatePattern.class, pattern);
    }

    @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)", property.name(), LocalDatePattern.class, pattern);
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy