mercredi 20 septembre 2017

Design practice of using java.util.Function in non-stream context

I'm currently defining a Function within a method to help w/ two objectives:

  1. Simplify the if block in a section of non-stream driven code,
  2. Encapsulating the function within the method for which it's only relevant.

Example (for illustration only):

public String someMethod(String parm1, String parm2) {
  final Function<String, Boolean> isValidString = parm -> parm.equals("Test") || parm.equals("Test2"); 
  ...
  String parm1 = ...
  if (parm1 != null && isValidString.apply(parm)) {
    ... do something
 }
 ...

In my situation the Function method is a bit more complex and covers 1 specific business case I'm checking for within the if block.

Is there a standard design pattern or anti-pattern for this or recommendation? I'm specifically looking for answers more than just personal preference.

Aucun commentaire:

Enregistrer un commentaire