• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.47
today-context/src/main/java/infra/format/support/StringToPeriodConverter.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.format.support;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.time.Period;
23
import java.time.temporal.ChronoUnit;
24
import java.util.Collections;
25
import java.util.Set;
26

27
import infra.core.TypeDescriptor;
28
import infra.core.conversion.Converter;
29
import infra.core.conversion.GenericConverter;
30
import infra.format.annotation.PeriodFormat;
31
import infra.format.annotation.PeriodStyle;
32
import infra.format.annotation.PeriodUnit;
33
import infra.util.ObjectUtils;
34

35
/**
36
 * {@link Converter} to convert from a {@link String} to a {@link Period}. Supports
37
 * {@link Period#parse(CharSequence)} as well a more readable form.
38
 *
39
 * @author Eddú Meléndez
40
 * @author Edson Chávez
41
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
42
 * @see PeriodFormat
43
 * @see PeriodUnit
44
 * @since 4.0
45
 */
46
final class StringToPeriodConverter implements GenericConverter {
3✔
47

48
  @Override
49
  public Set<GenericConverter.ConvertiblePair> getConvertibleTypes() {
50
    return Collections.singleton(new GenericConverter.ConvertiblePair(String.class, Period.class));
7✔
51
  }
52

53
  @Nullable
54
  @Override
55
  public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
56
    if (ObjectUtils.isEmpty(source)) {
3!
57
      return null;
×
58
    }
59
    return convert(source.toString(), getStyle(targetType), getPeriodUnit(targetType));
11✔
60
  }
61

62
  @Nullable
63
  private PeriodStyle getStyle(TypeDescriptor targetType) {
64
    PeriodFormat annotation = targetType.getAnnotation(PeriodFormat.class);
5✔
65
    return (annotation != null) ? annotation.value() : null;
7✔
66
  }
67

68
  @Nullable
69
  private ChronoUnit getPeriodUnit(TypeDescriptor targetType) {
70
    PeriodUnit annotation = targetType.getAnnotation(PeriodUnit.class);
5✔
71
    return (annotation != null) ? annotation.value() : null;
7✔
72
  }
73

74
  private Period convert(String source, @Nullable PeriodStyle style, @Nullable ChronoUnit unit) {
75
    style = (style != null) ? style : PeriodStyle.detect(source);
7✔
76
    return style.parse(source, unit);
5✔
77
  }
78

79
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc