1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
public class MutablePropertySources implements PropertySources {
private final List<PropertySource<?>> propertySourceList = new CopyOnWriteArrayList<>();
/**
* Create a new {@link MutablePropertySources} object.
*
* 构造方法
*/
public MutablePropertySources() {
}
/**
* Create a new {@code MutablePropertySources} from the given propertySources
* object, preserving the original order of contained {@code PropertySource} objects.
* 构造方法, 传递一个集合, 将集合中的数据放入 {@code propertySourceList}.
*/
public MutablePropertySources(PropertySources propertySources) {
this();
// PropertySources 是一个迭代器接口的实现,通过循环取出信息放入到 propertySourceList 中
for (PropertySource<?> propertySource : propertySources) {
// 放入方法
addLast(propertySource);
}
}
/**
* 获取迭代器对象
*/
@Override
public Iterator<PropertySource<?>> iterator() {
return this.propertySourceList.iterator();
}
/**
* 获取 Spliterator 对象
*/
@Override
public Spliterator<PropertySource<?>> spliterator() {
return Spliterators.spliterator(this.propertySourceList, 0);
}
/**
* 获取流
*/
@Override
public Stream<PropertySource<?>> stream() {
return this.propertySourceList.stream();
}
/**
* 判断是否存在 name
* @param name the {@linkplain PropertySource#getName() name of the property source} to find
*/
@Override
public boolean contains(String name) {
return this.propertySourceList.contains(PropertySource.named(name));
}
/**
* 获取 PropertySource 信息
* @param name the {@linkplain PropertySource#getName() name of the property source} to find
* @return
*/
@Override
@Nullable
public PropertySource<?> get(String name) {
// 获取 name 所在的索引位置
int index = this.propertySourceList.indexOf(PropertySource.named(name));
// get方法获取结果
return (index != -1 ? this.propertySourceList.get(index) : null);
}
/**
* Add the given property source object with highest precedence.
*
* 头插数据
*/
public void addFirst(PropertySource<?> propertySource) {
removeIfPresent(propertySource);
this.propertySourceList.add(0, propertySource);
}
/**
* Add the given property source object with lowest precedence.
*
* 尾插数据
*/
public void addLast(PropertySource<?> propertySource) {
removeIfPresent(propertySource);
this.propertySourceList.add(propertySource);
}
/**
* Add the given property source object with precedence immediately higher
* than the named relative property source.
*
* 在relativePropertySourceName的索引位置前添加数据
*/
public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) {
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
removeIfPresent(propertySource);
int index = assertPresentAndGetIndex(relativePropertySourceName);
addAtIndex(index, propertySource);
}
/**
* Add the given property source object with precedence immediately lower
* than the named relative property source.
* 在relativePropertySourceName的索引位置后添加数据
*/
public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) {
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
// 删除存在的数据
removeIfPresent(propertySource);
// 获取所有
int index = assertPresentAndGetIndex(relativePropertySourceName);
// 在索引+1出添加数据
addAtIndex(index + 1, propertySource);
}
/**
* Return the precedence of the given property source, {@code -1} if not found.
* 获取索引位置
*/
public int precedenceOf(PropertySource<?> propertySource) {
return this.propertySourceList.indexOf(propertySource);
}
/**
* Remove and return the property source with the given name, {@code null} if not found.
* 删除索引位置
* @param name the name of the property source to find and remove
*/
@Nullable
public PropertySource<?> remove(String name) {
// 获取索引
int index = this.propertySourceList.indexOf(PropertySource.named(name));
// 删除索引上的数据
return (index != -1 ? this.propertySourceList.remove(index) : null);
}
/**
* Replace the property source with the given name with the given property source object.
* 替换 name 的信息
* @param name the name of the property source to find and replace
* @param propertySource the replacement property source
* @throws IllegalArgumentException if no property source with the given name is present
* @see #contains
*/
public void replace(String name, PropertySource<?> propertySource) {
// 获取索引位置
int index = assertPresentAndGetIndex(name);
// 设置具体所应位置的值
this.propertySourceList.set(index, propertySource);
}
/**
* Return the number of {@link PropertySource} objects contained.
* 数量
*/
public int size() {
return this.propertySourceList.size();
}
@Override
public String toString() {
return this.propertySourceList.toString();
}
/**
* Ensure that the given property source is not being added relative to itself.
* 确保两个 PropertySource 的 name不相同
*/
protected void assertLegalRelativeAddition(String relativePropertySourceName, PropertySource<?> propertySource) {
// 获取 PropertySource 的名字
String newPropertySourceName = propertySource.getName();
// 历史名字和新的名字是否相同
if (relativePropertySourceName.equals(newPropertySourceName)) {
throw new IllegalArgumentException(
"PropertySource named '" + newPropertySourceName + "' cannot be added relative to itself");
}
}
/**
* Remove the given property source if it is present.
* 删除已存在的数据
*/
protected void removeIfPresent(PropertySource<?> propertySource) {
this.propertySourceList.remove(propertySource);
}
/**
* Add the given property source at a particular index in the list.
* 指定索引位置插入数据
*/
private void addAtIndex(int index, PropertySource<?> propertySource) {
removeIfPresent(propertySource);
this.propertySourceList.add(index, propertySource);
}
/**
* Assert that the named property source is present and return its index.
* 获取 name 所在的索引位置
* @param name {@linkplain PropertySource#getName() name of the property source} to find
* @throws IllegalArgumentException if the named property source is not present
*/
private int assertPresentAndGetIndex(String name) {
int index = this.propertySourceList.indexOf(PropertySource.named(name));
if (index == -1) {
throw new IllegalArgumentException("PropertySource named '" + name + "' does not exist");
}
return index;
}
}
|