Update Programming Guide

2025-07-06 18:24:12 +02:00
parent 992806c58c
commit 45bbd65ae2

@@ -17,10 +17,10 @@ import conflux.Listener;
Define typed keys for the data you want to store: Define typed keys for the data you want to store:
```java ```java
Key<Integer> counterKey = new Key<>("counter", Integer.class); Key<Integer> counterKey = Key.of("counter", Integer.class);
Key<String> nameKey = new Key<>("name", String.class); Key<String> nameKey = Key.of("name", String.class);
Key<int[]> arrayKey = new Key<>("numbers", int[].class); Key<int[]> arrayKey = Key.of("numbers", int[].class);
Key<MyPojo> pojoKey = new Key<>("myPojo", MyPojo.class); Key<MyPojo> pojoKey = Key.of("myPojo", MyPojo.class);
``` ```
## Storing and Retrieving Values ## Storing and Retrieving Values
@@ -121,11 +121,11 @@ Ctx.INSTANCE.put(pojoKey, new MyPojo("Widget Pro", 99)); // triggers listener
Once a key is registered with a type, you cannot use the same name with a different type: Once a key is registered with a type, you cannot use the same name with a different type:
```java ```java
Key<Integer> myKey = new Key<>("sharedKey", Integer.class); Key<Integer> myKey = Key.of("sharedKey", Integer.class);
Ctx.INSTANCE.put(myKey, 5); Ctx.INSTANCE.put(myKey, 5);
// later: // later:
Key<String> badKey = new Key<>("sharedKey", String.class); Key<String> badKey = Key.of("sharedKey", String.class);
Ctx.INSTANCE.put(badKey, "wrong type"); // throws IllegalStateException Ctx.INSTANCE.put(badKey, "wrong type"); // throws IllegalStateException
``` ```
@@ -153,7 +153,7 @@ Ctx.INSTANCE.clear();
## Complete Example ## Complete Example
```java ```java
Key<Integer> temperatureKey = new Key<>("temperature", Integer.class); Key<Integer> temperatureKey = Key.of("temperature", Integer.class);
Listener<Integer> display = t -> Listener<Integer> display = t ->
System.out.println("Temperature changed to " + t + "°C"); System.out.println("Temperature changed to " + t + "°C");