jcabi-immutable
jcabi-immutable copied to clipboard
Array is mutable
Array consisting of mutable objects is mutable which contradicts the project's documentation.
The following tests fails:
/**
* Array must be immutable
*/
@Test
public void isImmutable() {
final Mutable[] ints = new Mutable[]{new Mutable(1), new Mutable(2), new Mutable(3)};
final Array<Mutable> array = new Array<Mutable>(ints);
ints[1].setState(200);
Assert.assertTrue(
Arrays.equals(array.toArray(), new Mutable[]{new Mutable(1), new Mutable(2), new Mutable(3)})
);
}
public class Mutable {
Integer state;
public Mutable(Integer state) {
this.state = state;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
@Override
public int hashCode() {
return state;
}
@Override
public boolean equals(Object obj) {
return state.equals(((Mutable)obj).getState());
}
}
@yegor256/z please, pay attention to this issue
@pluresideas/z this project will fix the problem faster if you donate a few dollars to it; just click here and pay via Stripe, it's very fast, convenient and appreciated; thanks a lot!
@pluresideas how would you suggest to solve this?
@yegor256 how about adding to the documentation the text below?
Limitations: Encapsulated objects in these library collections can mutate their state if these objects expose mutators.
Because of the significancy of this restriction, I wonder whether Javadoc for each of the collections should also be updated with the limitation statement so that it would be harder to overlook it and use this lib incorrectly.
Thoughts?
@pluresideas sounds reasonable. Would you be interested in submitting a pull request?
Yes, I will submit a pull request.