C - JComponent typeD - data typepublic class WeakComponentData<C extends JComponent,D> extends Object
JComponent-related data that have to be stored using weak references. The only hard reference
to that data will be stored within JComponent itself, so the data will only get removed along with the JComponent itself
or if removal calls are made explicitly.
This class also keeps a Set of weak references to all components that have data of this kind stored within them. It can be used
to iterate through all of those components whenever it is needed.
Note that stored data cannot be null at any point as null is considered to be no data case.| Modifier and Type | Field and Description |
|---|---|
protected Set<C> |
components
Set of JComponents that have data of this kind stored within them. |
protected String |
key
Key used to place data within
JComponent using JComponent.putClientProperty(Object, Object) method. |
| Constructor and Description |
|---|
WeakComponentData(String key,
int initialCapacity)
Constructs new
WeakComponentData. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
allMatch(BiPredicate<C,D> predicate)
Returns whether or not all of the stored data pieces are accepted by specified
BiPredicate. |
boolean |
anyMatch(BiPredicate<C,D> predicate)
Returns whether or not at least one of the stored data pieces is accepted by specified
BiPredicate. |
D |
clear(C component)
Clears data stored in the
JComponent. |
D |
clear(C component,
BiConsumer<C,D> removedDataConsumer)
Clears data stored in the
JComponent. |
Set<C> |
components()
Returns
Set of JComponents that have data of this kind stored within them. |
boolean |
contains(C component)
Returns whether or not specified
JComponent has data of this kind stored within. |
void |
forEach(BiConsumer<C,D> consumer)
Provides each stored data piece into specified
BiConsumer. |
D |
get(C component)
Returns data stored in the
JComponent. |
D |
get(C component,
Function<C,D> defaultData)
Returns data stored in the
JComponent. |
D |
modify(C component,
BiFunction<C,D,D> modifiedData,
Function<C,D> defaultData)
Modifies data stored in the
JComponent using specified BiFunction and returns it. |
boolean |
noneMatch(BiPredicate<C,D> predicate)
Returns whether or not none of the stored data pieces are accepted by specified
BiPredicate. |
D |
set(C component,
D data)
Stores data in the specified
JComponent. |
D |
set(C component,
D data,
BiConsumer<C,D> oldDataConsumer)
Stores data in the specified
JComponent. |
int |
size()
Returns size of the
Set of JComponents that have data of this kind stored within them. |
@NotNull protected final String key
JComponent using JComponent.putClientProperty(Object, Object) method.
Make sure that every manager uses its own unique keys for registering different data to avoid unwanted overwrites.@NotNull protected final Set<C extends JComponent> components
Set of JComponents that have data of this kind stored within them.
WeakHashSet implementation is used to avoid hard component references that could cause memory leaks.public WeakComponentData(@NotNull String key, int initialCapacity)
WeakComponentData.key - key used to place data within JComponentinitialCapacity - initial capacity for the Set of JComponents@NotNull public Set<C> components()
Set of JComponents that have data of this kind stored within them.Set of JComponents that have data of this kind stored within thempublic int size()
Set of JComponents that have data of this kind stored within them.Set of JComponents that have data of this kind stored within thempublic boolean contains(@NotNull C component)
JComponent has data of this kind stored within.component - JComponent to check data intrue if specified JComponent has data of this kind stored within, false otherwise@Nullable public D get(@NotNull C component)
JComponent.
Could return null if no data can be found.component - JComponent to retrieve data fromJComponent@NotNull public D get(@NotNull C component, @NotNull Function<C,D> defaultData)
JComponent.
If no data can be found it will be created using mapping Function, stored within JComponent and returned as result.component - JComponent to retrieve data fromdefaultData - Function that creates default data in case it is missingJComponent@NotNull public D modify(@NotNull C component, @NotNull BiFunction<C,D,D> modifiedData, @NotNull Function<C,D> defaultData)
JComponent using specified BiFunction and returns it.
If no data can be found it will be created using mapping Function, stored within JComponent and returned as result.component - JComponent to retrieve and modify data frommodifiedData - BiFunction that modifies data in case it existsdefaultData - Function that creates default data in case it is missingJComponent@Nullable public D set(@NotNull C component, @Nullable D data)
JComponent.
Providing null data would result in calling clear(JComponent) method instead.component - JComponent to store data indata - data to store@Nullable public D set(@NotNull C component, @Nullable D data, @NotNull BiConsumer<C,D> oldDataConsumer)
JComponent.
Providing null data would result in calling clear(JComponent, BiConsumer) method instead.component - JComponent to store data indata - data to storeoldDataConsumer - BiConsumer for previous data@Nullable public D clear(@NotNull C component)
JComponent.component - JComponent to clear stored data for@Nullable public D clear(@NotNull C component, @NotNull BiConsumer<C,D> removedDataConsumer)
JComponent.component - JComponent to clear stored data forremovedDataConsumer - BiConsumer for removed datapublic void forEach(@NotNull BiConsumer<C,D> consumer)
BiConsumer.consumer - BiConsumer for JComponent and datapublic boolean anyMatch(@NotNull BiPredicate<C,D> predicate)
BiPredicate.predicate - BiPredicate for JComponent and datatrue if at least one of the stored data pieces is accepted by specified BiPredicate, false otherwisepublic boolean allMatch(@NotNull BiPredicate<C,D> predicate)
BiPredicate.predicate - BiPredicate for JComponent and datatrue if all of the stored data pieces are accepted by specified BiPredicate, false otherwisepublic boolean noneMatch(@NotNull BiPredicate<C,D> predicate)
BiPredicate.predicate - BiPredicate for JComponent and datatrue if none of the stored data pieces are accepted by specified BiPredicate, false otherwiseCopyright © 2020. All rights reserved.