• 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

82.14
today-context/src/main/java/infra/format/support/DurationToNumberConverter.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.Duration;
23
import java.util.Collections;
24
import java.util.Set;
25

26
import infra.core.TypeDescriptor;
27
import infra.core.conversion.Converter;
28
import infra.core.conversion.GenericConverter;
29
import infra.format.annotation.DurationFormat;
30
import infra.format.annotation.DurationFormat.Unit;
31
import infra.format.annotation.DurationUnit;
32
import infra.util.ReflectionUtils;
33

34
/**
35
 * {@link Converter} to convert from a {@link Duration} to a {@link Number}.
36
 *
37
 * @author Phillip Webb
38
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
39
 * @see DurationFormat
40
 * @see DurationUnit
41
 * @since 4.0
42
 */
43
final class DurationToNumberConverter implements GenericConverter {
3✔
44

45
  @Override
46
  public Set<ConvertiblePair> getConvertibleTypes() {
47
    return Collections.singleton(new ConvertiblePair(Duration.class, Number.class));
7✔
48
  }
49

50
  @Nullable
51
  @Override
52
  public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
53
    if (source == null) {
2!
54
      return null;
×
55
    }
56
    Unit unit = getUnit(sourceType);
3✔
57
    if (unit == null) {
2✔
58
      unit = Unit.MILLIS;
2✔
59
    }
60
    return convert((Duration) source, unit, targetType.getObjectType());
8✔
61
  }
62

63
  @Nullable
64
  static Unit getUnit(TypeDescriptor sourceType) {
65
    DurationUnit annotation = sourceType.getAnnotation(DurationUnit.class);
5✔
66
    if (annotation != null) {
2✔
67
      return Unit.fromChronoUnit(annotation.value());
4✔
68
    }
69

70
    DurationFormat durationFormat = sourceType.getAnnotation(DurationFormat.class);
5✔
71
    if (durationFormat != null) {
2✔
72
      return durationFormat.defaultUnit();
3✔
73
    }
74
    return null;
2✔
75
  }
76

77
  private Object convert(Duration source, Unit unit, Class<?> type) {
78
    try {
79
      return type.getConstructor(String.class)
15✔
80
              .newInstance(String.valueOf(unit.longValue(source)));
4✔
81
    }
82
    catch (Exception ex) {
×
83
      ReflectionUtils.rethrowRuntimeException(ex);
×
84
      throw new IllegalStateException(ex);
×
85
    }
86
  }
87

88
}
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