简体   繁体   English

ImmutableMap构建器如何创建抽象类的实例?

[英]How can ImmutableMap builder create instance of abstract class?

I am learning to use guava library and referred to this I see use of builder to construct an instance of immutable map. 我正在学习使用番石榴库,并提到了这一点,我看到了使用builder来构造不可变地图的实例。 How is the builder constructor able to create an abstract class instance? 构建器构造函数如何创建抽象类实例?

   static final ImmutableMap<String, Integer> WORD_TO_INT =
       new ImmutableMap.Builder<String, Integer>()
           .put("one", 1)
           .put("two", 2)
           .put("three", 3)
           .build();

I does not, the Builder creates an implementation of an ImmutableMap (a class that extends the ImmutableMap ). 我没有, Builder创建了ImmutableMap实现extendsImmutableMap的类)。

To understand clearly, start here before working with Guava. 为了清楚地理解,请先从这里开始然后再使用Guava。

Update: see comment by @Louis Wasserman. 更新:请参阅@Louis Wasserman的评论。 Indeed an essential comment. 确实是必不可少的评论。

It is not the constructor of the builder class that returns an instance of an immutable map. 不是生成器类的构造函数返回不可变映射的实例。

You are first creating the builder by calling new ImmutableMap.Builder<String, Integer>() and then you are calling methods in a chain on this ImmutableMap.Builder instance - three times the put method and then the build method. 首先,通过调用new ImmutableMap.Builder<String, Integer>()创建构建器,然后在此ImmutableMap.Builder实例上的链中调用方法- put方法的三倍,然后是build方法的三倍。

The build method is what is called last, and that is what creates and returns the instance of ImmutableMap . build方法是最后一个方法,它创建并返回ImmutableMap的实例。

The "trick" here is that the put method of ImmutableMap.Builder returns the builder itself (it has a statement return this; at the end) so that you can chain method calls like this. 这里的“窍门”是ImmutableMap.Builderput方法返回了生成器本身(它有一个return this;的语句return this;最后),以便您可以像这样链接方法调用。

And, indeed, the build method returns an instance of a subclass of ImmutableMap because class ImmutableMap is abstract, so it cannot be directly instantiated. 而且,确实, build方法返回ImmutableMap子类的实例,因为ImmutableMap类是抽象的,因此无法直接实例化。

ImmutableMap (Guava: Google Core Libraries for Java 21.0-SNAPSHOT API) states that it is "a Map whose contents will never change, with many other important properties detailed at ImmutableCollection ". ImmutableMap (Guava: Google Core Libraries for Java 21.0-SNAPSHOT API)指出,它是“内容永远不会改变的MapImmutableCollection详述了许多其他重要属性”。

The "other important properties detailed at ImmutableCollection " include the following guarantees : “在ImmutableCollection详细介绍的其他重要属性”包括以下保证

Each makes the following guarantees: 每个提供以下保证:

  • Shallow immutability. 浅层不变性。 Elements can never be added, removed or replaced in this collection. 绝对不能在此集合中添加,删除或替换元素。 This is a stronger guarantee than that of Collections.unmodifiableCollection(java.util.Collection<? extends T>) , whose contents change whenever the wrapped collection is modified. 这比Collections.unmodifiableCollection(java.util.Collection<? extends T>)保证要强。
  • Null-hostility. 空敌意。 This collection will never contain a null element. 此集合永远不会包含null元素。
  • Deterministic iteration. 确定性迭代。 The iteration order is always well-defined, depending on how the collection was created (see the appropriate factory method for details). 迭代顺序始终是明确定义的,具体取决于创建集合的方式(有关详细信息,请参见适当的工厂方法)。 View collections such as Multiset.elementSet() iterate in the same order as the parent, except as noted. 除非另有说明,否则诸如Multiset.elementSet()类的视图集合以与父级相同的顺序进行迭代。
  • Thread safety. 线程安全。 It is safe to access this collection concurrently from multiple threads. 从多个线程同时访问此集合是安全的。
  • Integrity. 诚信 This type cannot be subclassed outside this package (which would allow these guarantees to be violated). 此类型不能在此包的外部进行子类化(这将违反这些保证)。

The last guarantee, integrety , alludes to the fact that internally Guava has concrete implementations (non-abstract) of ImmutableMap and other immutable objects which is what is actually returned by these builders. 最后一个保证是integrety ,这个事实暗示着Guava内部具有ImmutableMap和其他不可变对象的具体实现(非抽象),而这些实际上是这些构建器返回的。

Furthermore, the source is open; 此外,源是开放的; you can go find out for yourself how the builder is able to do it (eg you might start here ). 您可以自己了解构建器是如何做到的(例如,您可以从此处开始)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM