Guide to Guava ClassToInstanceMap – Guava ClassToInstanceMap指南

最后修改: 2017年 5月 12日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

A ClassToInstanceMap<B> is a special kind of map that associates classes with corresponding instances. It makes sure that all the keys and the values are subtypes of the upper bound type B.

ClassToInstanceMap<B>是一种特殊的地图,它将类与相应的实例联系起来。它确保所有的键和值都是上界类型的子类型B.

ClassToInstanceMap extends Java’s Map interface and provides two additional methods: T getInstance(Class<T>) and T putInstance(Class<T>, T). The advantage of this map is that these two methods can be used to perform type-safe operations and avoid casting.

ClassToInstanceMap扩展了Java的Map接口并提供了两个额外的方法。T getInstance(Class<T>)T putInstance(Class<T>, T).这个地图的优点是这两个方法可以用来执行类型安全的操作,避免铸造。

In this tutorial, we will show how to use the Google Guava’s ClassToInstanceMap interface and its implementations.

在本教程中,我们将展示如何使用Google Guava的ClassToInstanceMap接口和其实现。

2. Google Guava’s ClassToInstanceMap

2.Google Guava的ClassToInstanceMap

Let’s have a look at how to use the implementation.

让我们来看看如何使用该实施方案。

We’ll start by adding the Google Guava library dependency in the pom.xml:

我们将首先在pom.xml中添加Google Guava库的依赖关系。

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

The latest version of the dependency can be checked here.

最新版本的依赖关系可以在这里查看

ClassToInstanceMap interface has two implementations: a mutable and an immutable one. Let’s have a look at each of them separately.

ClassToInstanceMap接口有两个实现:一个是可变的,一个是不可变的。让我们分别看一下它们。

3. Creating an ImmutableClassToInstanceMap

3.创建一个ImmutableClassToInstanceMap

We can create an instance of ImmutableClassToInstanceMap in a number of ways:

我们可以通过多种方式创建一个ImmutableClassToInstanceMap的实例。

  • using the of() method to create an empty map:
    ImmutableClassToInstanceMap.of()
  • using the of(Class<T> type, T value) method to create a single entry map:
    ImmutableClassToInstanceMap.of(Save.class, new Save());
  • using copyOf() method which accepts another map as a parameter. It will create a map with the same entries as map provided as the parameter:
    ImmutableClassToInstanceMap.copyOf(someMap)
  • using the builder:
    ImmutableClassToInstanceMap.<Action>builder()
      .put(Save.class, new Save())
      .put(Open.class, new Open())
      .put(Delete.class, new Delete())
      .build();

4. Creating a MutableClassToInstanceMap

4.创建一个MutableClassToInstanceMap

We can also create an instance of MutableClassToInstanceMap:

我们也可以创建一个MutableClassToInstanceMap的实例。

  • using the create() method which makes an instance backed by HashMap:
    MutableClassToInstanceMap.create();
  • using the create(Map<Class<? extends B>, B> backingMap) which makes an instance backed by the provided empty map:
    MutableClassToInstanceMap.create(new HashMap());

5. Usage

5.使用方法

Let’s have a look how to use the two new methods that are added to the regular Map interface:

让我们看看如何使用添加到常规Map接口的两个新方法。

  • first method is <T extends B> T getInstance(Class<T> type):
    Action openAction = map.get(Open.class);
    Delete deleteAction = map.getInstance(Delete.class);
  • second method is <T extends B> T putInstance(Class<T> type, @Nullable T value):
    Action newOpen = map.put(Open.class, new Open());
    Delete newDelete = map.putInstance(Delete.class, new Delete());

6. Conclusion

6.结论

In this quick tutorial, we showed examples how to use ClassToInstanceMap from the Guava library.

在这个快速教程中,我们展示了如何使用Guava库的ClassToInstanceMap的例子。

The implementation of these examples can be found in these examples can be found in the GitHub project.

这些例子的实现可以在这些例子中找到,可以在GitHub项目中找到。