immutability

Possible ways of making an Object immutable

混江龙づ霸主 提交于 2019-12-12 08:16:42
问题 I am looking for some efficient way for building a immutable class, just like Java's String class. 回答1: All the fields must be private and preferably final Ensure the class cannot be overridden - make the class final, or use static factories and keep constructors private Fields must be populated from the Constructor/Factory Don't provide any setters for the fields Watch out for collections. Use Collections.unmodifiable* . Also, collections should contain only immutable Objects All the getters

Immutable Object in Objective-C: Big init method?

风格不统一 提交于 2019-12-12 07:38:45
问题 I want to have an Object with immutable fields in Objective-C. In C#, I would use Properties with private setters and a big constructor. What would I use in Objective-C? Using @property doesn't seem to allow me to declare the setter as private. Using initWithData: (NSString*) something createDate: (NSDate*) date userID: (long) uid seems overly verbose if I have more than 4 properties to set. Would I declare the getters in the .h file and the setters only in .m? I need to use retain or copy on

Why String class is immutable even though it has a non -final field called “hash”

怎甘沉沦 提交于 2019-12-12 07:30:01
问题 I was reading through Item 15 of Effective Java by Joshua Bloch. Inside Item 15 which speaks about 'minimizing mutability' he mentions five rules to make objects immutable. One of them is is to make all fields final . Here is the rule : Make all fields final : This clearly expresses your intent in a manner that is enforced by the system. Also, it is necessary to ensure correct behavior if a reference to a newly created instance is passed from one thread to another without synchronization, as

Convert java.util.IdentityHashMap to scala.immutable.Map

时光毁灭记忆、已成空白 提交于 2019-12-12 04:45:59
问题 What is the simplest way to convert a java.util.IdentityHashMap[A,B] into a subtype of scala.immutable.Map[A,B] ? I need to keep keys separate unless they are eq . Here's what I've tried so far: scala> case class Example() scala> val m = new java.util.IdentityHashMap[Example, String]() scala> m.put(Example(), "first!") scala> m.put(Example(), "second!") scala> m.asScala // got a mutable Scala equivalent OK res14: scala.collection.mutable.Map[Example,String] = Map(Example() -> first!, Example(

updating object within redux state array not re-rendering react component [duplicate]

对着背影说爱祢 提交于 2019-12-12 04:43:20
问题 This question already has an answer here : My Redux state has changed, why doesn't React trigger a re-render? (1 answer) Closed 2 years ago . I am trying to update a single chef within the chefs array in my redux state. An example of the array is as follows: [ { _id: 1, name: 'first chef' }, { _id: 2, name: 'second chef' }, { _id: 2, name: 'third chef' } ] I make a call to the API which then returns me the updated chef object. I basically find the related chef object in my current state,

Implementing a Number System in Java: Mutable vs. Immutable

时光总嘲笑我的痴心妄想 提交于 2019-12-12 04:38:29
问题 I am implementing classes for rational numbers, but the problems and issues are essentially the same for complex numbers as well as other classes intended to be used in applications with a significant number of calculations performed on a given mathematical object. In the libraries distributed with the JRE and in many third-party libraries, number classes are immutable. This has the advantage that "equals" and "hashcode" can be reliably implemented together as intended. This will enable

why my redux state not updating

自闭症网瘾萝莉.ら 提交于 2019-12-12 02:09:25
问题 state not updating.when action is dispatched the state should update to isAuthenticated to true..but the state not updating..redux returning the initial state not the updated state. export function setCurrentUser(user) { console.log(user); return { type: "SET_CURRENT_USER", user }; } ... export function login(userData) { return dispatch => { return axios.post('/user/auth',userData).then((res) => { const { status, sessionId, username, error} = res.data; if(status === "success"){ dispatch

Defining my own immutable array in Java

随声附和 提交于 2019-12-12 01:38:16
问题 I know that declaring an array final does not make it immutable. I can however define a wrapper class that functions like an immutable array, e.g. public class ImmutableIntArray { private int[] array; public ImmutableIntArray(int[] array) { this.array = (int []) array.clone(); } public int get(int i) { return array[i]; } public int length() { return array.length; } public static void main(String[] args) { ImmutableIntArray a = new ImmutableIntArray(new int[]{1, 2, 3}); for (int i = 0; i < a

Python Bit Operations on a list of Int's

心不动则不痛 提交于 2019-12-11 19:14:04
问题 What I want to do that does not work: List of ints... BebossArray=[0 for i in xrange(1024)] My bit functions (There are all the usual set,clear,toggle, test but here is set) def setBit(dint, offset): mask = 1 << offset dint=(dint|mask) return so I would like to set a bit in one of the ints in the list... setBit(DebossArray[index],3) This, of course, does not work. The assignment in setBit just creates a new object and DebossArray[index] stays put as expected. So I get that INT's are

Doubts related to volatile , immutable objects, and their use to acheive synchronization

五迷三道 提交于 2019-12-11 18:59:17
问题 I was reading book "java concurrency in practice" and end up with some doubts after few pages. 1) Voltile with non premitive data types : private volatile Student s; what is significance of volatile when it comes with non premitive data types ? (I think in this case only think that is sure to be visible to all threads is what Strudent object s is pointing currently and it is possible one thread A modifies some internal member of student and that is not visible to other threads. Am I right ??)