17
17
package org .springframework .core .env ;
18
18
19
19
import java .util .Iterator ;
20
- import java .util .LinkedList ;
20
+ import java .util .List ;
21
+ import java .util .concurrent .CopyOnWriteArrayList ;
21
22
22
23
import org .apache .commons .logging .Log ;
23
24
import org .apache .commons .logging .LogFactory ;
24
25
25
- import org .springframework .util .Assert ;
26
26
import org .springframework .util .StringUtils ;
27
27
28
28
/**
35
35
* will be searched when resolving a given property with a {@link PropertyResolver}.
36
36
*
37
37
* @author Chris Beams
38
+ * @author Juergen Hoeller
38
39
* @since 3.1
39
40
* @see PropertySourcesPropertyResolver
40
41
*/
41
42
public class MutablePropertySources implements PropertySources {
42
43
43
- static final String NON_EXISTENT_PROPERTY_SOURCE_MESSAGE = "PropertySource named [%s] does not exist" ;
44
- static final String ILLEGAL_RELATIVE_ADDITION_MESSAGE = "PropertySource named [%s] cannot be added relative to itself" ;
45
-
46
44
private final Log logger ;
47
45
48
- private final LinkedList <PropertySource <?>> propertySourceList = new LinkedList <PropertySource <?>>();
46
+ private final List <PropertySource <?>> propertySourceList = new CopyOnWriteArrayList <PropertySource <?>>();
49
47
50
48
51
49
/**
52
50
* Create a new {@link MutablePropertySources} object.
53
51
*/
54
52
public MutablePropertySources () {
55
- this .logger = LogFactory .getLog (this . getClass ());
53
+ this .logger = LogFactory .getLog (getClass ());
56
54
}
57
55
58
56
/**
@@ -62,7 +60,7 @@ public MutablePropertySources() {
62
60
public MutablePropertySources (PropertySources propertySources ) {
63
61
this ();
64
62
for (PropertySource <?> propertySource : propertySources ) {
65
- this . addLast (propertySource );
63
+ addLast (propertySource );
66
64
}
67
65
}
68
66
@@ -83,7 +81,7 @@ public boolean contains(String name) {
83
81
@ Override
84
82
public PropertySource <?> get (String name ) {
85
83
int index = this .propertySourceList .indexOf (PropertySource .named (name ));
86
- return index == -1 ? null : this .propertySourceList .get (index );
84
+ return ( index != -1 ? this .propertySourceList .get (index ) : null );
87
85
}
88
86
89
87
@ Override
@@ -100,7 +98,7 @@ public void addFirst(PropertySource<?> propertySource) {
100
98
propertySource .getName ()));
101
99
}
102
100
removeIfPresent (propertySource );
103
- this .propertySourceList .addFirst ( propertySource );
101
+ this .propertySourceList .add ( 0 , propertySource );
104
102
}
105
103
106
104
/**
@@ -112,7 +110,7 @@ public void addLast(PropertySource<?> propertySource) {
112
110
propertySource .getName ()));
113
111
}
114
112
removeIfPresent (propertySource );
115
- this .propertySourceList .addLast (propertySource );
113
+ this .propertySourceList .add (propertySource );
116
114
}
117
115
118
116
/**
@@ -161,7 +159,7 @@ public PropertySource<?> remove(String name) {
161
159
logger .debug (String .format ("Removing [%s] PropertySource" , name ));
162
160
}
163
161
int index = this .propertySourceList .indexOf (PropertySource .named (name ));
164
- return index == -1 ? null : this .propertySourceList .remove (index );
162
+ return ( index != -1 ? this .propertySourceList .remove (index ) : null );
165
163
}
166
164
167
165
/**
@@ -190,7 +188,7 @@ public int size() {
190
188
@ Override
191
189
public String toString () {
192
190
String [] names = new String [this .size ()];
193
- for (int i = 0 ; i < size (); i ++) {
191
+ for (int i = 0 ; i < size (); i ++) {
194
192
names [i ] = this .propertySourceList .get (i ).getName ();
195
193
}
196
194
return String .format ("[%s]" , StringUtils .arrayToCommaDelimitedString (names ));
@@ -201,17 +199,17 @@ public String toString() {
201
199
*/
202
200
protected void assertLegalRelativeAddition (String relativePropertySourceName , PropertySource <?> propertySource ) {
203
201
String newPropertySourceName = propertySource .getName ();
204
- Assert .isTrue (!relativePropertySourceName .equals (newPropertySourceName ),
205
- String .format (ILLEGAL_RELATIVE_ADDITION_MESSAGE , newPropertySourceName ));
202
+ if (relativePropertySourceName .equals (newPropertySourceName )) {
203
+ throw new IllegalArgumentException (
204
+ String .format ("PropertySource named [%s] cannot be added relative to itself" , newPropertySourceName ));
205
+ }
206
206
}
207
207
208
208
/**
209
209
* Remove the given property source if it is present.
210
210
*/
211
211
protected void removeIfPresent (PropertySource <?> propertySource ) {
212
- if (this .propertySourceList .contains (propertySource )) {
213
- this .propertySourceList .remove (propertySource );
214
- }
212
+ this .propertySourceList .remove (propertySource );
215
213
}
216
214
217
215
/**
@@ -230,7 +228,9 @@ private void addAtIndex(int index, PropertySource<?> propertySource) {
230
228
*/
231
229
private int assertPresentAndGetIndex (String name ) {
232
230
int index = this .propertySourceList .indexOf (PropertySource .named (name ));
233
- Assert .isTrue (index >= 0 , String .format (NON_EXISTENT_PROPERTY_SOURCE_MESSAGE , name ));
231
+ if (index == -1 ) {
232
+ throw new IllegalArgumentException (String .format ("PropertySource named [%s] does not exist" , name ));
233
+ }
234
234
return index ;
235
235
}
236
236
0 commit comments