This file contains hidden or 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
maybeCar.map(Car::getAudioSystem) | |
.filter(as -> "Pionner".equals(as.getBrand()) | |
.ifPresent(() -> System.out.println("ok")); |
This file contains hidden or 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 { | |
private String engine; | |
private String color; | |
private long frameNumber; | |
public Car(long frameNumber, String engine, String color) { | |
this.engine = engine; | |
this.color = color; | |
this.frameNumber = frameNumber; |
This file contains hidden or 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 LightCar { | |
private String engine; | |
private String color; | |
public LightCar(String engine, String color) { | |
this.engine = engine; | |
this.color = color; | |
} |
This file contains hidden or 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 LightCarContainer { | |
private static HashMap<String,LightCar> lightCars = new HashMap<String, LightCar>(); | |
public static LightCar getLightCar(String engine, String color) { | |
String key = engine + color; | |
if (!lightCars.containsKey(key)) { | |
lightCars.put(key, new LightCar(engine,color)); | |
} | |
return lightCars.get(key); |
This file contains hidden or 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 CarFlyWeight implements Car { | |
private long frameNumber; | |
private LightCar lightCar; | |
public CarFlyWeight(long frameNumber, LightCar lightCar) { | |
this.frameNumber = frameNumber; | |
this.lightCar = lightCar; | |
} |
This file contains hidden or 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 CarFactory { | |
Car create(long frameNumber, String engine, String color) { | |
return new CarFlyWeight(frameNumber, LightCarContainer.getLightCar(engine, color)); | |
} | |
} |
This file contains hidden or 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
// business logic | |
public static List<Car> filterCarsByBrand(List<Car> cars, String brand) { | |
ArrayList<Car> filteredCars = new ArrayList<Car>(); | |
for (Car car : cars) { | |
if (brand.equals(car.getBrand())) { | |
filteredCars.add(car); | |
} | |
} | |
return filteredCars; | |
} |
This file contains hidden or 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
// business logic | |
public static List<Car> filterCarsByBrandAndColor(List<Car> cars, String brand, String color) { | |
// we can reuse the method to filtering by brand | |
List<Car> filteredCarsByBrand = filterCarsByBrand(cars, brand); | |
ArrayList<Car> filteredCars = new ArrayList<Car>(); | |
for (Car car : cars) { | |
if (color.equals(car.getColor())) { | |
filteredCars.add(car); | |
} | |
} |
This file contains hidden or 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
// new interface | |
public interface CarChecker { | |
boolean check(Car car); | |
} | |
... | |
//business logic | |
public static List<Car> filterCars(List<Car> cars, CarChecker carChecker) { | |
ArrayList<Car> filteredCars = new ArrayList<Car>(); |
This file contains hidden or 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
List<Car> cars = filterCars(input, new CarChecker() { | |
public boolean check(Car car) { | |
return "Ferrari".equals(car.getBrand()) && "red".equals(car.getColor()); | |
} | |
}); |