I am using an API with several operations to be performed, and it is currently under development. In order to isolate it, so I could deal easily with those constant changes, I've created a functional interface Operation
with a method perform
, like this:
public interface Operation<R, T> {
R perform(T arg);
}
All operations I have implements this interface. For instance, the implementation of a find file action would be like this:
public class Finder implements Operation<InputStream, String> {
@Override
public InputStream perform(String arg) {
//Code to find the file
}
}
This class uses an InputStream as return and String as parameter, but other operations could take other parameters and have different returns...
To be able to create any operation, I've created an Enum
called OperationType
to identify each operation and a factory that creates those operations. The factory is below:
public class OperationsFactory {
public static Operation create(OperationType type) {
switch (type) {
case FIND:
return new Finder();
// cases for each operation
default:
throw new InvalidParameterException("Can't create operation type: " + type.toString());
}
}
}
Ok, this is the structure I have. It might not be the best option for you, but that's not the problem I'm trying to solve here. The thing is, when I try to use it I get Unchecked Assignment
warning, and I want to remove it without putting an annotation to ignore such warning (which is bad, in my opinion). Let me put a code of a method that would have this exception:
public InputStream performFindOperation(String path) {
Operation<InputStream, String> findOperation = OperationsFactory.create(OperationType.FIND); //warning here!!
return findOperation.perform(path);
}
So, Java experts, how do I remove this warning?