Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to have different implementations of extensions #1152

Merged
merged 2 commits into from
Feb 25, 2020
Merged

Conversation

jonenst
Copy link
Contributor

@jonenst jonenst commented Feb 10, 2020

Signed-off-by: Jon Harper jon.harper87@gmail.com

Please check if the PR fulfills these requirements (please use '[x]' to check the checkboxes, or submit the PR and then click the checkboxes)

  • The commit message follows our guidelines
  • Docs have been added / updated (for bug fixes / features)

Does this PR already have an issue describing the problem ? If so, link to this issue using '#XXX' and skip the rest
#828

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Feature

What is the current behavior? (You can also link to an open issue here)
It's not possible to have multiple implementations of an extension

What is the new behavior (if this is a feature change)?
it is possible to have multiple implementations of an extension

Does this PR introduce a breaking change or deprecate an API? If yes, check the following:
NO

Other information:

(if any of the questions/checkboxes don't apply, please delete them entirely)

@jonenst jonenst mentioned this pull request Feb 10, 2020
5 tasks
@jonenst
Copy link
Contributor Author

jonenst commented Feb 10, 2020

Coverage is done is #1153

Copy link
Contributor

@mathbagu mathbagu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I request at least the adding of the license header

protected T extendable;

protected AbstractExtensionAdder(T extendable) {
this.extendable = extendable;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 questions: do we allow null values and is extendable final?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no problem with forbidding null values (this is always called by doing extendable.newExtensionAdder(..)) so the extendable is never null.
We can also make the extendable final, even private if we want (and pass it to the createExtension method.

You think we should do these changes ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's cleaner if you declare it private, final and if you assert the extendable is not null

@jonenst jonenst changed the title Allow to have different implementations of extensions [WIP] Allow to have different implementations of extensions Feb 11, 2020
@jonenst
Copy link
Contributor Author

jonenst commented Feb 11, 2020

Will add a few tests in this PR by extracting the static state of ExtendableProviders

@jonenst jonenst force-pushed the extension2 branch 2 times, most recently from c4c5b97 to cebfc1a Compare February 13, 2020 17:10
@jonenst
Copy link
Contributor Author

jonenst commented Feb 13, 2020

I added tests in the same module as the code instead of relying on cross module testing. Also this improves the test coverage.

One problem with the current design is that it allows a compilation (with a warning) of the following code:

public void bad(VoltageLevel vl) {
  vl.newExtension(ConnectablePositionBuilder.class).add();//bad: compiles but shouldn't
}

The previous API was better (although not perfect)

public void bad(VoltageLevel vl) {
  vl.addExtension(ConnectablePosition.class, new ConnectablePosition<>());//good: compile error
  vl.addExtension(ConnectablePosition.class, new ConnectablePosition());//bad, compiles but shouldn't. But you are using raw types 
}

@jonenst
Copy link
Contributor Author

jonenst commented Feb 14, 2020

The previous API also allowed to compile the following:

        vl.getExtension(ConnectablePosition.class); // should not compile; there is a warning though

I think we have to use the same trick as Collections.emptyList() vs Collections.EMPTY_LIST to allow the type checker to check the generic types

@jonenst jonenst changed the title [WIP] Allow to have different implementations of extensions Allow to have different implementations of extensions Feb 14, 2020
@jonenst
Copy link
Contributor Author

jonenst commented Feb 14, 2020

@mathbagu What do you think of my proposed "clazz" static method ? It fixes the holes in the previous and new APIS.

It is only needed for generic extensions and generic extension adders, but should we say that every extension and extension adder should implement it ?

@jonenst
Copy link
Contributor Author

jonenst commented Feb 14, 2020

We can also discuss to find a better solution to the asymetry introduced by the new API:

// before it was symmetric:
   extendable.addExtension(ExtensionAdder.class, ...);
   extendable.getExtension(Extension.class);

//now it is asymetric
  extendable.newExtension(ExtensionAdder.class);
  extendable.getExtension(Extension.class);

}

private static interface SimpleExtensionAdder extends ExtensionAdder<SimpleExtendable, SimpleExtension> {
default Class<SimpleExtension> getExtensionClass() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's dirty but I assume we can write dirty code in tests... Default should not be used to avoid duplications

@jonenst
Copy link
Contributor Author

jonenst commented Feb 17, 2020

The previous API also allowed to compile the following:

        vl.getExtension(ConnectablePosition.class); // should not compile; there is a warning though

I think we have to use the same trick as Collections.emptyList() vs Collections.EMPTY_LIST to allow the type checker to check the generic types

This is only in eclipse. Using maven (ie javac) or intellij, we get the expected compile error.

Copy link
Member

@geofjamg geofjamg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok apart some cosmetic Java & doc issues

@jonenst jonenst force-pushed the extension2 branch 2 times, most recently from 86e7e43 to d6b5719 Compare February 24, 2020 18:13
Copy link
Contributor

@mathbagu mathbagu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonenst Could you check my last comment please?

"Multiple ExtensionAdderProviders found for ExtensionAdder {} for implementation {} : {}",
type.getSimpleName(), name, providers);
throw new PowsyblException(
"Multiple platform configuration providers found");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a copy/past error: are you sure about this message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Signed-off-by: Jon Harper <jon.harper87@gmail.com>
@sonarcloud
Copy link

sonarcloud bot commented Feb 25, 2020

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 2 Code Smells

90.0% 90.0% Coverage
0.0% 0.0% Duplication

@geofjamg geofjamg merged commit 2a99a79 into master Feb 25, 2020
Evolutions IIDM automation moved this from PR in progress to Merged PR Feb 25, 2020
@geofjamg geofjamg deleted the extension2 branch February 25, 2020 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Evolutions IIDM
  
Merged PR
Development

Successfully merging this pull request may close these issues.

None yet

4 participants