Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions proof/collections/CollectorsTeeing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.*;

/// Proof: collectors-teeing
/// Source: content/collections/collectors-teeing.yaml
record Item(String name, double price) {}
record Stats(long count, double total) {}

void main() {
var items = List.of(new Item("a", 10.0), new Item("b", 20.0));
var result = items.stream().collect(
Collectors.teeing(
Collectors.counting(),
Collectors.summingDouble(Item::price),
Stats::new
)
);
}
11 changes: 11 additions & 0 deletions proof/collections/CopyingCollectionsImmutably.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.*;

/// Proof: copying-collections-immutably
/// Source: content/collections/copying-collections-immutably.yaml
void main() {
List<String> original = new ArrayList<>(List.of("a", "b", "c"));
List<String> copy =
List.copyOf(original);
}
10 changes: 10 additions & 0 deletions proof/collections/ImmutableListCreation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.*;

/// Proof: immutable-list-creation
/// Source: content/collections/immutable-list-creation.yaml
void main() {
List<String> list =
List.of("a", "b", "c");
}
10 changes: 10 additions & 0 deletions proof/collections/ImmutableMapCreation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.*;

/// Proof: immutable-map-creation
/// Source: content/collections/immutable-map-creation.yaml
void main() {
Map<String, Integer> map =
Map.of("a", 1, "b", 2, "c", 3);
}
10 changes: 10 additions & 0 deletions proof/collections/ImmutableSetCreation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.*;

/// Proof: immutable-set-creation
/// Source: content/collections/immutable-set-creation.yaml
void main() {
Set<String> set =
Set.of("a", "b", "c");
}
9 changes: 9 additions & 0 deletions proof/collections/MapEntryFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.*;

/// Proof: map-entry-factory
/// Source: content/collections/map-entry-factory.yaml
void main() {
var e = Map.entry("key", 42);
}
12 changes: 12 additions & 0 deletions proof/collections/ReverseListIteration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.*;

/// Proof: reverse-list-iteration
/// Source: content/collections/reverse-list-iteration.yaml
void main() {
List<String> list = List.of("a", "b", "c");
for (String element : list.reversed()) {
System.out.println(element);
}
}
12 changes: 12 additions & 0 deletions proof/collections/SequencedCollections.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.*;

/// Proof: sequenced-collections
/// Source: content/collections/sequenced-collections.yaml
void main() {
List<String> list = new ArrayList<>(List.of("a", "b", "c"));
var last = list.getLast();
var first = list.getFirst();
var reversed = list.reversed();
}
16 changes: 16 additions & 0 deletions proof/collections/StreamToarrayTyped.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.*;
import java.util.stream.*;

/// Proof: stream-toarray-typed
/// Source: content/collections/stream-toarray-typed.yaml
List<String> getNames() {
return List.of("Alice", "Bob", "Charlie", "Dave");
}

void main() {
String[] arr = getNames().stream()
.filter(n -> n.length() > 3)
.toArray(String[]::new);
}
11 changes: 11 additions & 0 deletions proof/collections/UnmodifiableCollectors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.*;
import java.util.stream.*;

/// Proof: unmodifiable-collectors
/// Source: content/collections/unmodifiable-collectors.yaml
void main() {
Stream<String> stream = Stream.of("a", "b", "c");
List<String> list = stream.toList();
}
16 changes: 16 additions & 0 deletions proof/concurrency/CompletablefutureChaining.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.concurrent.*;

/// Proof: completablefuture-chaining
/// Source: content/concurrency/completablefuture-chaining.yaml
String fetchData() { return "data"; }
String transform(String data) { return data.toUpperCase(); }

void main() {
CompletableFuture.supplyAsync(
this::fetchData
)
.thenApply(this::transform)
.thenAccept(System.out::println);
}
34 changes: 34 additions & 0 deletions proof/concurrency/ConcurrentHttpVirtual.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.net.*;
import java.net.http.*;
import java.util.*;
import java.util.concurrent.*;

/// Proof: concurrent-http-virtual
/// Source: content/concurrency/concurrent-http-virtual.yaml
HttpRequest req(String url) throws Exception {
return HttpRequest.newBuilder(new URI(url)).build();
}

void main() throws Exception {
var client = HttpClient.newHttpClient();
var urls = List.<String>of(); // empty — proof is compile-only
try (var exec = Executors
.newVirtualThreadPerTaskExecutor()) {
var results = urls.stream()
.map(u -> exec.submit(() -> {
try {
return client.send(req(u),
HttpResponse.BodyHandlers.ofString()).body();
} catch (Exception e) {
throw new RuntimeException(e);
}
}))
.toList().stream()
.map(f -> {
try { return f.get(); }
catch (Exception e) { throw new RuntimeException(e); }
}).toList();
}
}
14 changes: 14 additions & 0 deletions proof/concurrency/ExecutorTryWithResources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.concurrent.*;

/// Proof: executor-try-with-resources
/// Source: content/concurrency/executor-try-with-resources.yaml
void main() throws Exception {
Runnable task = () -> System.out.println("task");
try (var exec =
Executors.newCachedThreadPool()) {
exec.submit(task);
}
// auto shutdown + await on close
}
28 changes: 28 additions & 0 deletions proof/concurrency/LockFreeLazyInit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
//JAVAC_OPTIONS --enable-preview --release 25
//JAVA_OPTIONS --enable-preview
import java.util.function.*;

/// Proof: lock-free-lazy-init
/// Source: content/concurrency/lock-free-lazy-init.yaml
///
/// Note: The snippet calls INST.get() on a StableValue<Config>. In JDK 25,
/// StableValue.supplier() returns a Supplier<T> whose get() provides the same
/// lock-free lazy initialization semantics.
class Config {
private static final Supplier<Config> INST =
StableValue.supplier(Config::load);

static Config get() {
return INST.get();
}

static Config load() {
return null; // placeholder — real load reads from disk/DB
}
}

void main() {
Config.get();
}
13 changes: 13 additions & 0 deletions proof/concurrency/ProcessApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
/// Proof: process-api
/// Source: content/concurrency/process-api.yaml
void main() {
ProcessHandle ph =
ProcessHandle.current();
long pid = ph.pid();
ph.info().command()
.ifPresent(System.out::println);
ph.children().forEach(
c -> System.out.println(c.pid()));
}
31 changes: 31 additions & 0 deletions proof/concurrency/ScopedValues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.concurrent.*;

/// Proof: scoped-values
/// Source: content/concurrency/scoped-values.yaml
record User(String name) {}
record Request(String path) {}

class Handler {
static final ScopedValue<User> CURRENT =
ScopedValue.newInstance();

User authenticate(Request req) {
return new User("Alice");
}

void handle(Request req) {
ScopedValue.where(CURRENT,
authenticate(req)
).run(this::process);
}

void process() {
// use CURRENT.get() in scope
}
}

void main() {
new Handler().handle(new Request("/"));
}
29 changes: 29 additions & 0 deletions proof/concurrency/StableValues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
//JAVAC_OPTIONS --enable-preview --release 25
//JAVA_OPTIONS --enable-preview
import java.util.function.*;
import java.util.logging.*;

/// Proof: stable-values
/// Source: content/concurrency/stable-values.yaml
///
/// Note: The snippet calls logger.get() on a StableValue<Logger>. In JDK 25,
/// StableValue.supplier() returns a Supplier<T> whose get() provides the same
/// lazy, thread-safe initialization semantics.
class LoggerHolder {
private final Supplier<Logger> logger =
StableValue.supplier(this::createLogger);

Logger getLogger() {
return logger.get();
}

Logger createLogger() {
return Logger.getLogger(getClass().getName());
}
}

void main() {
new LoggerHolder().getLogger();
}
24 changes: 24 additions & 0 deletions proof/concurrency/StructuredConcurrency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
//JAVAC_OPTIONS --enable-preview --release 25
//JAVA_OPTIONS --enable-preview
import java.util.concurrent.*;

/// Proof: structured-concurrency
/// Source: content/concurrency/structured-concurrency.yaml
record User(String name) {}
record Order(String id) {}
record Result(User user, Order order) {}

User fetchUser() { return new User("Alice"); }
Order fetchOrder() { return new Order("o1"); }
Result combine(User u, Order o) { return new Result(u, o); }

void main() throws Exception {
try (var scope = StructuredTaskScope.open()) {
var u = scope.fork(this::fetchUser);
var o = scope.fork(this::fetchOrder);
scope.join();
combine(u.get(), o.get());
}
}
14 changes: 14 additions & 0 deletions proof/concurrency/ThreadSleepDuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.time.*;

/// Proof: thread-sleep-duration
/// Source: content/concurrency/thread-sleep-duration.yaml
void main() throws InterruptedException {
Thread.sleep(
Duration.ofMillis(1)
);
Thread.sleep(
Duration.ofMillis(1)
);
}
9 changes: 9 additions & 0 deletions proof/concurrency/VirtualThreads.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
/// Proof: virtual-threads
/// Source: content/concurrency/virtual-threads.yaml
void main() throws InterruptedException {
Thread.startVirtualThread(() -> {
System.out.println("hello");
}).join();
}
15 changes: 15 additions & 0 deletions proof/datetime/DateFormatting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.time.*;
import java.time.format.*;

/// Proof: date-formatting
/// Source: content/datetime/date-formatting.yaml
void main() {
DateTimeFormatter fmt =
DateTimeFormatter.ofPattern(
"uuuu-MM-dd");
String formatted =
LocalDate.now().format(fmt);
// Thread-safe, immutable
}
20 changes: 20 additions & 0 deletions proof/datetime/DurationAndPeriod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.time.*;
import java.time.temporal.*;

/// Proof: duration-and-period
/// Source: content/datetime/duration-and-period.yaml
void main() {
LocalDate date1 = LocalDate.of(2024, 1, 1);
LocalDate date2 = LocalDate.of(2025, 1, 1);
LocalTime time1 = LocalTime.of(8, 0);
LocalTime time2 = LocalTime.of(17, 0);

long days = ChronoUnit.DAYS
.between(date1, date2);
Period period = Period.between(
date1, date2);
Duration elapsed = Duration.between(
time1, time2);
}
Loading