Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array is mutable #37

Open
pluresideas opened this issue Nov 26, 2019 · 6 comments
Open

Array is mutable #37

pluresideas opened this issue Nov 26, 2019 · 6 comments

Comments

@pluresideas
Copy link
Contributor

pluresideas commented Nov 26, 2019

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());
    }
}
@0crat
Copy link

0crat commented Nov 26, 2019

@yegor256/z please, pay attention to this issue

@0crat
Copy link

0crat commented Nov 26, 2019

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

@yegor256
Copy link
Member

@pluresideas how would you suggest to solve this?

@pluresideas
Copy link
Contributor Author

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

@yegor256
Copy link
Member

yegor256 commented Aug 5, 2020

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

@pluresideas
Copy link
Contributor Author

Yes, I will submit a pull request.

pluresideas added a commit to pluresideas/jcabi-immutable that referenced this issue Aug 16, 2020
yegor256 added a commit that referenced this issue Sep 1, 2020
#37 Limitation: Encapsulated objects with exposed mutators can mutate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants