3
votes

I need to know following code can be convert to lamda expression.

private class K<D extends BaseDose<I>, I extends DoseInstance>
    implements Predicate<Prescription<D, I>>
  {
    @Override
    public boolean test(final Prescription<D, I> input)
    {
      return DepositType.DATE == input.getBaseDeposit().getDepositType();
    }
  }
1
Predicate<Prescription<D, I>> is a functional interface, so yes. The whole thing can probably be written as input -> DepositType.DATE == input.getBaseDeposit().getDepositType().ernest_k
@ernest_k Shouldn't this be an answer?glglgl

1 Answers

4
votes

Instead of creating a class (your K class) that implements Predicate<Prescription<D, I>>, you can assign a lambda expression to a Predicate variable.

For example:

Predicate<Prescription<BaseDose<DoseInstance>, DoseInstance>> pred =
    p -> DepositType.DATE == p.getBaseDeposit().getDepositType();

If you need a lambda expression with multiple statements, you can write:

Predicate<Prescription<BaseDose<DoseInstance>, DoseInstance>> pred =
    p -> {
        final SigningStatus signingStatus = p.getSigningStatus();
        return !p.isReplacedByLatest() && SigningStatus.INVALIDATED.equals(signingStatus);
    };

though this can be simplified to a single statement:

Predicate<Prescription<BaseDose<DoseInstance>, DoseInstance>> pred =
    p -> !p.isReplacedByLatest() && SigningStatus.INVALIDATED.equals(p.getSigningStatus());