美文网首页
MutablePropertyValues 源码解析

MutablePropertyValues 源码解析

作者: 断忆残缘 | 来源:发表于2023-01-14 15:08 被阅读0次

由于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;
  
}

从上看的源码以及继承关系可以看出

  1. MutablePropertyValues就是一个存储PropertyValue一个容器。
  2. PropertyValues实现了Iterable借口,并定义了存储数据类型是PropertyValue,当然还做了一些默认实现。
  3. 从官方给出的说明可以看出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的单个键值对容器。

相关文章

网友评论

      本文标题:MutablePropertyValues 源码解析

      本文链接:https://www.haomeiwen.com/subject/yxxocdtx.html