Immutable Collections
There are a few classes that are truly immutable (not like those pseudo immutables from Guava and Apache Commons):
Array
: implementsCollection
;ArraySet
: implementsSet
;ArraySortedSet
: implementsSortedSet
;ArrayMap
: implementsMap
.
All of them are annotated with @Immutable
.
The only dependency you need is (you can also download jcabi-immutable-1.5.0.jar
and add it to the classpath):
<dependency> <groupId>com.jcabi</groupId> <artifactId>jcabi-immutable</artifactId> <version>1.5.0</version> </dependency>
Limitation
Encapsulated objects with exposed mutators can mutate their state.
Cutting Edge Version
If you want to use current version of the product, you can do it with this configuration in your pom.xml
:
<repositories> <repository> <id>oss.sonatype.org</id> <url>https://oss.sonatype.org/content/repositories/snapshots/</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.jcabi</groupId> <artifactId>jcabi-immutable</artifactId> <version>2.0-SNAPSHOT</version> </dependency> </dependencies>