由于spring源碼中大量使用到這個(gè)類,所以決定分享一下。
類的功能解析
看下類的繼承關(guān)系
public class MutablePropertyValues implements PropertyValues, Serializable {
private final List<PropertyValue> propertyValueList;
}
public interface PropertyValues extends Iterable<PropertyValue> {
}
/**
* Object to hold information and value for an individual bean property.
* Using an object here, rather than just storing all properties in
* a map keyed by property name, allows for more flexibility, and the
* ability to handle indexed properties etc in an optimized way.
*/
public class PropertyValue extends BeanMetadataAttributeAccessor implements Serializable {
private final String name;
@Nullable
private final Object value;
}
從上看的源碼以及繼承關(guān)系可以看出
-
MutablePropertyValues
就是一個(gè)存儲(chǔ)PropertyValue
一個(gè)容器。 -
PropertyValues
實(shí)現(xiàn)了Iterable
借口羡榴,并定義了存儲(chǔ)數(shù)據(jù)類型是PropertyValue
,當(dāng)然還做了一些默認(rèn)實(shí)現(xiàn)。 - 從官方給出的說(shuō)明可以看出
PropertyValue
只有一個(gè)key-value鍵值對(duì)存儲(chǔ)類蜻直。
看下MutablePropertyValues
構(gòu)造函數(shù)
/**
* Creates a new empty MutablePropertyValues object.
* <p>Property values can be added with the {@code add} method.
* @see #add(String, Object)
*/
public MutablePropertyValues() {
this.propertyValueList = new ArrayList<>(0);
}
/**
* Deep copy constructor. Guarantees PropertyValue references
* are independent, although it can't deep copy objects currently
* referenced by individual PropertyValue objects.
* @param original the PropertyValues to copy
* @see #addPropertyValues(PropertyValues)
*/
public MutablePropertyValues(@Nullable PropertyValues original) {
// We can optimize this because it's all new:
// There is no replacement of existing property values.
if (original != null) {
PropertyValue[] pvs = original.getPropertyValues();
this.propertyValueList = new ArrayList<>(pvs.length);
for (PropertyValue pv : pvs) {
this.propertyValueList.add(new PropertyValue(pv));
}
}
else {
this.propertyValueList = new ArrayList<>(0);
}
}
/**
* Construct a new MutablePropertyValues object from a Map.
* @param original a Map with property values keyed by property name Strings
* @see #addPropertyValues(Map)
*/
public MutablePropertyValues(@Nullable Map<?, ?> original) {
// We can optimize this because it's all new:
// There is no replacement of existing property values.
if (original != null) {
this.propertyValueList = new ArrayList<>(original.size());
original.forEach((attrName, attrValue) -> this.propertyValueList.add(
new PropertyValue(attrName.toString(), attrValue)));
}
else {
this.propertyValueList = new ArrayList<>(0);
}
}
/**
* Construct a new MutablePropertyValues object using the given List of
* PropertyValue objects as-is.
* <p>This is a constructor for advanced usage scenarios.
* It is not intended for typical programmatic use.
* @param propertyValueList a List of PropertyValue objects
*/
public MutablePropertyValues(@Nullable List<PropertyValue> propertyValueList) {
this.propertyValueList =
(propertyValueList != null ? propertyValueList : new ArrayList<>());
}
進(jìn)一步得出結(jié)論,MutablePropertyValues
就是用ArrayList
存儲(chǔ)PropertyValues
的單個(gè)鍵值對(duì)容器袁串。