YantrikDB/packs
Sign in
officialyantrikdbsigned

java-modern

yantrik/java-modern@0.1.0·47 facts·1124 KB·1 downloads·by spranab

Java as it is written on a current LTS, not as it was written in 2014. Records, sealed types and pattern matching; virtual threads and what still pins them; the concurrency and equality contracts that fail silently; java.time, BigDecimal and the numeric traps; and the JDK APIs that are insecure by default until you configure them.

covers

Modern Java language features: records, sealed types, pattern matching, text blocks · Java virtual threads and structured concurrency · Java concurrency correctness: visibility, atomicity, thread-safe collections · Java equality, hashCode, comparison and collection contracts · Java exception handling, try-with-resources and interruption · java.time, BigDecimal and numeric precision · Java streams and the Collections API · Java security defaults: SQL injection, XML parsing, deserialization, randomness

rules this pack installs (20)

Injected on every turn while mounted. Shown verbatim — a pack's rules are public by design.

Data carriers are records: A class whose purpose is to hold values is a `record`. Validation and defensive copying (`List.copyOf`) go in the compact constructor. Manual `equals`/`hashCode` pairs are not written by hand for value types.
Closed hierarchies are sealed and switched exhaustively: A fixed set of subtypes is declared `sealed ... permits`. Dispatch uses a pattern `switch` with no `default`, so adding a subtype becomes a compile error rather than a runtime fallthrough. Guards use `when`.
instanceof binds a pattern variable: `if (o instanceof Customer c)` — never instanceof followed by a cast. Nested structure is destructured with record patterns rather than chained accessors.
Concurrency starts with virtual threads and a closed executor: Task-per-request work uses `Executors.newVirtualThreadPerTaskExecutor()` inside try-with-resources. Virtual threads are never pooled or reused, and blocking I/O inside one is correct rather than something to avoid. `StructuredTaskScope` is only used with an explicit note that it is a preview API.
Shared mutable state uses the right primitive, not volatile alone: `volatile` publishes a value; it does not make read-modify-write atomic. Counters use `AtomicInteger`/`LongAdder`, maps use `ConcurrentHashMap` with its compound operations (`computeIfAbsent`, `merge`), and check-then-act sequences over a concurrent collection are never written as separate calls.
InterruptedException is propagated or the flag is restored: `catch (InterruptedException e)` either rethrows or calls `Thread.currentThread().interrupt()` before returning. It is never logged and discarded.
equals and hashCode are overridden together, over the same fields: And nothing used in `hashCode` is mutated after the object enters a hash-based collection. A `Comparator` returning 0 implies `equals`, and is built with `Comparator.comparing(...).thenComparing(...)` rather than subtraction.
Boxed numbers are compared with equals: `==` on `Integer`, `Long` or `Double` is identity. Unboxing a value that may be absent is checked first — a null `Integer` assigned to an `int` throws at the assignment.
Money and exact decimals use BigDecimal built from strings: `BigDecimal.valueOf` or `new BigDecimal("…")`, never `new BigDecimal(double)`. Every `divide` carries a scale and a `RoundingMode`. Equality between BigDecimals uses `compareTo(x) == 0`, because `equals` also compares scale.
Time uses java.time, with the type that matches the concept: `Instant` for a moment, `ZonedDateTime` when the zone is meaningful, `LocalDate`/`LocalDateTime` only for zone-free wall-clock values, `Duration` for elapsed time measured with `System.nanoTime`. `java.util.Date`, `Calendar` and `SimpleDateFormat` do not appear.
Optional is returned, never stored or accepted: No `Optional` fields, no `Optional` parameters, no `.get()` without a presence check. Absence is handled with `orElseGet`, `orElseThrow` or `map`.
Resources are managed by try-with-resources: Every `AutoCloseable` — streams, connections, executors — is acquired in the resource clause. No manual `finally { close(); }`, and no `return` or `throw` inside a `finally`.
Exceptions caught are exceptions that can be handled: Specific types, multi-catch where behaviour is shared. Never `catch (Throwable)`, never an empty catch block, and never catch-log- continue for something the caller needs to know about. Failures that cannot be handled locally propagate.
Every SQL value is a bind parameter: `PreparedStatement` with `?`, or `setParameter` in JPQL. No string concatenation into a query under any circumstance. Identifiers are validated against an allowlist rather than interpolated.
Parsers are hardened before they touch untrusted input: XML factories disable DOCTYPE declarations and external entities. Untrusted bytes are never passed to `ObjectInputStream.readObject`. Deserialisation uses a schema-bearing format.
Unguessable values come from SecureRandom: Tokens, session identifiers, salts and nonces use `SecureRandom`. `java.util.Random` and `ThreadLocalRandom` are for simulation only. Passwords go through a slow KDF with a per-password salt, and digest comparison uses `MessageDigest.isEqual`.
Nulls are rejected at the boundary: Constructor and public-method arguments that must not be null go through `Objects.requireNonNull(x, "name")`. Null-safe helpers (`Objects.equals`, `Objects.hash`, `requireNonNullElse`) replace hand-written null checks.
Collections are chosen for their mutability contract: `List.of` / `Map.of` for immutable literals, `List.copyOf` to snapshot, `new ArrayList<>(…)` when it must be mutable. Structural modification during iteration uses `removeIf` or the iterator. `Collectors.toMap` always supplies a merge function.
Nested classes are static unless the outer instance is needed: Non-static inner classes pin their enclosing instance in memory, which leaks whenever the inner object outlives the outer's usefulness.
Modern syntax where it is genuinely clearer: Text blocks for embedded SQL, JSON and HTML. Sequenced-collection methods (`getFirst`, `getLast`, `reversed`) instead of index arithmetic. Enhanced switch expressions over statement switches with fallthrough. String templates (`STR."…"`) are never emitted — the feature was withdrawn and does not compile.

evaluation notes

skills.constitution — pack installs 20 always-on rule(s); they are shown verbatim on the listing for review

install

Native client — verifies against the registry, then installs:

curl -O https://packs.yantrikdb.com/client/yantrik_pack_client.py
python yantrik_pack_client.py install yantrik-java-modern

Manual — download first, then:

yantrikdb pack info java-modern-0.1.0.ydbpack
yantrikdb pack install java-modern-0.1.0.ydbpack

Verify any copy of this file, wherever it came from:

curl "https://packs.yantrikdb.com/api/v1/verify?sha256=4e90f788c7006ff3c41a7619952d1086aba21d16df389b7ce1e203dd9cfb22a3"

get it

Download 0.1.0
sha256
4e90f788c7006ff3c41a7619952d1086aba21d16df389b7ce1e203dd9cfb22a3
digest
blake3:855cf683512a32d1a5415f469a7f143c9b9b53eb34a98a017510f9b2c332fb42
signer
13a9232bc7829ea6a03e4bd6a0706c08f80ff4a2ecc83fd10062314acfb59683