| 1 | /* | |
| 2 | * Copyright 2004 the original author or authors. | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package com.ancientprogramming.fixedformat4j.format.impl; | |
| 17 | ||
| 18 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException; | |
| 19 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; | |
| 20 | import org.apache.commons.lang3.StringUtils; | |
| 21 | ||
| 22 | import java.time.LocalDateTime; | |
| 23 | import java.time.format.DateTimeFormatter; | |
| 24 | import java.time.format.DateTimeParseException; | |
| 25 | ||
| 26 | /** | |
| 27 | * Formatter for {@link java.time.LocalDateTime} data. | |
| 28 | * The formatting and parsing is performed using {@link DateTimeFormatter}. | |
| 29 | * The pattern is configured via {@link com.ancientprogramming.fixedformat4j.annotation.FixedFormatPattern}. | |
| 30 | * The default pattern is {@code yyyy-MM-dd'T'HH:mm:ss} (ISO-8601 local date-time). | |
| 31 | * | |
| 32 | * @author Jacob von Eyben - <a href="https://eybenconsult.com">https://eybenconsult.com</a> | |
| 33 | * @since 1.6.0 | |
| 34 | */ | |
| 35 | public class LocalDateTimeFormatter extends AbstractPatternFormatter<LocalDateTime> { | |
| 36 | ||
| 37 | @Override | |
| 38 | protected int computeFormattedLengthForPattern(String pattern) { | |
| 39 |
1
1. computeFormattedLengthForPattern : replaced int return with 0 for com/ancientprogramming/fixedformat4j/format/impl/LocalDateTimeFormatter::computeFormattedLengthForPattern → SURVIVED |
return DateTimeFormatter.ofPattern(pattern).format(LocalDateTime.of(1970, 1, 1, 0, 0, 0)).length(); |
| 40 | } | |
| 41 | ||
| 42 | /** {@inheritDoc} */ | |
| 43 | public LocalDateTime asObject(String string, FormatInstructions instructions) throws FixedFormatException { | |
| 44 |
1
1. asObject : negated conditional → KILLED |
if (StringUtils.isEmpty(string)) { |
| 45 | return null; | |
| 46 | } | |
| 47 | String pattern = instructions.getFixedFormatPatternData().getPattern(); | |
| 48 | try { | |
| 49 |
1
1. asObject : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/LocalDateTimeFormatter::asObject → KILLED |
return LocalDateTime.parse(string, DateTimeFormatter.ofPattern(pattern)); |
| 50 | } catch (DateTimeParseException e) { | |
| 51 | throw new FixedFormatException(String.format("Could not parse value[%s] by pattern[%s] to %s", string, pattern, LocalDateTime.class.getName())); | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 | /** {@inheritDoc} */ | |
| 56 | public String asString(LocalDateTime dateTime, FormatInstructions instructions) { | |
| 57 |
1
1. asString : negated conditional → KILLED |
if (dateTime == null) { |
| 58 |
1
1. asString : replaced return value with "" for com/ancientprogramming/fixedformat4j/format/impl/LocalDateTimeFormatter::asString → SURVIVED |
return null; |
| 59 | } | |
| 60 | String pattern = instructions.getFixedFormatPatternData().getPattern(); | |
| 61 |
1
1. asString : replaced return value with "" for com/ancientprogramming/fixedformat4j/format/impl/LocalDateTimeFormatter::asString → KILLED |
return DateTimeFormatter.ofPattern(pattern).format(dateTime); |
| 62 | } | |
| 63 | } | |
Mutations | ||
| 39 |
1.1 |
|
| 44 |
1.1 |
|
| 49 |
1.1 |
|
| 57 |
1.1 |
|
| 58 |
1.1 |
|
| 61 |
1.1 |