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
private static CompletableFuture<String> futureCall1(){ | |
return CompletableFuture.supplyAsync(() -> { | |
try { | |
Thread.sleep(1000); | |
} catch (Exception e) { | |
System.out.print("exception occurred"); | |
} | |
return "Hello"; | |
}); | |
} |
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
private static void chainingExample(){ | |
CompletableFuture<String> completableFuture | |
= CompletableFuture.supplyAsync(() -> "Hello ") | |
.thenCompose(s -> CompletableFuture.supplyAsync(() -> s + "My ")) | |
.thenCompose(s -> CompletableFuture.supplyAsync(() -> s + "Friend")); | |
try { | |
System.out.print(completableFuture.get()); | |
} catch (Exception e) { | |
System.out.print("exception occurred"); | |
} |
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
I am in Bike implementation of vehicle |
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
public class Car implements Vehicle { | |
public void drive() { | |
System.out.println("I am in Car implementation of vehicle"); | |
} | |
} |
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
public interface Vehicle { | |
public void drive(); | |
} |
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 com.google.inject.AbstractModule; | |
public class VehicleShoppingModule extends AbstractModule { | |
@Override | |
protected void configure() { | |
bind(Vehicle.class).to(Bike.class); | |
} | |
} |
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 com.google.inject.Guice; | |
import com.google.inject.Injector; | |
public class Main { | |
public static void main(String[] args) { | |
Injector injector = Guice.createInjector(new VehicleShoppingModule()); | |
VehicleShopping vehicleShopping = injector.getInstance(VehicleShopping.class); | |
vehicleShopping.testDrive(); | |
} |
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
public class Bike implements Vehicle { | |
public void drive() { | |
System.out.println("I am in Bike implementation of vehicle"); | |
} | |
} |
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 com.google.inject.Inject; | |
public class VehicleShopping { | |
private Vehicle vehicle; | |
@Inject | |
public VehicleShopping(Vehicle vehicle) { | |
this.vehicle = vehicle; | |
} |