Skip to content

Instantly share code, notes, and snippets.

@aruld
Created January 11, 2018 03:06
Show Gist options
  • Save aruld/b56fa5f14e69bdeeeae528b2e99c61cd to your computer and use it in GitHub Desktop.
Save aruld/b56fa5f14e69bdeeeae528b2e99c61cd to your computer and use it in GitHub Desktop.
Local Variable Type involving lambda
import java.util.Comparator;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
public class RideTest {
interface RideProvider {
long getFareEstimate(String start_lat, String start_lng, String end_lat, String end_lng, String type);
}
public static void main(String[] args) {
var start_lat = "37.7749295";//San Francisco
var start_lng = "-122.41941550000001";//San Francisco
var end_lat = "37.3382082";//San Jose
var end_lng = "-121.88632860000001";//San Jose
var lyft = new RideProvider() {
@Override
public long getFareEstimate(String start_lat, String start_lng, String end_lat, String end_lng, 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;
}
};
var lyftFuture = CompletableFuture.supplyAsync(() -> lyft.getFareEstimate(start_lat, start_lng, end_lat, end_lng, "Lyft"));
var uber = new RideProvider() {
@Override
public long getFareEstimate(String start_lat, String start_lng, String end_lat, String end_lng, 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;
}
};
var uberFuture = CompletableFuture.supplyAsync(() -> uber.getFareEstimate(start_lat, start_lng, end_lat, end_lng, "uberX"));
// compare the futures for best fare estimate
Stream.of(lyftFuture, uberFuture)
.map(CompletableFuture::join)
.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