简体   繁体   中英

How to make a generic parameter optional in interface in java?

I have an interface which has 2 generic parameters.But abstract method inside sometimes is needed to accept only 1 generic parameter.How can i make that happen ? The way i see it i can't make the generics optional and also if i write an overloaded method inside the interface it will be unnecessarily implemented in some java classes(which will be implementing that interface) where it's not needed. Now i am passing the parameterized which is needed twice both in the class header while implementing the interface and also inside a function,since i compulsorily have to pass 2 arguments.I am stuck.Any direction ?

public interface ITaskPermissionHandler<T, V> {

    String getType();

    boolean hasPermission(T clazz, UserRef user, String type, V clazz1);
}

public class CreateTaskPermission implements ITaskPermissionHandler<Task,Task> {

    @Override
    public String getType() {
        return TaskConstants.CREATE_TASK;
    }

    @Override
    public boolean hasPermission(Task task, UserRef user, String type, Task      task1) {
        return type.equalsIgnoreCase(TaskConstants.CREATE_TASK);
    }
}

Your method takes 4 parameters. You cannot suddenly make it take 3.

If you want a method with 3 parameters, then it is a different method (overload), and you need a different interface with that method.

You can however do what the java.util.function interfaces do, and extend the interface with a simpler version, like how BinaryOperator<T> extends BiFunction<T,T,T> .

Like one of these ways:

interface ISimplePermissionHandler<T> extends ITaskPermissionHandler<T, T> {
    default boolean hasPermission(T clazz, UserRef user, String type) {
        return hasPermission(clazz, user, type, clazz);
    }
}   
class CreateTaskPermission implements ISimplePermissionHandler<Task> {
    @Override
    public String getType() {
        return TaskConstants.CREATE_TASK;
    }
    @Override
    public boolean hasPermission(Task task, UserRef user, String type, Task task1) {
        return type.equalsIgnoreCase(TaskConstants.CREATE_TASK);
    }
}
interface ISimplePermissionHandler<T> extends ITaskPermissionHandler<T, T> {
    @Override
    default boolean hasPermission(T clazz, UserRef user, String type, T clazz1) {
        return hasPermission(clazz, user, type);
    }
    boolean hasPermission(T clazz, UserRef user, String type);
}   
class CreateTaskPermission implements ISimplePermissionHandler<Task> {
    @Override
    public String getType() {
        return TaskConstants.CREATE_TASK;
    }
    @Override
    public boolean hasPermission(Task task, UserRef user, String type) {
        return type.equalsIgnoreCase(TaskConstants.CREATE_TASK);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM