Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8307984

Allow easy creation of read only variants of properties

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • None
    • javafx
    • None

      Currently creating a read only variant of a writable property must be done in this way:

            ReadOnlyObjectWrapper<X> writableX
                 = new ReadOnlyObjectWrapper<>();
            ReadOnlyProperty<X> readOnlyX = writableX.getReadOnlyProperty();

      The name of the wrapper (ReadOnlyObjectWrapper) is confusing; it's actually a writable property. Using plain `ObjectProperty<X>` for this declaration to alleviate this makes the `getReadOnlyProperty` method inaccessible.

      The word Wrapper is also a source of confusion. Nothing is wrapped, it is a subclass that re-introduces ReadOnly in the hierarchy (all ObjectProperties are already ReadOnlyObjectProperties). In other words, this "wrapper" can't be used to wrap an existing property.

      I propose to simplify this and add this functionality to all properties by default. Instead of having to use this class, any property could offer a read only variant of itself by calling `getReadOnlyProperty`. The above example would then become:

            ObjectProperty<X> writableX
                 = new SimpleObjectProperty<>();
            ReadOnlyProperty<X> readOnlyX = writableX.getReadOnlyProperty();

      Note that it is also possible to cast `writableX` to `ReadOnlyProperty<X>` and although that communicates the correct intent, the encapsulation could be broken by casting it back to `ObjectProperty<X>`.

      The implementation would not be any heavier than the current solution. A new instance is still created when a read only property is desired, just like `ReadOnlyObjectWrapper` creates the internal private class `ReadOnlyPropertyImpl`.

            jhendrikx John Hendrikx
            jhendrikx John Hendrikx
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: