Class JustALong

  • All Implemented Interfaces:
    Serializable

    public abstract class JustALong
    extends Object
    implements Serializable
    Base class for creating simple "typed long" value classes, for cases when you do not want to pass around simple longs for numbers that has certain (business-) semantics in your application, even though a long does adequately express the value. By extending this class you do not have to implement the equals and hashcode. You would not refer to this class other than with an extends JustALong decalaration in your class definition.

    This is a special case implementation of JustA to avoid the boxing cost for primitive longs. The typical use for this are for ids, and classes where instances are uniquely distinguished by their long ids, and equals/hashcode will exercise correct behavior when only operating on the id. Any additional fields in an extending class should usually be declared final.

    A common pattern for using this class would be:

     interface WithId {
    
       static JustAnId of(long id) {
         return new JustAnId(id);
       }
    
       final class JustAnId extends JustALong implements WithId {
    
          private JustAnId(String id) {
            super(id);
          }
    
          public long getId() {
            return theValue;
          }
        }
    
    
        long getId();
     }
    See Also:
    is the generic version for any (reference-) type., Serialized Form
    • Field Detail

      • theLong

        protected final long theLong