[YANGTOOLS-1509] Revise ExtensibleObject API Created: 16/May/23 Updated: 17/May/23 Resolved: 17/May/23 |
|
| Status: | Resolved |
| Project: | yangtools |
| Component/s: | common |
| Affects Version/s: | None |
| Fix Version/s: | 11.0.0 |
| Type: | Improvement | Priority: | Medium |
| Reporter: | Robert Varga | Assignee: | Robert Varga |
| Resolution: | Done | Votes: | 0 |
| Labels: | pt | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
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. |