• 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

88.57
today-context/src/main/java/infra/format/support/DelimitedStringToArrayConverter.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.lang.reflect.Array;
23
import java.util.Collections;
24
import java.util.Set;
25

26
import infra.core.TypeDescriptor;
27
import infra.core.conversion.ConditionalGenericConverter;
28
import infra.core.conversion.ConversionService;
29
import infra.format.annotation.Delimiter;
30
import infra.lang.Assert;
31
import infra.util.StringUtils;
32

33
/**
34
 * Converts a {@link Delimiter delimited} String to an Array.
35
 *
36
 * @author Phillip Webb
37
 * @author <a href="https://github.com/TAKETODAY">海子 Yang</a>
38
 */
39
final class DelimitedStringToArrayConverter implements ConditionalGenericConverter {
40

41
  private final ConversionService conversionService;
42

43
  DelimitedStringToArrayConverter(ConversionService conversionService) {
2✔
44
    Assert.notNull(conversionService, "ConversionService is required");
3✔
45
    this.conversionService = conversionService;
3✔
46
  }
1✔
47

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

53
  @Override
54
  public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
55
    return targetType.getElementDescriptor() == null
8!
56
            || this.conversionService.canConvert(sourceType, targetType.getElementDescriptor());
6✔
57
  }
58

59
  @Nullable
60
  @Override
61
  public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
62
    if (source == null) {
2!
63
      return null;
×
64
    }
65
    return convert((String) source, sourceType, targetType);
7✔
66
  }
67

68
  private Object convert(String source, TypeDescriptor sourceType, TypeDescriptor targetType) {
69
    Delimiter delimiter = targetType.getAnnotation(Delimiter.class);
5✔
70
    String[] elements = getElements(source, delimiter != null ? delimiter.value() : ",");
10✔
71
    TypeDescriptor elementDescriptor = targetType.getElementDescriptor();
3✔
72
    Assert.state(elementDescriptor != null, "elementDescriptor is missing");
6!
73
    Object target = Array.newInstance(elementDescriptor.getType(), elements.length);
6✔
74
    for (int i = 0; i < elements.length; i++) {
8✔
75
      String sourceElement = elements[i];
4✔
76
      Object targetElement = this.conversionService.convert(sourceElement.trim(), sourceType, elementDescriptor);
8✔
77
      Array.set(target, i, targetElement);
4✔
78
    }
79
    return target;
2✔
80
  }
81

82
  private String[] getElements(String source, String delimiter) {
83
    return StringUtils.delimitedListToStringArray(source, Delimiter.NONE.equals(delimiter) ? null : delimiter);
10✔
84
  }
85

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