使用guava immutable集合作为方法参数和/或返回类型 [英] Use of guava immutable collection as method parameter and/or return type

查看:112
本文介绍了使用guava immutable集合作为方法参数和/或返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定ImmutableList的最佳实践。以下是一个简单的例子,有助于解决我的问题:

I am trying to determine what the best practices would be for an ImmutableList. Below is a simplistic example that will help drive my questions:

例如:

public ImmutableCollection<Foo> getFooOne(ImmutableList<Foo> fooInput){ 
   //.. do some work
   ImmutableList<Foo> fooOther = // something generated during the code
   return fooOther;
}

public Collection<Foo> getFooTwo(List<Foo> fooInput){
   //.. do some work
   List<Foo> fooOther = // something generated during the code
   return ImmutableList.copyOf(fooOther);
}

public void doSomethingOne(){
  ImmutableCollection<Foo> myFoo = getFooOne(myList);
  ...
  someOtherMethod(myFoo);
}

public void doSomethingTwo(){
  Collection<Foo> myFoo = getFooOne(myList);
  ...
  someOtherMethod(myFoo);
}

我的问题:


  1. 哪种在应用程序中最有意义? [doSomethingOne和getFooOne]或[doSomethingTwo和fooTwo]?换句话说,如果你知道你正在使用ImmutableCollections,是否有意义来回继续进行转换并执行copyOf(),或者只是在所有地方使用Immutable?

  1. Which makes the most sense to use in an application? [doSomethingOne and getFooOne] or [doSomethingTwo and fooTwo]? In other words if you know you are using ImmutableCollections does it make sense to keep casting back and forth and doing copyOf(), or just use Immutable everywhere?

这些示例是公共方法,可能意味着其他人使用它们。如果方法是私有的并且在内部使用,这些答案中的任何一个都会改变吗?

These examples are public methods which could imply that other people use them. Would any of these answers change if the methods were private and used internally?

如果用户尝试向不可变列表添加任何内容,则会抛出异常。因为他们可能没有意识到这一点,显然返回一个ImmutableCollection而不是集合会更有意义吗?

If a user tries to add anything to an immutable List an exception will be thrown. Because they may not be aware of this, wouldn't it make more sense to explicitly return an ImmutableCollection instead of a Collection?


推荐答案

一般来说,明智的做法是不要在声明的返回类型中提交特定的实现,但我们将不可变类型视为异常。声明退货类型不可变* 的原因有几个:

In general, it's wise not to commit to a specific implementation in your declared return type, but we think of the immutable types as an exception. There are a few reasons to declare a return type of Immutable*:


  • 他们记录您正在返回快照,而不是实时视图。

  • 他们记录调用者无法改变结果。

  • 他们的文档保留了该插入顺序(在您的用例中可能会或可能不会很重要)。

  • 他们记录该集合不包含 null

  • 有人可能想要 asList() reverse()方法。

  • 如果他希望分配给,您可以为某人 copyOf()打电话不可变* 字段。 (但请注意,如果他确实包含 copyOf(),它将为大多数不可变输入短路,即使您没有声明返回类型。)

  • They document that you're returning a snapshot, not a live view.
  • They document that the caller can't mutate the result.
  • They document that insertion order is preserved (which may or may not be significant in your use case).
  • They document that the collection won't contain null.
  • Someone might want the asList() or reverse() method.
  • You may save someone a copyOf() call if he wishes to assign to an Immutable* field. (But note that, if he does include copyOf(), it will short-circuit for most immutable inputs, even if you don't declare the return type.)

基本上,我只是来自 https://github.com/google/guava/wiki/TenThingsAboutImmutableCollections ,您可能需要完整查看。

Basically, I'm just cribbing from https://github.com/google/guava/wiki/TenThingsAboutImmutableCollections, which you may want to check out in its entirety.

这篇关于使用guava immutable集合作为方法参数和/或返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆