-2
public static <E extends EntityBase, FE>
FE findForeignEntity(E entities, Function<E, Long> fnForeignId, Function<Long, FE> fnFindIdIn) {
    if (entities == null) return null;
    Long fid = fnForeignId.apply(entities);
    return fnFindIdIn.apply(fid);
}

City city = Utils.findForeignEntity(candidate,
            c -> c.getAddress() != null ? c.getAddress().getCity() : null,
            fid -> cityRepo.findOne(fid));

what is "fid" stand for ? I am not familiar with this kind of usage in java. Can anybody give me a clue.

Green Baylee
  • 117
  • 3
  • 8

3 Answers3

1

In the method findForeignEntity you are passing 3 arguments, first one is entities which is the argument to method fnForeignId. The result of the method fnForeignId is fid and fid is the input to your method fnFindIdIn which returns you FE(Foreign Entity).

City city = Utils.findForeignEntity(candidate,
            c -> c.getAddress() != null ? c.getAddress().getCity() : null,
            fid -> cityRepo.findOne(fid));

In this code what confuses you might be the lambdas. I will try to make it simple. The 2 arguments of your method findForeignEntity are Function, which is a functional interface, which requires a definition.

The definition of the method fnForeignId is

c -> c.getAddress() != null ? c.getAddress().getCity() : null

which is something like

return c.getAddress() != null ? c.getAddress().getCity() : null

And definition of the method fnFindIdIn is

fid -> cityRepo.findOne(fid)

which can be read as something like

return cityRepo.findOne(fid)

These are all lambdas which was introduced in Java8

As @GhostCat has told fid stands for foreign id.

Community
  • 1
  • 1
Johny
  • 2,128
  • 3
  • 20
  • 33
0

Obviously, fid abbreviates "foreign id".

If you are asking about the -> syntax: that is a lambda expression. Their main goal is:

Lambda expressions let you express instances of single-method classes more compactly.

But obviously there is a lot "behind" that concept.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Are you looking for a lambda expression? Look at java's documetation on it. Lambdas are a great way of using a function as a parameter, but it will only ever be a one line function. A quick little way of getting around things.

Spencer Pollock
  • 444
  • 4
  • 17