Java Method in Method in Java - java

I was searching several documents for getting answers but I couldn't.
What I want to code is "Methods in Method". Below is example in java8 (Stream API)
private static int sumStream(List<Integer> list) {
return list.stream().filter(i -> i > 10).mapToInt(i -> i).sum();
}
In that codes, list call stream() method, stream() method called filter() method , mapToInt() method called sum() and return.
How can I code like this pattern?? Or, could you tell me search word in google or example codes please?

You are referring to chaining of method calls. This can be achieved when the methods of a class return the instance on which they were called.
For example:
public class A {
public A foo () {
// do something
return this;
}
public A bar () {
// do something
return this;
}
}
Then you can chain the method calls:
A a = new A();
a.foo().bar().foo();

Related

Java Supplier<> defined as lambda. Need help understanding how this code even runs

Sorry for the somewhat unclear title but hopefully you'll see soon that it wasn't so easy to come up with a better one :)
So I have this interface that extends the Java Supplier #FunctionalInterface by defining one new method and also a default implementation of the Supplier.get() method. My default impl of .get() only wraps a call to the other method in some exception handling.
Then in my code I have different "versions" of this Supplier initialized using lambda notation.
Ex: SomeSupplier s = () -> doSomething();
Not sure why I even tried this because logically I don't understand how this even works, which it does. In my mind when I define my supplier using lambda like this I'm essentially overriding the Supplier.get() method. So how is it that in practice it seems to override my SomeSupplier.getSome() method? And leave the default impl of the .get() method intact?
What am I missing here?
Working example code:
public static void main(String[] args) throws InterruptedException {
SomeSupplier s = () -> getSomeOrException(); // "implements" the Supplier.get(), right?
for (int i = 0; i < 100; i++) {
System.out.println(s.get()); // => "Some!" or "null"
Thread.sleep(2);
}
}
private static String getSomeOrException() throws SomeCheckedException {
if (System.currentTimeMillis() % 10 == 0) {
throw new SomeCheckedException("10 %!");
}
return "Some!";
}
private interface SomeSupplier extends Supplier<String> {
#Override
default String get() {
try {
return getSome();
}
catch (SomeCheckedException e) {
return e.getMessage();
}
}
String getSome() throws SomeCheckedException; // How is this overridden/implemented?
}
private static class SomeCheckedException extends Exception {
public SomeCheckedException(String message) {
super(message);
}
}
}```
Your mistake is that assuming that if a Lambda of a Supplier implements get then a lambda of a SomeSupplier must also implement get.
But instead a Lambda will always implement the single abstract method of an interface* it's about to implement. In Supplier that's get. Your SomeSupplier however has implemented get (with a default method). Therefore getSome() becomes the single abstract method of the functional interface SomeSupplier. So this line:
SomeSupplier s = () -> getSomeOrException();
is roughly analogous to this:
SomeSupplier s = new SomeSupplier() {
String getSome() throws SomeCheckedException() {
return getSomeOrException();
}
};
Note that this implements getSome and not the underlying get method.
*: This is also why functional interfaces can only ever have one abstract method: there's no fallback logic to pick one option if more than one such method exists for a given target type.

java 8 lambda tech question for jsonpath library

I'm looking info about lambda although I'm not able to find something similar to below function. It belongs to class org.springframework.test.web.servlet.result.JsonPathResultMatchers , and ResultMatcher is a #FunctionalInterface, and result is type MvcResult and jsonPathHelper.doesNotExist return void
public ResultMatcher doesNotExist() {
return result -> jsonPathHelper.doesNotExist(getContent(result));
}
I call above one through
jsonPath("$._embedded" ).doesNotExist()
I have literally no idea about:
if jsonPathHelper.doesNotExist return void then why doesNotExist return ResultMatcher.
Class has anything similar to result, where is this argument come from?
Thanks
The lambda in your code:
result -> jsonPathHelper.doesNotExist(getContent(result));
is just a representation of a ResultMatcher since its a FunctionalInterface. You can look at it as if :
public ResultMatcher doesNotExist() {
return new ResultMatcher() {
#Override
public void match(MvcResult result) throws Exception {
jsonPathHelper.doesNotExist(getContent(result)); // returns void
}
};
}
if jsonPathHelper.doesNotExist return void then why doesNotExist
return ResultMatcher
Your method doesNotExist, just returns the functional interface in itself, which can thereafter be used to invoke its match function. Note that invocation would also be returning void.
Class has anything similar to result, where is this argument come
from?
If you look at the anonymous class above, with lambda representation the result becomes the argument of the match method within the ResultMatcher implementation.
So when you actually wish to access this implementation(or ResultMatcher in general), you would invoke the method as follows(simplified initializations):
ResultMatcher resultMatcher = doesNotExist(); // your method returns here
MvcResult result = new MvcResult(); // some MvcResult object
resultMatcher.match(result); // actual invocation

How to call functor objects's function without invoking call method

I have a functor object:
private static Func1<MyEvent, Observable<Data>> getDataOnEvent = new Func1<MyEvent, Observable<Data>>() {
#Override
public Observable<Data> call(MyEvent event) {
return ApiFactory.get().getData()
}
};
For its invoking I need to do this:
result = getDataOnEvent.call(someEvent)
Is it possible to do this instead:
result = getDataOnEvent(someEvent)
Like it is done with Python and Javascript? Maybe a new version of java OR some library like Lombok?
Just use,
private static Observable<Data> getDataOnEvent(MyEvent event) {
return ApiFactory.get().getData()
}
and you can call result = getDataOnEvent(someEvent); whenever you need it. As you can see, writing it this way, will save even more boiler code than the five letters .call on the invocation side.
If Func1 is a functional interface, you can use ContainingClass::getDataOnEvent wherever a Func1<MyEvent, Observable<Data>> is expected. You can also store it into a static variable, if you prefer using the simple identifier getDataOnEvent as function:
private static Func1<MyEvent, Observable<Data>> getDataOnEvent
= ContainingClass::getDataOnEvent;
Then you can use getDataOnEvent(event) to call it or getDataOnEvent to refer to it as a Func1 instance whenever you need it.
If Func1 is not a functional interface, then you can’t create the function in this compact form, but on the other hand, in that case it wouldn’t be reasonable to ask for a support for calling an arbitrary method without naming it explicitly, either.
I know what you mean, groovy code call a closure like this:
def code = { 123 };
//can be called like any other method if the variable is a closure.
assert code() == 123;
//can be call explicitly by using `call` method
assert code.call() == 123;
javascript code call a function like this:
let code = () => 123;
//can be called like any other function if the variable is a function.
assert code() == 123;
//can be call explicitly by using `call` method
assert code.call() == 123;
But I can tell you java gammar not support this feature for fields/variables, maybe in the next jdk will be enable this feature which a field/variable refer to Callable.
Fortunately, Single-Static-Import Declarations supports calling a method directly if the static member is a method, for example:
import static java.lang.Math.abs;
assert abs(-1) == 1;
If you really want to make an identifier called like a method call, you can fake something like this:
class ApiFactory {
public static Func1<MyEvent, Observable<Data>> getDataOnEvent = new Func1<MyEvent, Observable<Data>>() {
public Observable<Data> call(MyEvent event) {
return ApiFactory.get().getData();
}
};
public static Observable<Data> getDataOnEvent(MyEvent event) {
return getDataOnEvent.call(event);
}
}
then you can call like this:
import static ${package}.ApiFactory.getDataOnEvent;
// which is calling a static method
result = getDataOnEvent(event);
// which is calling a static field
result = getDataOnEvent.call(event);

Lambda Expressions in Java8

import java.util.concurrent.Callable;
public class AdvancedLambda {
static void invoke(Runnable r){
r.run();
}
static Object invoke(Callable c) throws Exception {
return c.call();
}
public static void main(String[] args) throws Exception {
String s = (String) invoke(() -> true);
System.out.println(s);
}
}
Can anyone help me understand this? I was under the impression that we can only use lamba expressions in Java 8 only when we implement an interface and override its methods (replacing Anonymous classes by Lambda expressions).
Also in which situation will the method invoke(Runnable r) be called?
In the following line
String s = (String) invoke(() -> true);
It is actually invoke(Callable) that is getting called. The reason is:
() -> true is a lambda expression that has zero formal parameter and return a result.
Such a signature (zero parameter, single result) is compatible with the functional method call() of the Callable interface. Note that the interface does not need to have the #FunctionalInterface annotation, it just needs to have a single abstract method.
If you want to invoke invoke(Runnable) instead, you will need to create a lambda that is compatible with a functional method that takes zero parameter and returns no result (i.e. conforms with the signature of run()). Something like this:
invoke(() -> System.out.println("foo"));
Which just prints foo when ran.
A lambda expression supplies an implementation for a functional interface. This is what your code snippet does.
Your call to invoke passes a lambda expression with no arguments that returns a value (a boolean in your case). Therefore it matches Object invoke(Callable c), and not void invoke(Runnable r) (since a Callable's call method has a return value while a Runnable's run method doesn't return anything).
invoke(() -> {System.out.println("something");});
will call void invoke(Runnable r), since in this case the lambda expression has no return type.
only when we Implement a interface and override its methods
That's, more or less, what you do here. Not methods, but just one method: call(). This () -> true part is your implementation of Callable#call().
In other words, this line:
String s = (String) invoke(() -> true);
would be totally equivalent with this one:
String s = (String) invoke(new Callable() {
#Override
public Object call() throws Exception {
return true;
}
});
LambdaParameters -> LambdaBody
The arrow operator (->) for defining lambda functions
Lambda :can only be used to execute background tasks (here compiler then figures out)
Expressions : are return a value of some kind
Lambda expression is another way of writing an instance of anonymous class, to make an instance of anonymous class easier to write. In JVM, it will not occupy much memory as comparing with normal java object creation with new(executing static variables, static blocks, loading classes from whole hierarchy ).
Lambda expression syntax:
(params) -> expression to implement a #FunctionalInterface
In your test case: String s = (String) invoke(() -> true); the expression has return type true with no argument. So the Runnable FunctionalInterface does not match with lambda expression because it has void run() attribute. It matches with Callable FuncationalInterface using
V call().
How lambda expressions work under the hood?
It might look like the lambda expressions are just the syntax sugar for anonymous inner classes, but there is much more elegant approach. The simplest explanation is: the lambda expression is represented by a new method, and it is invoked at run-time using invokedynamic.
Source Code:
class LambdaExample {
public void abc() {
Runnable r = () -> {
System.out.println("hello");
}
r.run();
}
}
Bytecode equivalent:
class LambdaExample {
public void abc() {
Runnable r = <lambda$1 as Runnable instance>;
r.run();
}
static void lambda$1() {
System.out.println("hello");
}
}
Inside the JVM, there is a lambda factory that creates an instance of the functional interface (e.g. Runnable) from the generated lambda method (e.g. lambda$1).
Lambda expressions are great, and there's even more great stuff in Java 8...
Take a look at below example.
import javafx.beans.DefaultProperty;
import jdk.nashorn.internal.codegen.CompilerConstants;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* Created by KUMAJYE on 13/09/2016.
*/
public class ThreadMonitor {
private static int noOfThreads = 0;
public static void main(String[] args) {
ThreadModel threadModel;
noOfThreads = Integer.parseInt(args[0]);
if (noOfThreads > 0) {
threadModel = getThreadingModelForCallsInCallingThread();
} else {
threadModel = getAsynchThreadingModel();
}
}
public static ThreadModel getThreadingModelForCallsInCallingThread() {
ExecutorService executor = Executors.newFixedThreadPool(noOfThreads);
return (r) -> executor.submit(r);
}
public static ThreadModel getAsynchThreadingModel() {
// do execution on same Thread or separate Thread.
// or
// r.run();
return (r) -> new Thread(r).start();
}
interface ThreadModel {
void invoke(Runnable r);
}
}

How to chain BiFunctions?

I would like to chain BiFunctions, like in the method chainWanted in the code sample below.
BiFunction takes Function as a parameter of AndThen. is it possible to somehow chain BiFunctions ?
The code here doesn't compile because of this and I cannot cast BiFunction to Function.
import java.util.function.BiFunction;
import java.util.function.Function;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Wf {
BiFunction<RemoteWebDriver, WfParams, RemoteWebDriver> init = this::init;
BiFunction<RemoteWebDriver, WfParams, RemoteWebDriver> wait = this::wait;
BiFunction<RemoteWebDriver, WfParams, RemoteWebDriver> chainNow = init
.andThen(d -> {
System.out.println("--------------");
return null;
});
BiFunction<RemoteWebDriver, WfParams, RemoteWebDriver> chainWanted = init
.andThen((BiFunction) wait);
public RemoteWebDriver init(RemoteWebDriver d, WfParams params) {
System.out.println("init(d, params)");
return d;
}
public RemoteWebDriver wait(RemoteWebDriver d, WfParams params) {
System.out.println("Wf.wait(d, params)");
return d;
}
public static void main(String[] args) throws Exception {
new Wf().start();
}
private void start() {
chainNow.apply(null, null);
}
}
Chaining of one Function to another works naturally because the return value of the first function is passed as the argument to the next function, and that function's return value is passed as the argument to the subsequent function, and so forth. This doesn't work naturally with BiFunction because they take two arguments. The first argument would be the return value from the previous function, but what would the second argument be? It also explains why BiFunction allows chaining with andThen to a Function instead of to another BiFunction.
This suggests, however, that it would be possible to chain one BiFunction to another if there were some way of providing the value for second argument. This can be done by creating a helper function that stores the value for the second argument in a local variable. Then, a BiFunction can be converted into a Function by capturing that local variable from the environment and using it as the second argument.
Here's what that would look like.
BiFunction<RemoteWebDriver, WfParams, RemoteWebDriver> chainWanted = this::chainHelper;
RemoteWebDriver chainHelper(RemoteWebDriver driver, WfParams params) {
return
init.andThen(rwd -> wait.apply(rwd, params))
.apply(driver, params);
}
// ...
chainWanted.apply(driver, params);
The chainHelper method holds the params argument for later capture. We call init.andThen() in order to do the chaining. But this requires a Function whereas wait is a BiFunction. Instead of using a method reference this::wait we use the lambda expression
rwd -> wait.apply(rwd, params)
which captures params from the lexical environment. This gives a lambda expression that takes a single argument and returns a single value, so it's now a Function that wraps the wait which is a BiFunction. This is an example of partial application or currying. Finally, we call the resulting BiFunction using apply(), passing the original arguments.
Where should the WfParams come from for the invocation of wait? If you mean to reuse the same WfParams for all the functions calls, just put WfParams as a class member variable instead of passing it to each function.
class Wf {
private final WfParams params;
public Wf(WfParams params) {
this.params = params;
}
UnaryOperator<RemoteWebDriver> init = this::init;
UnaryOperator<RemoteWebDriver> wait = this::wait;
Function<RemoteWebDriver,RemoteWebDriver> chain = init.andThen(wait);
RemoteWebDriver init(RemoteWebDriver d) {
// can use WfParams here
return d;
}
RemoteWebDriver wait(RemoteWebDriver d) {
// can use WfParams here
return d;
}
private void start() {
chain.apply(null);
}
public static void main(String[] args) {
new Wf(new WfParams()).start();
}
}
Is there a particular reason you want to use function chaining like that? Why not simply call init(...); wait(...); from start()?
I did something like this - created my custom BiFunction.
The idea being:
Return type is same as the second argument
First argument is passed internally to chained biFunction
public interface BiFunctionCustom<T, U> extends BiFunction<T,U,U> {
default BiFunctionCustom<T, U> andThen(BiFunctionCustom<T, U> after) {
Objects.requireNonNull(after);
return (T t, U u) -> after.apply(t, apply(t, u));
}
}

Categories

Resources