Last active
August 20, 2018 06:11
-
-
Save aruld/23dccab72c90974a3c4b22afedcafeae to your computer and use it in GitHub Desktop.
Example shows Local variables support for Lambda Parameters in Java 11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jetbrains.annotations.NotNull; | |
import java.util.Comparator; | |
import java.util.function.ToLongFunction; | |
import java.util.stream.Stream; | |
public class RideProviderTest { | |
@FunctionalInterface | |
private interface RideProvider { | |
long getFareEstimate(String type); | |
} | |
public static void main(String[] args) { | |
// implicitly typed lambda expression | |
ToLongFunction<RideProvider> lyftProvider = (@NotNull var provider) -> provider.getFareEstimate("Lyft"); | |
var lyft = new RideProvider() { | |
@Override | |
public long getFareEstimate(String type) { | |
var estimated_cost_cents_max = 0; | |
switch (type) { | |
case "Line": | |
estimated_cost_cents_max = 3307; | |
break; | |
case "Lyft": | |
estimated_cost_cents_max = 7306; | |
break; | |
case "Plus": | |
estimated_cost_cents_max = 12451; | |
break; | |
case "Premier": | |
estimated_cost_cents_max = 17562; | |
break; | |
case "Lux": | |
estimated_cost_cents_max = 23624; | |
break; | |
case "Lux SUV": | |
estimated_cost_cents_max = 25879; | |
break; | |
} | |
return estimated_cost_cents_max; | |
} | |
}; | |
long lyftFare = lyftProvider.applyAsLong(lyft); | |
// implicitly typed lambda expression | |
ToLongFunction<RideProvider> uberProvider = (@NotNull var provider) -> provider.getFareEstimate("uberX"); | |
var uber = new RideProvider() { | |
@Override | |
public long getFareEstimate(String type) { | |
var estimated_cost_cents_max = 0; | |
switch (type) { | |
case "POOL": | |
estimated_cost_cents_max = 7000; | |
break; | |
case "EXPRESS POOL": | |
estimated_cost_cents_max = 7000; | |
break; | |
case "uberX": | |
estimated_cost_cents_max = 9200; | |
break; | |
case "WAV": | |
estimated_cost_cents_max = 9200; | |
break; | |
case "ASSIST": | |
estimated_cost_cents_max = 9200; | |
break; | |
case "uberXL": | |
estimated_cost_cents_max = 14800; | |
break; | |
case "SELECT": | |
estimated_cost_cents_max = 20900; | |
break; | |
case "SUV": | |
estimated_cost_cents_max = 30600; | |
break; | |
} | |
return estimated_cost_cents_max; | |
} | |
}; | |
long uberFare = uberProvider.applyAsLong(uber); | |
// compare the futures for best fare estimate | |
Stream.of(lyftFare, uberFare) | |
. | |
min(Comparator.comparing(i -> i)) | |
. | |
ifPresent(estimated_cost_cents_max -> System.out.println("$" + estimated_cost_cents_max / 100)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment