Home » Android » android – How to only allow certain values as parameter for a method in Java?

android – How to only allow certain values as parameter for a method in Java?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to write a method that only takes certain values for a parameter, like f.e. in the Toast class in Android. You can only use Toast.LENGTH_SHORT or Toast.LENGTH_LONG as duration for the method makeText(Context context, int resId, int duration). I had a look at the source code of the Toast class but found nothing there. How can I achieve that?

How to&Answers:

Use an Enum Type, from the Java Tutorial,

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

As an example,

public enum MyEnum {
  ONE, TWO;
}

public static void myMethod(MyEnum a) {
  // a must be MyEnum.ONE or MyEnum.TWO (in this example)
}

Edit

To get String(s) from your enum types you can add field level values (which must be compile time constants) with something like,

public enum MyEnum {
  ONE("uno"), TWO("dos");
  MyEnum(String v) {
    value = v;
  }
  private String value;
  public String getValue() {
    return value;
  }
}

Answer:

You can use @IntDef or @StringDef annotations for your methods like this:

@Retention(SOURCE)
@IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
public @interface NavigationMode {}
public static final int NAVIGATION_MODE_STANDARD = 0;
public static final int NAVIGATION_MODE_LIST = 1;
public static final int NAVIGATION_MODE_TABS = 2;
...
public abstract void setNavigationMode(@NavigationMode int mode);
@NavigationMode
public abstract int getNavigationMode();

Answer:

To use ints as is done with the Toast class, you can do something like this:

public class MyClass {
    //by convention, constant names are all caps
    public static final int VALUE_ONE = 0;
    public static final int VALUE_TWO = 1;


    public void myMethod(int value) throws InvalidParameterException {
        if(value != VALUE_ONE || value != VALUE_TWO) {
            throw new InvalidParameterException();
            //or set default value instead of throwing an exception
        }
        else {
            //your code
        }
    }
}

VALUE_ONE and VALUE_TWO are static and final, meaning that throughout the entire application there will only be one instance of that variable, and its value will never change (if you know C, it is similar to a #DEFINE). Thus, when someone passes MyClass.VALUE_ONE as an argument, you know exactly what it is, every time, while the caller doesn’t necessarily need to know the integer value behind the constant. And then you will want to do a runtime check to make sure that what was passed in was one of the valid values, and if not, throw an exception. Or, if the value passed in isn’t very critical, you could just set a default value if the argument is incorrect rather than throw an exception.