• 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

94.44
today-context/src/main/java/infra/format/support/DelimitedStringToCollectionConverter.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.util.Arrays;
23
import java.util.Collection;
24
import java.util.Collections;
25
import java.util.Set;
26
import java.util.stream.Stream;
27

28
import infra.core.TypeDescriptor;
29
import infra.core.conversion.ConditionalGenericConverter;
30
import infra.core.conversion.ConversionService;
31
import infra.format.annotation.Delimiter;
32
import infra.lang.Assert;
33
import infra.util.CollectionUtils;
34
import infra.util.StringUtils;
35

36
/**
37
 * Converts a {@link Delimiter delimited} String to a Collection.
38
 *
39
 * @author Phillip Webb
40
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
41
 * @since 4.0
42
 */
43
final class DelimitedStringToCollectionConverter implements ConditionalGenericConverter {
44

45
  private final ConversionService conversionService;
46

47
  DelimitedStringToCollectionConverter(ConversionService conversionService) {
2✔
48
    Assert.notNull(conversionService, "ConversionService is required");
3✔
49
    this.conversionService = conversionService;
3✔
50
  }
1✔
51

52
  @Override
53
  public Set<ConvertiblePair> getConvertibleTypes() {
54
    return Collections.singleton(new ConvertiblePair(String.class, Collection.class));
7✔
55
  }
56

57
  @Override
58
  public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
59
    return targetType.getElementDescriptor() == null
8✔
60
            || this.conversionService.canConvert(sourceType, targetType.getElementDescriptor());
6✔
61
  }
62

63
  @Nullable
64
  @Override
65
  public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
66
    if (source == null) {
2!
67
      return null;
×
68
    }
69
    return convert((String) source, sourceType, targetType);
7✔
70
  }
71

72
  private Object convert(String source, TypeDescriptor sourceType, TypeDescriptor targetType) {
73
    Delimiter delimiter = targetType.getAnnotation(Delimiter.class);
5✔
74
    String[] elements = getElements(source, delimiter != null ? delimiter.value() : ",");
10✔
75
    TypeDescriptor elementDescriptor = targetType.getElementDescriptor();
3✔
76
    Collection<Object> target = createCollection(targetType, elementDescriptor, elements.length);
7✔
77
    Stream<Object> stream = Arrays.stream(elements).map(String::trim);
5✔
78
    if (elementDescriptor != null) {
2✔
79
      stream = stream.map((element) -> this.conversionService.convert(element, sourceType, elementDescriptor));
14✔
80
    }
81
    stream.forEach(target::add);
7✔
82
    return target;
2✔
83
  }
84

85
  private Collection<Object> createCollection(
86
          TypeDescriptor targetType, @Nullable TypeDescriptor elementDescriptor, int length) {
87
    return CollectionUtils.createCollection(
3✔
88
            targetType.getType(), elementDescriptor != null ? elementDescriptor.getType() : null, length);
8✔
89
  }
90

91
  private String[] getElements(String source, String delimiter) {
92
    return StringUtils.delimitedListToStringArray(source, Delimiter.NONE.equals(delimiter) ? null : delimiter);
10✔
93
  }
94

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