Uploaded image for project: 'yangtools'
  1. yangtools
  2. YANGTOOLS-1496

Add Runnable-backed Registration implementations

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Medium Medium
    • 13.0.5
    • None
    • common

      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.

            rovarga Robert Varga
            rovarga Robert Varga
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: