DateField.java

  1. /*
  2.  * Licensed under the Apache License, Version 2.0 (the "License");
  3.  * you may not use this file except in compliance with the License.
  4.  * You may obtain a copy of the License at
  5.  *
  6.  * http://www.apache.org/licenses/LICENSE-2.0
  7.  *
  8.  * Unless required by applicable law or agreed to in writing, software
  9.  * distributed under the License is distributed on an "AS IS" BASIS,
  10.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11.  * See the License for the specific language governing permissions and
  12.  * limitations under the License.
  13.  */

  14. package org.basepom.mojo.propertyhelper.fields;

  15. import org.basepom.mojo.propertyhelper.Field;
  16. import org.basepom.mojo.propertyhelper.FieldContext;
  17. import org.basepom.mojo.propertyhelper.ValueProvider;
  18. import org.basepom.mojo.propertyhelper.definitions.DateDefinition;

  19. import java.time.ZoneId;
  20. import java.time.ZonedDateTime;
  21. import java.time.temporal.ChronoUnit;
  22. import java.util.StringJoiner;

  23. import com.google.common.annotations.VisibleForTesting;


  24. public final class DateField extends Field<ZonedDateTime, DateDefinition> {

  25.     private final ValueProvider valueProvider;
  26.     private final ZoneId timezone;

  27.     @VisibleForTesting
  28.     public static DateField forTesting(DateDefinition dateDefinition, ValueProvider valueProvider) {
  29.         return new DateField(dateDefinition, valueProvider, FieldContext.forTesting());
  30.     }

  31.     public DateField(final DateDefinition dateDefinition, final ValueProvider valueProvider, FieldContext fieldContext) {
  32.         super(dateDefinition, fieldContext);

  33.         this.valueProvider = valueProvider;
  34.         this.timezone = dateDefinition.getTimezone();
  35.     }

  36.     @Override
  37.     public String getFieldName() {
  38.         return fieldDefinition.getId();
  39.     }

  40.     @Override
  41.     public String getValue() {
  42.         ZonedDateTime date = valueProvider.getValue()
  43.             .map(value -> fieldDefinition.getParser().apply(value))
  44.             .orElseGet(() -> fieldDefinition.getValue()
  45.                 .map(fieldDefinition.getLongParser())
  46.                 .orElseGet(this::now));

  47.         String result = formatResult(date);

  48.         if (fieldDefinition.getFormatter().isPresent()) {
  49.             // format was set, store time in the chosen format
  50.             valueProvider.setValue(result);
  51.         } else {
  52.             // not format was set. Store time as millis.
  53.             valueProvider.setValue(Long.toString(date.toInstant().toEpochMilli()));
  54.         }

  55.         return result;
  56.     }

  57.     @Override
  58.     public boolean isExposeAsProperty() {
  59.         return fieldDefinition.isExport();
  60.     }

  61.     private ZonedDateTime now() {
  62.         // code only saves in millisecond precision.
  63.         return ZonedDateTime.now(timezone).truncatedTo(ChronoUnit.MILLIS);
  64.     }

  65.     @Override
  66.     public String toString() {
  67.         return new StringJoiner(", ", DateField.class.getSimpleName() + "[", "]")
  68.             .add("valueProvider=" + valueProvider)
  69.             .toString();
  70.     }
  71. }