Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: how to limit argument's values that can be passed to a method

Tags:

java

Suppose I have a method that takes one int as a argument,

suppose the method expects only the values 0, 1, 2 and not other int.

Is there a way to "force" the method to accept only 0, 1, 2?

like image 483
Lisa Anne Avatar asked Feb 22 '15 13:02

Lisa Anne


People also ask

How do you restrict parameters in Java?

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.

How do you reduce the number of parameters in a method?

Well, if a method has five or more parameters, then you should consider refactoring that method and decreasing the number of parameters. This is where Introduce Parameter Object refactoring comes into play. In short, this refactoring is a way of replacing a method's argument list with a single parameter object.

How does Java deal with data types when passing arguments to methods?

In Java, you can pass an argument of any valid Java data type into a method. This includes simple data types such as doubles, floats and integers as you saw in the computePayment() method above, and complex data types such as objects and arrays. Here's an example of a method that accepts an array as an argument.

What is the limit to the number of parameters a method can have?

The number of method parameters is limited to 255 by the definition of a method descriptor (§4.3. 3), where the limit includes one unit for this in the case of instance or interface method invocations.


2 Answers

You would have to create a custom data type that can only take the values 0, 1 and 2. You can't use an ordinary int.

In this case you could use an enum:

enum ZeroOneOrTwo {
    ZERO(0),
    ONE(1),
    TWO(2);
    public final int val;
    private ZeroOneOrTwo(int val) {
        this.val = val;
    }
}

and use it as follows:

void myMethod(ZeroOneOrTwo arg) {
    System.out.println("Int value: " + arg.val);
}

If you're forced to take an int as argument (if you're implementing an interface for instance) you can resort to throwing an IllegalArgumentException if the given value is out of range. But generally you would want to let the compiler catch the problem rather than having to deal with it at runtime.

like image 141
aioobe Avatar answered Oct 06 '22 10:10

aioobe


Maybe not applicable in every case (but you didn't specify further what you're trying to accomplish, so maybe it's worth a shot), but maybe you might consider creating three methods instead of one? Or using inheritance, since you're using an OOP language?

For example, instead of coding some information into an int, it might be better coding style to change it.

So instead of ...

void addEmployee(int type) {
    switch(type) {
        case 0: // add regular eymploee
        case 1: // add manager
        case 2: // add CEO
    }
}

... consider this ...

void addRegularEmployee() { 
    // ... 
}
void addManager() { 
    // ... 
}
void addCEO() { 
    // ... 
}

... or even ...

void add(Employee e) {
    // ...
}
like image 29
Michael Borkowski Avatar answered Oct 06 '22 11:10

Michael Borkowski