com.surelogic
Annotation Type Immutable


@Documented
@Target(value=TYPE)
public @interface Immutable

The class to which this annotation is applied is immutable. This means that its state cannot be seen to change by callers, which implies that

Immutable objects may still have internal mutable state for purposes of performance optimization; some state variables may be lazily computed, so long as they are computed from immutable state and that callers cannot tell the difference.

Immutable objects are inherently thread-safe; they may be passed between threads or published without synchronization.

This annotation is trusted, i.e., it is not verified by analysis. Presently its use is for documentation and to quiet warnings similar to ThreadSafe. It is intended that this annotation will be verified in the future.

Implementation note: This annotation is derived from @Immutable proposed by Brian Goetz and Tim Peierls in the book Java Concurrency in Practice (Addison-Wesley 2006) we have simply adapted it to have semantics as a promise. Further, the annotation in net.jcip.annotations may be used instead of this one with the same tool behavior. One difference between the two annotations is that the one in net.jcip.annotations has retention policy of RetentionPolicy.RUNTIME while the one in com.surelogic defaults to RetentionPolicy.CLASS.

Examples:

The immutable Point class below is considered thread-safe.
 @Immutable
 public class Point {
 
   final int f_x;
   final int f_y;
 
   public Point(int x, int y) {
     f_x = x;
     f_y = y;
   }
 
   public int getX() {
     return f_x;
   }
 
   public int getY() {
     return f_y;
   }
 }
 

Javadoc usage notes:

This annotation may placed in Javadoc, which can be useful for Java 1.4 code which does not include language support for annotations, via the @annotate tag.
 /**
  * @annotate Immutable
  */
 public class Point {
   ...
 }
 

See Also:
ThreadSafe



Copyright © 2010 Surelogic, Inc.. All Rights Reserved.