Skip to content

Instantly share code, notes, and snippets.

View RonVeen's full-sized avatar

Ronveen RonVeen

  • @mention Quintor
  • Netherlands
View GitHub Profile
@RonVeen
RonVeen / EmployeeAndContractor.java
Created September 19, 2022 05:56
Employee and Contractor record defintion
record Employee(Long id, String name, String department) { };
record Contractor(Long id, String name, BigDecimal fee) { };
@RonVeen
RonVeen / Virtual-Thread-Example.java
Created September 16, 2022 15:07
Example of creating virtual and platform threads
package nl.rockstars.loom;
public class VirtualThread {
Runnable runnable = () -> {
System.out.println(", I am a virtual thread: " + Thread.currentThread().isVirtual());
};
public static void main(String... args) {
new VirtualThread().run();
@RonVeen
RonVeen / structured-concurrency-customer.java
Last active April 16, 2023 12:53
Structured concurrency example of retrieving customer info
CustomerInfo retrieveCustomerInfo(Long id) throws ExecutionException, InterruptedException {
try (var scope = new StructuredTaskScope.ShutdownOnFailure()()) {
Future<Customer> customerFuture = scope.fork(() -> retrieveCustomer(id));
Future<Orders> ordersFuture = scope.fork(() -> retrieveCustomerOrders(id));
Future<CustomerSalesHistory> historyFuture = scope.fork(() -> retrieveCustomerHistory(id));
scope.join();
scope.throwIfFailed(e -> new WebApplicationException(e));
@RonVeen
RonVeen / common-threading.java
Last active October 3, 2022 08:09
standard common threading
CustomerInfo retrieveCustomerInfo(Long id) throws ExecutionException, InterruptedException {
try (var pool = new ScheduledThreadPoolExecutor()) {
Future<Customer> customerFuture = pool.submit(() -> retrieveCustomer(id));
Future<CustomerOrders> ordersFuture = pool.submit(() -> retrieveCurrentCustomerOrders(id));
Future<CustomerSalesHistory> historyFuture = pool.submit(() -> retrieveCustomerHistory(id));
Customer customer = customerFuture.get();
Collection<Orders> outstandingOrders = ordersFuture.get();
Collection<CustomerSalesHistory> salesHistory = historyFuture.get();
@RonVeen
RonVeen / guarded-pattern-implicit-NPE.java
Created September 15, 2022 20:50
Guarded pattern with an implicit NPE
private String printValue(Object obj) {
return switch (obj) {
case String s when s.length() > 10 -> String.format("Long String with value %s", s);
case String s -> String.format("Not so long String with value %s", s);
default -> String.format("Dunno the type, but the value is %s", obj.toString());
};
}
@RonVeen
RonVeen / guarded-pattern-NPE.java
Created September 15, 2022 20:47
Guarded pattern throwing an NPE
private String printValue(Object obj) {
return switch (obj) {
case null -> throw new NullPointerException();
case String s when s.length() > 10 -> String.format("Long String with value %s", s);
case String s -> String.format("Not so long String with value %s", s);
default -> String.format("Dunno the type, but the value is %s", obj.toString());
};
}
private String printValue(Object obj) {
return switch (obj) {
case null -> new String("You can't pass in a null value!");
case String s when s.length() > 10 -> String.format("Long String with value %s", s);
case String s -> String.format("Not so long String with value %s", s);
default -> String.format("Dunno the type, but the value is %s", obj.toString());
};
}
@RonVeen
RonVeen / PatternMatching.java
Last active September 2, 2022 14:21
Record pattern matching example
private void process(Object o) {
if (o instanceof Employee(String id, String name, String department)) {
System.out.println(name + " => " + department);
}
if (o instanceof Contractor(String id, String name, BigDecimal fee)) {
System.out.println(name + " => " + fee.toPlainString());
}
}
import java.math.BigDecimal;
public class RecordPatternMatchingClassic {
public static void main(String...args) {
new RecordPatternMatchingClassic().execute();
}
private void execute() {
var john = new Employee("12", "John", "Auditing");
@RonVeen
RonVeen / suspended.kt
Created January 6, 2022 17:46
Using suspended functions
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.guide.exampleCompose02
import kotlinx.coroutines.*
import kotlin.system.*
fun main() = runBlocking<Unit> {
val time = measureTimeMillis {