immutability

Is there a bidirectional multimap persistent data structure?

不羁岁月 提交于 2019-12-01 02:07:46
问题 In other words, can we model many to many relationships in a persistent data structure efficiently? A pair of unidirectional multimaps was suggested. However, I'm not sure how this would work well for removal in a persistent data structure. Let's take the case where we have keys 1..4 to values "1".."4" and let's say they each refer to all the others, so we have two maps that look very similar for both directions: {1 => ["2","3","4"], 2 => ["1","3","4"], ...} {"1" => [2,3,4], "2" => [1,3,4], .

Why Lucene doesn't support any type of update to an existing document

ぃ、小莉子 提交于 2019-12-01 01:47:29
问题 My use case involves index a Lucene document, then on multiple future occasions add terms that point to this existing doc, that's without deleting and re-adding the entire document for each new term (because of performance, and not keeping the original terms). I do know that a document can not be truly updated. My question is why? Or more precisely, why are all forms of updates (terms, stored fields) not supported? Why it's not possible to add another term to point to an existing document -

Immutable readonly reference types & FXCop Violation: Do not declare read only mutable reference types

浪尽此生 提交于 2019-12-01 01:20:07
问题 I have been trying to wrap my head around this FXCop violation "DoNotDeclareReadOnlyMutableReferenceTypes" MSDN: http://msdn.microsoft.com/en-us/library/ms182302%28VS.80%29.aspx Code from MSDN which would cause this violation: namespace SecurityLibrary { public class MutableReferenceTypes { static protected readonly StringBuilder SomeStringBuilder; static MutableReferenceTypes() { SomeStringBuilder = new StringBuilder(); } } } From Jon's answer here and here , I understand that the field

What is the advantage of annotating an immutable Java class with @Immutable?

瘦欲@ 提交于 2019-12-01 01:08:20
问题 I get the concept of immutability, and why it is a good idea to make DTOs immutable. I also notice that Java has an @Immutable annotation that we can use to annotate immutable classes. My question is: what does annotating a Java class as @Immutable give us? Are there any library features that only work on classes annotated in this way? 回答1: The annotation documents the fact that your class is immutable and tells the users of the class that you have followed the contract defined in the

Why Wrapper class like Boolean in java is immutable?

◇◆丶佛笑我妖孽 提交于 2019-11-30 20:45:41
I can't see the reason why the Boolean wrapper classes were made Immutable. Why the Boolean Wrapper was not implemented like MutableBoolean in Commons lang which actually can be reset. Does anyone have any idea/understanding about this ? Thanks. Because 2 is 2. It won't be 3 tomorrow. Immutable is always preferred as the default, especially in multithreaded situations, and it makes for easier to read and more maintainable code. Case in point: the Java Date API, which is riddled with design flaws. If Date were immutable the API would be very streamlined. I would know Date operations would

python tuple is immutable - so why can I add elements to it

做~自己de王妃 提交于 2019-11-30 20:28:50
I've been using Python for some time already and today while reading the following code snippet: >>> a = (1,2) >>> a += (3,4) >>> a (1, 2, 3, 4) I asked myself a question: how come python tuples are immutable and I can use an += operator on them (or, more generally, why can I modify a tuple)? And I couldn't answer myself. I get the idea of immutability, and, although they're not as popular as lists, tuples are useful in python. But being immutable and being able to modify length seems contradictory to me... 5 is immutable, too. When you have an immutable data structure, a += b is equivalent to

Immutable views of mutable types

半城伤御伤魂 提交于 2019-11-30 20:13:38
I have a project where I need to construct a fair amount of configuration data before I can execute a process. During the configuration stage, it's very convenient to have the data as mutable. However, once configuration has been completed, I'd like to pass an immutable view of that data to the functional process, as that process will rely on configuration immutability for many of its computations (for instance, the ability to pre-compute things based on initial configuration.) I've come up with a possible solution using interfaces to expose a read-only view, but I'd like to know if anybody

Is guava's ImmutableXXX really immutable?

落花浮王杯 提交于 2019-11-30 19:31:43
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

Strings are immutable - that means I should never use += and only StringBuffer?

南楼画角 提交于 2019-11-30 18:41:51
Strings are immutable, meaning, once they have been created they cannot be changed. So, does this mean that it would take more memory if you append things with += than if you created a StringBuffer and appended text to that? If you use +=, you would create a new 'object' each time that has to be saved in the memory, wouldn't you? Yes, you will create a new object each time with +=. That doesn't mean it's always the wrong thing to do, however. It depends whether you want that value as a string, or whether you're just going to use it to build the string up further. If you actually want the

Is there an easy way to make an immutable version of a class?

百般思念 提交于 2019-11-30 18:35:25
Is there an easy way to make an instance immutable? Let's do an example, I have a class holding a lots of data fields (only data, no behavior): class MyObject { // lots of fields painful to initialize all at once // so we make fields mutable : public String Title { get; set; } public String Author { get; set; } // ... } Example of creation: MyObject CreationExample(String someParameters) { var obj = new MyObject { Title = "foo" // lots of fields initialization }; // even more fields initialization obj.Author = "bar"; return obj; } But now that I have fully created my object, I don't want the