Class JustA<T>

java.lang.Object
no.digipost.util.JustA<T>
Type Parameters:
T - The type of wrapped value. Should be an immutable value-type.
All Implemented Interfaces:
Serializable

public abstract class JustA<T> extends Object implements Serializable
Base class for creating simple "typed primitives" value classes. Typically for when you do not want to pass around simple Strings for values that has certain (business-) semantics in your application, even though a String 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 JustA<SomeType> decalaration in your class definition.

A common pattern for using this class would be:

 interface WithName {

   static JustAName of(String name) {
     return new JustAName(name);
   }

   final class JustAName extends JustA<String> implements WithName {

      private JustAName(String name) {
        super(name);
      }

      public String getName() {
        return theValue;
      }
    }


    String getName();
 }

This yields certain benefits, as you now have an interface (WithName) to use with your more complex domain types, which among other stuff, having a name. And you have a neat way to pass typed simple values using WithName.of("John Doe"). This especially enhances the readability of method invocations with multiple arguments, as you must explicitly state the semantics of each argument. Say a query of some sort:

db.findPerson(WithName.of("John Doe"), WithPhonenumber.of("555-98437"))
If the method parameters for some reason are refactored to switch places, the code invoking the method will not compile anymore, as the arguments are not given in the correct order even though they are really just Strings.
See Also:
  • Field Details

    • theValue

      protected final T theValue
  • Constructor Details

  • Method Details