Skip to content

Instantly share code, notes, and snippets.

View theboreddev's full-sized avatar
💭
💻

The Bored Dev theboreddev

💭
💻
View GitHub Profile
final Car car = new Car();
final CarWashStep chain = new InitialWashStep();
chain.andThen(new SoapStep())
.andThen(new RinseStep())
.andThen(new PolishStep())
.andThen(new DryStep());
final Car finalCar = chain.applyTo(car);
final Item item = new Item(1L, new BigDecimal("12.99"));
System.out.println("Delivery price is " + Plan.BASIC.deliveryPrice.apply(item));
System.out.println("Delivery price is " + Plan.PREMIUM.deliveryPrice.apply(item));
System.out.println("Delivery price is " + Plan.BUSINESS.deliveryPrice.apply(item));
final PriceCalculatorFactory factory = new PriceCalculatorFactory();
final Item newItem = new Item(1L, new BigDecimal("12.99"));
final DeliveryPriceCalculator priceCalculator = factory.priceCalculatorFor(OldStylePlan.BASIC);
System.out.println("Delivery price is " + priceCalculator.priceFor(newItem));
package com.theboreddev.parallelism;
import com.theboreddev.Processor;
import java.util.List;
public class ParallelProcessor implements Processor {
@Override
public Integer process(List<Integer> input) {
package com.theboreddev.sequential;
import com.theboreddev.Processor;
import java.util.List;
public class SequentialProcessor implements Processor {
@Override
public Integer process(List<Integer> input) {
@theboreddev
theboreddev / Processor.java
Created July 1, 2020 06:21
interface interface
public interface Processor {
Integer process(List<Integer> input) throws InterruptedException;
}
package com.theboreddev.concurrency;
import com.theboreddev.Processor;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAccumulator;
CompletableFuture<String> subTask = new CompletableFuture<>();
CompletableFuture<Result> chain = CompletableFuture.runAsync(() -> System.out.println("Start"))
.thenCombine(subTask, (nil, text) -> {
if (text == null || text.isEmpty())
throw new IllegalArgumentException("Text cannot be null or empty!");
return Result.COMPLETED;
})
.completeOnTimeout(Result.TIMEOUT, 500, TimeUnit.MILLISECONDS)
.exceptionally(exception -> Result.FAILED);
CompletableFuture<Result> chain = CompletableFuture.runAsync(() -> System.out.println("Start"))
.thenCombine(subTask, (nil, text) -> {
if (text == null || text.isEmpty())
throw new IllegalArgumentException("Text cannot be null or empty!");
return Result.COMPLETED;
})
.exceptionally(exception -> Result.FAILED);
CompletableFuture<Void> chain = new CompletableFuture<>();
chain.thenComposeAsync(nil -> createUser())
.thenAcceptAsync(logNewUserId())
.thenComposeAsync(nil ->
registerAddress()
.thenAcceptBothAsync(registerPaymentDetails(), (address, paymentDetailsSuccess) -> {
System.out.println("Registered address was : " + address);
System.out.println("Registered payment details success : " + paymentDetailsSuccess);
})