LocalDateFormatter.java

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.LocalDate;
23
import java.time.format.DateTimeFormatter;
24
import java.time.format.DateTimeParseException;
25
26
/**
27
 * Formatter for {@link java.time.LocalDate} 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 yyyyMMdd}.
31
 *
32
 * @author Jacob von Eyben - <a href="https://eybenconsult.com">https://eybenconsult.com</a>
33
 * @since 1.4.0
34
 */
35
public class LocalDateFormatter extends AbstractPatternFormatter<LocalDate> {
36
37
  @Override
38
  protected int computeFormattedLengthForPattern(String pattern) {
39 1 1. computeFormattedLengthForPattern : replaced int return with 0 for com/ancientprogramming/fixedformat4j/format/impl/LocalDateFormatter::computeFormattedLengthForPattern → SURVIVED
    return DateTimeFormatter.ofPattern(pattern).format(LocalDate.of(1970, 1, 1)).length();
40
  }
41
42
  /** {@inheritDoc} */
43
  public LocalDate 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/LocalDateFormatter::asObject → KILLED
      return LocalDate.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, LocalDate.class.getName()));
52
    }
53
  }
54
55
  /** {@inheritDoc} */
56
  public String asString(LocalDate date, FormatInstructions instructions) {
57 1 1. asString : negated conditional → KILLED
    if (date == null) {
58 1 1. asString : replaced return value with "" for com/ancientprogramming/fixedformat4j/format/impl/LocalDateFormatter::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/LocalDateFormatter::asString → KILLED
    return DateTimeFormatter.ofPattern(pattern).format(date);
62
  }
63
}

Mutations

39

1.1
Location : computeFormattedLengthForPattern
Killed by : none
replaced int return with 0 for com/ancientprogramming/fixedformat4j/format/impl/LocalDateFormatter::computeFormattedLengthForPattern → SURVIVED
Covering tests

44

1.1
Location : asObject
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestLocalDateFormatter.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestLocalDateFormatter]/[method:testAllSpaceInputParsesToNull()]
negated conditional → KILLED

49

1.1
Location : asObject
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestLocalDateFormatter.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestLocalDateFormatter]/[method:testParseDefaultPattern()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/LocalDateFormatter::asObject → KILLED

57

1.1
Location : asString
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestLocalDateFormatter.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestLocalDateFormatter]/[method:testNullFormatReturnsSpaces()]
negated conditional → KILLED

58

1.1
Location : asString
Killed by : none
replaced return value with "" for com/ancientprogramming/fixedformat4j/format/impl/LocalDateFormatter::asString → SURVIVED
Covering tests

61

1.1
Location : asString
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestLocalDateFormatter.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestLocalDateFormatter]/[method:testFormatAlternativePattern()]
replaced return value with "" for com/ancientprogramming/fixedformat4j/format/impl/LocalDateFormatter::asString → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.1