由于spring源码中大量使用到这个类,所以决定分享一下。
类的功能解析
看下类的继承关系
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;
}
从上看的源码以及继承关系可以看出
-
MutablePropertyValues
就是一个存储PropertyValue
一个容器。 -
PropertyValues
实现了Iterable
借口,并定义了存储数据类型是PropertyValue
,当然还做了一些默认实现。 - 从官方给出的说明可以看出
PropertyValue
只有一个key-value键值对存储类。
看下MutablePropertyValues
构造函数
/**
* 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<>());
}
进一步得出结论,MutablePropertyValues
就是用ArrayList
存储PropertyValues
的单个键值对容器。