@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2010 the original author or authors .
* Copyright 2002 - 2011 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -16,44 +16,80 @@
@@ -16,44 +16,80 @@
package org.springframework.core.env ;
import java.util.Collections ;
import java.util.Iterator ;
import java.util.LinkedList ;
import java.util.List ;
import org.springframework.util.Assert ;
/ * *
* Default implementation of the { @link PropertySources } interface .
* Allows manipulation of contained property sources and provides constructor
* to copying an existing { @code PropertySources } instance .
*
* < p > Where < em > precedence < / em > is mentioned in methods such as { @link # addFirst }
* and { @link # addLast } , this is with regard to the order in which property sources
* will be searched when resolving a given property with a { @link PropertyResolver } .
*
* @author Chris Beams
* @since 3 . 1
* @see PropertySourcesPropertyResolver
* /
public class MutablePropertySources implements PropertySources {
private final LinkedList < PropertySource < ? > > propertySourceList = new LinkedList < PropertySource < ? > > ( ) ;
static final String NON_EXISTENT_PROPERTY_SOURCE_MESSAGE = "PropertySource named [%s] does not exist" ;
static final String ILLEGAL_RELATIVE_ADDITION_MESSAGE = "PropertySource named [%s] cannot be added relative to itself" ;
private final LinkedList < PropertySource < ? > > propertySourceList = new LinkedList < PropertySource < ? > > ( ) ;
/ * *
* 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 .
* /
public MutablePropertySources ( PropertySources propertySources ) {
this . addAll ( propertySources ) ;
}
public void addAll ( PropertySources propertySources ) {
for ( PropertySource < ? > propertySource : propertySources . asList ( ) ) {
for ( PropertySource < ? > propertySource : propertySources ) {
this . addLast ( propertySource ) ;
}
}
public boolean contains ( String name ) {
return this . propertySourceList . contains ( PropertySource . named ( name ) ) ;
}
public PropertySource < ? > get ( String name ) {
return this . propertySourceList . get ( this . propertySourceList . indexOf ( PropertySource . named ( name ) ) ) ;
}
public Iterator < PropertySource < ? > > iterator ( ) {
return this . propertySourceList . iterator ( ) ;
}
/ * *
* Add the given property source object with highest precedence .
* /
public void addFirst ( PropertySource < ? > propertySource ) {
removeIfPresent ( propertySource ) ;
this . propertySourceList . addFirst ( propertySource ) ;
}
/ * *
* Add the given property source object with lowest precedence .
* /
public void addLast ( PropertySource < ? > propertySource ) {
removeIfPresent ( propertySource ) ;
this . propertySourceList . addLast ( propertySource ) ;
}
/ * *
* Add the given property source object with precedence immediately greater
* than the named relative property source .
* /
public void addBefore ( String relativePropertySourceName , PropertySource < ? > propertySource ) {
assertLegalRelativeAddition ( relativePropertySourceName , propertySource ) ;
removeIfPresent ( propertySource ) ;
@ -61,6 +97,10 @@ public class MutablePropertySources implements PropertySources {
@@ -61,6 +97,10 @@ public class MutablePropertySources implements PropertySources {
addAtIndex ( index , propertySource ) ;
}
/ * *
* Add the given property source object with precedence immediately less than
* than the named relative property source .
* /
public void addAfter ( String relativePropertySourceName , PropertySource < ? > propertySource ) {
assertLegalRelativeAddition ( relativePropertySourceName , propertySource ) ;
removeIfPresent ( propertySource ) ;
@ -68,56 +108,79 @@ public class MutablePropertySources implements PropertySources {
@@ -68,56 +108,79 @@ public class MutablePropertySources implements PropertySources {
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
* /
public PropertySource < ? > remove ( String name ) {
int index = this . propertySourceList . indexOf ( PropertySource . named ( name ) ) ;
if ( index > = 0 ) {
return this . propertySourceList . remove ( index ) ;
}
return null ;
}
/ * *
* Replace the property source with the given name with the given property source object .
* @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 ( ) ;
}
/ * *
* Ensure that the given property source is not being added relative to itself .
* /
protected void assertLegalRelativeAddition ( String relativePropertySourceName , PropertySource < ? > propertySource ) {
String newPropertySourceName = propertySource . getName ( ) ;
Assert . isTrue ( ! relativePropertySourceName . equals ( newPropertySourceName ) ,
String . format ( ILLEGAL_RELATIVE_ADDITION_MESSAGE , newPropertySourceName ) ) ;
}
protected void addAtIndex ( int index , PropertySource < ? > propertySource ) {
removeIfPresent ( propertySource ) ;
this . propertySourceList . add ( index , propertySource ) ;
}
/ * *
* Log the removal of the given propertySource if it is present .
* /
protected void removeIfPresent ( PropertySource < ? > propertySource ) {
if ( this . propertySourceList . contains ( propertySource ) ) {
// TODO SPR-7508: add logging
this . propertySourceList . remove ( propertySource ) ;
}
}
public boolean contains ( String propertySourceName ) {
return propertySourceList . contains ( PropertySource . named ( propertySourceName ) ) ;
}
public PropertySource < ? > remove ( String propertySourceName ) {
int index = propertySourceList . indexOf ( PropertySource . named ( propertySourceName ) ) ;
if ( index > = 0 ) {
return propertySourceList . remove ( index ) ;
}
return null ;
}
public void replace ( String propertySourceName , PropertySource < ? > propertySource ) {
int index = assertPresentAndGetIndex ( propertySourceName ) ;
this . propertySourceList . set ( index , 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 ) ;
}
protected int assertPresentAndGetIndex ( String propertySourceName ) {
/ * *
* Assert that the named property source is present and return its index .
* @throws IllegalArgumentException if the named property source is not present
* /
private int assertPresentAndGetIndex ( String propertySourceName ) {
int index = this . propertySourceList . indexOf ( PropertySource . named ( propertySourceName ) ) ;
Assert . isTrue ( index > = 0 , String . format ( NON_EXISTENT_PROPERTY_SOURCE_MESSAGE , propertySourceName ) ) ;
return index ;
}
public int size ( ) {
return propertySourceList . size ( ) ;
}
public List < PropertySource < ? > > asList ( ) {
return Collections . unmodifiableList ( this . propertySourceList ) ;
}
public PropertySource < ? > get ( String propertySourceName ) {
return propertySourceList . get ( propertySourceList . indexOf ( PropertySource . named ( propertySourceName ) ) ) ;
}
}