• 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

77.14
today-context/src/main/java/infra/format/support/CollectionToDelimitedStringConverter.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.Collection;
23
import java.util.Collections;
24
import java.util.Set;
25
import java.util.stream.Collectors;
26

27
import infra.core.TypeDescriptor;
28
import infra.core.conversion.ConditionalGenericConverter;
29
import infra.core.conversion.ConversionService;
30
import infra.format.annotation.Delimiter;
31

32
/**
33
 * Converts a Collection to a delimited String.
34
 *
35
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
36
 * @author Phillip Webb
37
 * @since 4.0
38
 */
39
final class CollectionToDelimitedStringConverter implements ConditionalGenericConverter {
40

41
  private final ConversionService conversionService;
42

43
  CollectionToDelimitedStringConverter(ConversionService conversionService) {
2✔
44
    this.conversionService = conversionService;
3✔
45
  }
1✔
46

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

52
  @Override
53
  public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
54
    TypeDescriptor sourceElementType = sourceType.getElementDescriptor();
3✔
55
    if (targetType == null || sourceElementType == null) {
4!
56
      return true;
2✔
57
    }
58
    return this.conversionService.canConvert(sourceElementType, targetType)
7!
59
            || sourceElementType.getType().isAssignableFrom(targetType.getType());
2!
60
  }
61

62
  @Nullable
63
  @Override
64
  public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
65
    if (source == null) {
2!
66
      return null;
×
67
    }
68
    Collection<?> sourceCollection = (Collection<?>) source;
3✔
69
    return convert(sourceCollection, sourceType, targetType);
6✔
70
  }
71

72
  private Object convert(Collection<?> source, TypeDescriptor sourceType, TypeDescriptor targetType) {
73
    if (source.isEmpty()) {
3!
74
      return "";
×
75
    }
76
    return source.stream().map((element) -> convertElement(element, sourceType, targetType))
16✔
77
            .collect(Collectors.joining(getDelimiter(sourceType)));
3✔
78
  }
79

80
  private CharSequence getDelimiter(TypeDescriptor sourceType) {
81
    Delimiter annotation = sourceType.getAnnotation(Delimiter.class);
5✔
82
    return (annotation != null) ? annotation.value() : ",";
7✔
83
  }
84

85
  private String convertElement(Object element, TypeDescriptor sourceType, TypeDescriptor targetType) {
86
    return String.valueOf(
7✔
87
            this.conversionService.convert(element, sourceType.elementDescriptor(element), targetType));
3✔
88
  }
89

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