Summary
Adds a public static utility method to CssMetaData for use by custom and core Nodes to simplify boilerplate around styleable properties as well as to save some memory.
Problem
- the same less-than-optimal implementation is duplicated throughout the javafx code base which combines the parent and child styleable properties, using ArrayList wrapped in a Collections.unmodifiableList()
- the capacity of underlying ArrayList might exceed the number of items, wasting memory
- a potential exists for the custom component developer to inadvertently create a sub-standard implementation (i.e. return a List which does not implement RandomAccess interface), or forget to include parent's styleables.
Non-Goals
- not redesigning the lazy initialization of STYLEABLES list
- not changing the way styleables are enumerated in Nodes and Controls
- not adding any new methods to JDK (collections)
Solution
The solution is to provide a narrow-focused utility method in CssMetaData class that combines the parent styleable properties with the properties declared in the child class, using more efficient (in terms of memory and cpu) code than the existing implementation.
The new code also ensures that the parent styleable properties precede the child ones (which may or may not be a requirement), and also that the resulting List is immutable and implements RandomAccess interface.
Specification
+ /**
+ * Utility method which combines {@code CssMetaData} items in one immutable list.
+ * <p>
+ * The intended usage is to combine the parent and the child {@code CssMetaData} for
+ * the purposes of {@code getClassCssMetaData()} method, see for example {@link Node#getClassCssMetaData()}.
+ * <p>
+ * Example:
+ * <pre>{@code
+ * private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES = CssMetaData.combine(
+ * <Parent>.getClassCssMetaData(),
+ * STYLEABLE1,
+ * STYLEABLE2
+ * );
+ * }</pre>
+ * This method returns an instance of a {@code List} that implements
+ * {@link java.util.RandomAccess} interface.
+ *
+ * @param inheritedFromParent the {@code CssMetaData} items inherited from parent, must not be null
+ * @param items the additional items
+ * @return the immutable list containing all of the items
+ *
+ * @since 22
+ */
+ public static List<CssMetaData<? extends Styleable, ?>> combine(
+ List<CssMetaData<? extends Styleable, ?>> inheritedFromParent,
+ CssMetaData<? extends Styleable, ?>... items)
- csr of
-
JDK-8320796 CssMetaData.combine()
-
- Closed
-