10/12/2017 - 3:41 PM


Limiting a method's input so only certain values are accepted, eliminating the possibility of unknown types

/*Here we will demonstrate how to use @intdef to specify a small set of integer values 
and then how to limit a method's input so that only those values can be accepted.
This is an excellent way to ensure you get no unknown types.

To enable these annotations in AST, go to 
->Project Structure
->Select app module
->Select Dependecy Tab from the right section
->click the "+" button 
-> Select Library
-> Select Support.Annotation option


public class MainActivity extends AppCompatActivity{
 //Setting the IntDef
    //Retention tells when to retain the Annotations (Source, class, runtime)
    //IntDef defines our allowable values (enum)
    //need interface to define where it will be implemented
    public @interface locationStatus{} 
  //The actual values
    public static final int LOCATION_STATUS_OK = 0;
    public static final int LOCATION_STATUS_SERVER_DOWN = 1;
    public static final int LOCATION_STATUS_SERVER_INVALID = 2;
    public static final int LOCATION_STATUS_UNKNOWN = 3;

  //We want only the values we defined in @IntDef, so we leave an annotation for
  //the interface that uses IntDef
  public void doTheThingWithIntDefs(@setLocation int locationStatus){