是guava的ImmutableXXX真的不可变吗? [英] Is guava's ImmutableXXX really immutable?

查看:207
本文介绍了是guava的ImmutableXXX真的不可变吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用番石榴一段时间,真正的信任,直到昨天我的例子,让我想到的例子。长整句,这里是:

  public static void testGuavaImmutability(){
StringBuilder stringBuilder = new StringBuilder第一部分);
ImmutableList< StringBuilder> myList = ImmutableList.of(stringBuilder);
System.out.println(myList.get(0));
stringBuilder.append(attached);
System.out.println(myList.get(0));
}

运行此操作后,您可以看到 ImmutableList 已更改。如果这里涉及两个线程,可能发生没有看到另一个的更新。



此外,让我非常不耐烦的答案是,Item15在有效Java,第五点说:



在构造函数中创建防御副本 - 看起来很逻辑。



查看ImmutableList的源代码,我看到:

  SingletonImmutableList(E element){
this.element = checkNotNull(element);
}

因此,实际上没有拷贝,虽然我不知道一个泛型



那么,为什么它们被称为Immutable呢?


<



不可变对象永远不会改变,但是它引用的任何东西都可能改变。深不变性要强得多:基础对象和任何可以从导航到的对象都会改变。



情况。当您创建自己的类,其类型为 Date 的字段时,该对象拥有该日期;它真的是它的一部分。因此,你应该制作防御副本(在出路!),以提供深不可变性。



并不真正拥有其元素。他们的状态不被认为是集合的状态的一部分;它是一个不同类型的类 - 一个容器。 (另外,正如你所说,它不知道正在使用什么元素类型,所以它不知道如何复制元素。)



另一个回答说,Guava集合应该使用术语 unmodifiable 。但在集合的上下文中,术语不可修改不可变之间存在非常明确的区别,它与浅层和深层不变性无关。 Unmodifiable表示无法通过您的参考更改此实例; 不可变的意味着这个实例不能改变,期间,无论是你还是任何其他actor。


I have been using guava for some time now and truly trusted it, until I stumbled of an example yesterday, which got me thinking. Long story short, here it is:

 public static void testGuavaImmutability(){
     StringBuilder stringBuilder = new StringBuilder("partOne");
     ImmutableList<StringBuilder> myList = ImmutableList.of(stringBuilder);
     System.out.println(myList.get(0));
     stringBuilder.append("appended");
     System.out.println(myList.get(0));
 }

After running this you can see that the value of an entry inside an ImmutableList has changed. If two threads were involved here, one could happen to not see the updated of the other.

Also the thing that makes me very impatient for an answer is that Item15 in Effective Java, point five says this:

Make defensives copies in the constructor - which seems pretty logic.

Looking at the source code of the ImmutableList, I see this:

 SingletonImmutableList(E element) {
     this.element = checkNotNull(element);
 }

So, no copy is actually made, although I have no idea how a generic deep copy would be implemented in such a case (may be serialization?).

So.. why are they called Immutable then?

解决方案

What you're getting at here is the difference between immutable and deeply immutable.

An immutable object will never change, but anything that it refers to might change. Deep immutability is much stronger: neither the base object nor any object you can navigate to from it will change.

Each is appropriate in its own situations. When you create your own class that has a field of type Date, that date is owned by your object; it's truly a part of it. Therefore, you should make defensive copies of it (on the way in and the way out!) to provide deep immutability.

But a collection does not really "own" its elements. Their states are not considered part of the collection's state; it is a different type of class -- a container. (Furthermore, as you allude, it has no deep knowledge of what element type is being used, so it wouldn't know how to copy the elements anyway.)

Another answer states that the Guava collections should have used the term unmodifiable. But there is a very well-defined difference between the terms unmodifiable and immutable in the context of collections, and it has nothing to do with shallow vs. deep immutability. "Unmodifiable" says you cannot change this instance, via the reference you have; "immutable" means this instance cannot change, period, whether by you or any other actor.

这篇关于是guava的ImmutableXXX真的不可变吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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