views:

74

answers:

1

I am looking for realistic examples of immutable classes developed in Java. In order to allow finding those classes which are intended to be immutable, it would be helpful if the code used an @Immutable annotation, or otherwise documented classes as intended to be immutable. I also need to be able to study the source code of those classes, so a project with any "read only" license is suitable.

In regards to the @Immutable annotation used, it doesn't matter which @Immutable it is. So whether the annotation is from JSR-305 or FindBugs, or in-house, etc., it doesn't matter, just that the simple name of the annotation is "Immutable". Similarly, if the project uses a consistent way of marking classes as immutable (class name convention, javadoc, marker interfaces, etc.) such that I can search the codebase and tell a mutable class from an immutable one, this is also what I'm looking for.

Are there any examples of open-source code containing extensive examples of immutable classes?

+3  A: 

Most classes in these libraries are immutable:

No Immutable annotation in the code, tough, AFAIK.

EDIT The project uses documentation mostly. One key aspect though: if you don't want your classes to be mutable, provide no (public) setters. If client code cannot modify the state of your objects, then you have got effectively an immutable class. This is a principle, both Joda time as well as joda money use. Off the top of my head, I could not name a single mutable class in either libraries (I have so far used only the immutable ones, and never missed mutable versions at all.)

Dirk
I will look into these two, thanks. Although there's no Immutable annotation, do you know of any other convention the project uses to tell immutable from mutable?
Grundlefleck
@Dirk (re: edit) for clarity's sake, I'm not looking for *how* to write immutable classes, just trying to find example classes that real developers come up with in order to make a certain code analysis tool a bit more robust :-)
Grundlefleck
Looking at the Joda Time code, there seems to be consistent mentions of "xxx is thread safe and immutable" in javadoc. Looks very useful for my purposes, +1.
Grundlefleck