0

How to create a method that accepts a boolean criteria and a lambda with arguments as a parameter in Java 8?

For Example I have lot of this code.

if(something is true) {
     foo("a", "b", 2)
}

if(something else is true) {
     bar("hello", 1)
}

Want to create a method that accepts boolean and a lambda so I can factor out all the if checks so I can do something like this?

  checkAndCAll(isValid, (a, b, c) -> { if(isValid) foo(a, b, c); })
  checkAndCAll(isValid2, (a, b) -> { if(isValid2) bar(a, b, c); })

If there is something even cleaner please suggest

user1870400
  • 6,028
  • 13
  • 54
  • 115
  • Try variadic paramters list like: fun(boolean cond, Type... params). Or if you want to have some validator then read about functional interfaces in java – Lemonov Jun 30 '17 at 08:51
  • Possible duplicate of [How do I define a method which takes a lambda as a parameter in Java 8?](https://stackoverflow.com/questions/13604703/how-do-i-define-a-method-which-takes-a-lambda-as-a-parameter-in-java-8) – Ramon Marques Jun 30 '17 at 08:55
  • @RamonMarques I don't understand it from that link therefore I asked the question here – user1870400 Jun 30 '17 at 09:01
  • @user1870400 it means that you can't do what you want. Lambdas in java don't work like it is in ruby or whatever other language you may know that lambdas are blocks – Ramon Marques Jun 30 '17 at 09:06

2 Answers2

1

Don't think the way you want to implement is good at all, but anyway - is this what you asked about?

public static void main(String[] args) {
    //how to use
    checkAndCAll(true, new Object[]{"param1", "param2"}, p -> someMethod(p));
    checkAndCAll(true, new Object[]{"param1", 2 , new AtomicInteger(124)}, p -> anotherMethod(p));
}

public static void checkAndCAll(boolean b, Object[] params, Consumer<Object[]> consumer) {
    if (b) consumer.accept(params);
}

public static void someMethod(Object[] args) {
    //some method to execute
    System.out.println(args);
}

public static void anotherMethod(Object[] args) {
    //some method to execute
    System.out.println(args);
}
Vitaliy Moskalyuk
  • 2,463
  • 13
  • 15
  • It's close so The functions can accept any type as a parameter so I just edited my question. Also can you please tell me why you think this is not good at all and what are the alternatives you can suggest ? – user1870400 Jun 30 '17 at 09:12
  • because you can just make simple if(boolean) callMethod(); without lambdas) but i'm not an enterprise developer, so i don't really know which solution is better) I've just answered what you asked in this case) – Vitaliy Moskalyuk Jun 30 '17 at 09:12
1

Can be even simpler, the paramaters can passed directly to the lambda:

public static void main(String[] args) {
    //how to use
    checkAndCAll(true, () -> someMethod("param1,", "param2"));
    checkAndCAll(false, () -> anotherMethod("param1", 123));
}

public static void checkAndCAll(boolean b, Runnable action) {
    if (b) action.run();
}

public static void someMethod(Object param1, Object param2) {
    //some method to execute
}

public static void anotherMethod(String param1, Integer param2) {
    //some method to execute
}

But I think it's not very useful. The traditional 'if' requires comparable amount of code and in addition is more readable. In fact it only duplicates Java's 'if' statement semantics.

manicka
  • 204
  • 2
  • 8
  • Thanks a lot! That looks clean! What if someMethodReturns a int and anotherMethod returns a String? Would the checkAndCall method signature change? – user1870400 Jun 30 '17 at 10:24
  • Yes, it would change, because the `Runnable.run()` takes nothing and returns nothing. You need a functional interface which takes nothing and returns something - [Supplier](https://docs.oraclehttps://stackoverflow.com/editing-help#comment-formatting.com/javase/8/docs/api/java/util/function/Supplier.html) – manicka Jun 30 '17 at 10:31
  • Awesome! Finally what if someMethod or anotherMethod throw an exception? – user1870400 Jun 30 '17 at 10:54
  • Well... It depends on type of the exception, if it's checked or not. In the case of unchecked exception everything will work normally, the exception can propagate through the lambda. In the case of checked exception it woun't compile by default, because it can't be propagated trough the lambda interface, then it's up to you to wrap it into another unchecked exception, catch it somewhere above a unwrap it and rethrow - quite complicated :) – manicka Jun 30 '17 at 11:40