jcabi-immutable icon indicating copy to clipboard operation
jcabi-immutable copied to clipboard

Array is mutable

Open pluresideas opened this issue 5 years ago • 6 comments

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());
    }
}

pluresideas avatar Nov 26 '19 19:11 pluresideas

@yegor256/z please, pay attention to this issue

0crat avatar Nov 26 '19 19:11 0crat

@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!

0crat avatar Nov 26 '19 19:11 0crat

@pluresideas how would you suggest to solve this?

yegor256 avatar Dec 16 '19 21:12 yegor256

@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 avatar Dec 19 '19 02:12 pluresideas

@pluresideas sounds reasonable. Would you be interested in submitting a pull request?

yegor256 avatar Aug 05 '20 12:08 yegor256

Yes, I will submit a pull request.

pluresideas avatar Aug 05 '20 14:08 pluresideas