You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2022/05/27 07:05:34 UTC

[GitHub] [netbeans] lkishalmi commented on a diff in pull request #4157: Added support for properties backed up by functional interfaces

lkishalmi commented on code in PR #4157:
URL: https://github.com/apache/netbeans/pull/4157#discussion_r883337120


##########
platform/openide.nodes/src/org/openide/nodes/PropertySupport.java:
##########
@@ -360,6 +522,35 @@ public ReadWrite(String name, Class<T> type, String displayName, String shortDes
         }
     }
 
+    private static final class FunctionalProperty<T> extends PropertySupport<T> {
+        private final Supplier<T> supplier;
+        private final Consumer<T> consumer;
+
+        public FunctionalProperty(String name, Class<T> type, String displayName, String shortDescription, Supplier<T> supplier, Consumer<T> consumer) {
+            super(name, type, displayName, shortDescription, supplier != null, consumer != null);
+            this.supplier = supplier;
+            this.consumer = consumer;
+        }
+
+        @Override
+        public T getValue() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {

Review Comment:
   Yes, copypaste indeed. Good catch!



##########
platform/openide.nodes/src/org/openide/nodes/PropertySupport.java:
##########
@@ -322,14 +485,13 @@ public void setValue(T val)
         * @return the property editor or <CODE>null</CODE> if there should not be
         *    any editor.
         */
+        @Override
         public PropertyEditor getPropertyEditor() {
             if (propertyEditorClass != null) {
                 try {
-                    return propertyEditorClass.newInstance();
-                } catch (InstantiationException ex) {
+                    return propertyEditorClass.getDeclaredConstructor().newInstance();
+                } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException ex) {

Review Comment:
   Could be. Just accepted whatever the IDE offered to convert to Multicatch.



##########
platform/openide.nodes/src/org/openide/nodes/PropertySupport.java:
##########
@@ -87,6 +92,162 @@ static <T> T cast(Class<T> c, Object o) {
         }
     }
 
+    /**
+     * Creates a "virtual" property where getter and setter are backed by the
+     * provided {@link Supplier} and {@link Consumer} functional interfaces.
+     * @param <T> the type of the property
+     * @param name the name of the property
+     * @param valueType the type of the property
+     * @param displayName the display name of the property, can be {@code null}.
+     * @param shortDescription the short description (used in tooltip) of the property, can be {@code null}.
+     * @param supplier the getter functional interface, can be {@code null} for write-only properties.
+     * @param consumer the setter functional interface, can be {@code null} for read-only properties.
+     *
+     * @since 7.62
+     * @return a {@link PropertySupport} instance where getter and setter are
+     *         backed by the provided functional interfaces.
+     */
+    public static <T> PropertySupport<T> readWrite(String name, Class<T> valueType, String displayName, String shortDescription, Supplier<T> supplier, Consumer<T> consumer) {
+        return new FunctionalProperty<>(name, valueType, displayName, shortDescription, supplier, consumer);
+    }
+
+    /**
+     * Creates a "virtual" property where getter and setter are backed by the
+     * provided {@link Supplier} and {@link Consumer} functional interfaces.
+     * @param <T> the type of the property
+     * @param name the name of the property
+     * @param valueType the type of the property
+     * @param displayName the display name of the property, can be {@code null}.
+     * @param supplier the getter functional interface, can be {@code null} for write-only properties.
+     * @param consumer the setter functional interface, can be {@code null} for read-only properties.
+     *
+     * @since 7.62
+     * @return a {@link PropertySupport} instance where getter and setter are
+     *         backed by the provided functional interfaces.
+     */
+    public static <T> PropertySupport<T> readWrite(String name, Class<T> valueType, String displayName, Supplier<T> supplier, Consumer<T> consumer) {

Review Comment:
   Thanks for the feedback, I think I'd go with the fluent construction. That's a good idea! Thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists