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

Revise ExtensibleObject API

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Done
    • Icon: Medium Medium
    • 11.0.0
    • None
    • common

      ExtensibleObject relies on ClassToInstanceMap indirection through getExtensions() method. Since we have default methods (since Java 8), we can do better without the Guava tie-in:

       

      interface ExtensibleObject {
          default <T extends E> @Nullable T extension(final Class<T> type) {
            // Iterate over availableExtension() and use isInstance() check
          }
      
          Collection<? extends E> supportedExtensions();
      }

      Given there typically are only one or two extensions, the search is equally-perfomant and users end up cleaner:

      ExtensibleObject obj;
      var ext = obj.extension(FooExtension.class);
      // ... whatever
      

      where previous they'd do:

      ExtensibleObject obj;
      var ext = obj.getExtensions().getInstance(FooExtension.class);
      // ... whatever 

      This will make it slightly ugly for extensions which build on top of each other, but we have exactly 0 of those and, at the end on the day, they'd have to co-operate anyway, so a reasonable implementation would use the same object – and we cover that use case.

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

              Created:
              Updated:
              Resolved: