[YANGTOOLS-1496] Add Runnable-backed Registration implementations Created: 05/Apr/23  Updated: 18/Jan/24

Status: In Review
Project: yangtools
Component/s: common
Affects Version/s: None
Fix Version/s: 13.0.2

Type: Improvement Priority: Medium
Reporter: Robert Varga Assignee: Robert Varga
Resolution: Unresolved Votes: 0
Labels: pt
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Registration is a simple interface, which is quite easy to get wrong due to simple lamdas not being idempotent:

Registration reg = () -> {
        // not idempotent!
    };

reg.close() // executes the block
reg.close() // also executes the block!

Current way of doing this is quite verbose:

var reg = AbstractRegistration() {
    @Override
    protected void removeRegistration() {
        // idempotent
    }
};

reg.close() // executes the block
reg.close() // does nothing

Add Registration.of(Runnable) utility method which wraps a callback and executes it just once, backed by AbstractRegistration logic. This allows users to use lambdas (and method references) and wrap them with a guard:

var reg = Registration.of(() -> {
        // idempotent
    });

reg.close() // executes the block
reg.close() // does nothing

The same patterns applies to ObjectRegistration and ListenerRegistration.


Generated at Wed Feb 07 20:56:19 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.