Update Programming Guide
@@ -17,10 +17,10 @@ import conflux.Listener;
|
||||
Define typed keys for the data you want to store:
|
||||
|
||||
```java
|
||||
Key<Integer> counterKey = new Key<>("counter", Integer.class);
|
||||
Key<String> nameKey = new Key<>("name", String.class);
|
||||
Key<int[]> arrayKey = new Key<>("numbers", int[].class);
|
||||
Key<MyPojo> pojoKey = new Key<>("myPojo", MyPojo.class);
|
||||
Key<Integer> counterKey = Key.of("counter", Integer.class);
|
||||
Key<String> nameKey = Key.of("name", String.class);
|
||||
Key<int[]> arrayKey = Key.of("numbers", int[].class);
|
||||
Key<MyPojo> pojoKey = Key.of("myPojo", MyPojo.class);
|
||||
```
|
||||
|
||||
## 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:
|
||||
|
||||
```java
|
||||
Key<Integer> myKey = new Key<>("sharedKey", Integer.class);
|
||||
Key<Integer> myKey = Key.of("sharedKey", Integer.class);
|
||||
Ctx.INSTANCE.put(myKey, 5);
|
||||
|
||||
// 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
|
||||
```
|
||||
|
||||
@@ -153,7 +153,7 @@ Ctx.INSTANCE.clear();
|
||||
## Complete Example
|
||||
|
||||
```java
|
||||
Key<Integer> temperatureKey = new Key<>("temperature", Integer.class);
|
||||
Key<Integer> temperatureKey = Key.of("temperature", Integer.class);
|
||||
|
||||
Listener<Integer> display = t ->
|
||||
System.out.println("Temperature changed to " + t + "°C");
|
||||
|
||||
Reference in New Issue
Block a user