Skip to main content
2 of 2
added 198 characters in body
deamon
  • 886
  • 5
  • 18

java.util.function.Function as an interface for an mutable object

I have an interface with only a single method that could be replaced by java.util.function.Function from the JDK:

interface DataModel {
  boolean apply(Map<String, Integer> input);
}

The replacement would be

Function<Map<String, Integer>, Boolean>

However, I wonder whether I should keep this interface since at least one implementation (other implementations might follow) contains mutable state and is not thread safe. A factory creates a new instance for each call.

So while technically possible, the functional interface might semantically not the best solution.

The API documentation doesn't say something about mutability, but maybe it would violate the principle of least astonishment or some other kind of contract. Is there any such contract?

Would you go with Function or with the custom DataModel interface and why?

deamon
  • 886
  • 5
  • 18