• 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

97.1
today-context/src/main/java/infra/context/properties/source/DefaultConfigurationPropertySources.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.context.properties.source;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.ArrayDeque;
23
import java.util.Iterator;
24
import java.util.NoSuchElementException;
25
import java.util.function.Function;
26

27
import infra.core.env.ConfigurableEnvironment;
28
import infra.core.env.PropertySource;
29
import infra.core.env.PropertySource.StubPropertySource;
30
import infra.core.env.PropertySources;
31
import infra.lang.Assert;
32
import infra.origin.OriginLookup;
33
import infra.util.ConcurrentReferenceHashMap;
34
import infra.util.ConcurrentReferenceHashMap.ReferenceType;
35

36
/**
37
 * Adapter to convert Framework's {@link PropertySources} to
38
 * {@link ConfigurationPropertySource ConfigurationPropertySources}.
39
 *
40
 * @author Phillip Webb
41
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
42
 * @since 4.0
43
 */
44
class DefaultConfigurationPropertySources implements Iterable<ConfigurationPropertySource> {
45

46
  private final Iterable<PropertySource<?>> sources;
47

48
  private final ConcurrentReferenceHashMap<PropertySource<?>, ConfigurationPropertySource> cache
7✔
49
          = new ConcurrentReferenceHashMap<>(16, ReferenceType.SOFT);
50

51
  DefaultConfigurationPropertySources(Iterable<PropertySource<?>> sources) {
2✔
52
    Assert.notNull(sources, "Sources is required");
3✔
53
    this.sources = sources;
3✔
54
  }
1✔
55

56
  boolean isUsingSources(Iterable<PropertySource<?>> sources) {
57
    return this.sources == sources;
8✔
58
  }
59

60
  @Override
61
  public Iterator<ConfigurationPropertySource> iterator() {
62
    return new SourcesIterator(this.sources.iterator(), this::adapt);
9✔
63
  }
64

65
  private ConfigurationPropertySource adapt(PropertySource<?> source) {
66
    ConfigurationPropertySource result = this.cache.get(source);
6✔
67
    // Most PropertySources test equality only using the source name, so we need to
68
    // check the actual source hasn't also changed.
69
    if (result != null && result.getUnderlyingSource() == source) {
6✔
70
      return result;
2✔
71
    }
72
    result = DefaultConfigurationPropertySource.from(source);
3✔
73
    if (source instanceof OriginLookup) {
3✔
74
      result = result.withPrefix(((OriginLookup<?>) source).getPrefix());
6✔
75
    }
76
    this.cache.put(source, result);
6✔
77
    return result;
2✔
78
  }
79

80
  private static class SourcesIterator implements Iterator<ConfigurationPropertySource> {
81
    private final ArrayDeque<Iterator<PropertySource<?>>> iterators;
82
    private final Function<PropertySource<?>, ConfigurationPropertySource> adapter;
83

84
    @Nullable
85
    private ConfigurationPropertySource next;
86

87
    SourcesIterator(Iterator<PropertySource<?>> iterator, Function<PropertySource<?>, ConfigurationPropertySource> adapter) {
2✔
88
      this.iterators = new ArrayDeque<>(4);
6✔
89
      this.iterators.push(iterator);
4✔
90
      this.adapter = adapter;
3✔
91
    }
1✔
92

93
    @Override
94
    public boolean hasNext() {
95
      return fetchNext() != null;
7✔
96
    }
97

98
    @Override
99
    public ConfigurationPropertySource next() {
100
      ConfigurationPropertySource next = fetchNext();
3✔
101
      if (next == null) {
2!
102
        throw new NoSuchElementException();
×
103
      }
104
      this.next = null;
3✔
105
      return next;
2✔
106
    }
107

108
    @Nullable
109
    private ConfigurationPropertySource fetchNext() {
110
      if (this.next == null) {
3✔
111
        if (this.iterators.isEmpty()) {
4✔
112
          return null;
2✔
113
        }
114
        if (!this.iterators.peek().hasNext()) {
6✔
115
          this.iterators.pop();
4✔
116
          return fetchNext();
3✔
117
        }
118
        PropertySource<?> candidate = this.iterators.peek().next();
7✔
119
        if (candidate.getSource() instanceof ConfigurableEnvironment) {
4✔
120
          push((ConfigurableEnvironment) candidate.getSource());
5✔
121
          return fetchNext();
3✔
122
        }
123
        if (isIgnored(candidate)) {
4✔
124
          return fetchNext();
3✔
125
        }
126
        this.next = this.adapter.apply(candidate);
7✔
127
      }
128
      return this.next;
3✔
129
    }
130

131
    private void push(ConfigurableEnvironment environment) {
132
      this.iterators.push(environment.getPropertySources().iterator());
6✔
133
    }
1✔
134

135
    private boolean isIgnored(PropertySource<?> candidate) {
136
      return candidate instanceof StubPropertySource
10✔
137
              || candidate instanceof ConfigurationPropertySourcesPropertySource;
138
    }
139

140
  }
141

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