On Tue, 31 Jan 2023 20:18:05 GMT, Michael Strauß <[email protected]> wrote:
> 2. ObservableMap. Similarly to Node.getProperties(), I wonder if there might
> be a better way to observe the changes. May be a different metaphor
> (subscription?), like adding a value change listener to a specific key. We
> do need a set of keys (perhaps that can be an ObservableSet). Having said
> that, ObservableMap is good enough solution, and forgive me for stating the
> obvious, it should not initialize anything if the platform properties have
> not been requested by the application code.
I've pulled on that string a little more:
#### 1. `Optional` API + listeners
This would remove the `ObservableMap` implementation, and add the following
methods instead:
interface Preferences {
...
void addListener(String key, InvalidationListener listener);
void removeListener(String key, InvalidationListener listener);
void addListener(String key, ChangeListener<?> listener);
void removeListener(String key, ChangeListener<?> listener);
Optional<Integer> getInteger(String key);
Optional<Double> getDouble(String key);
...
}
I don't quite like this idea though, since it would allow developers to either
add listeners to non-existent keys, or require developers to probe whether a
key exists before adding a listener for it (maybe by also adding a `boolean
keyExists(String key)` method. It also doesn't allow developers to enumerate
the keys that are available on a given platform.
If we added a set of keys (maybe as an `ObservableSet`), the result is
basically a map. We're better off implementing `ObservableMap` in this case.
#### 2. `Property` API
interface Preferences {
...
ReadOnlyIntegerProperty getIntegerProperty(String key);
ReadOnlyDoubleProperty getDoubleProperty(String key);
...
}
With this idea, instead of having manual listener management and an API to
query value mappings, we'd expose read-only properties for keys. We might also
need a method like `boolean keyExists(String key)`, but we can't enumerate all
platform preferences with this approach.
#### 3. `Optional` + `Property` API
interface Preferences {
...
Optional<ReadOnlyIntegerProperty> getIntegerProperty(String key);
Optional<ReadOnlyDoubleProperty> getDoubleProperty(String key);
...
}
This API combines all aspects into a single method call. We also can't
enumerate the platform preferences.
I still think that implementing `ObservableMap` is preferable to all of these
alternatives.
-------------
PR Comment: https://git.openjdk.org/jfx/pull/1014#issuecomment-1500535347