-
Notifications
You must be signed in to change notification settings - Fork 165
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
(#1575) Generify org.cactoos.collection
package
#1624
(#1575) Generify org.cactoos.collection
package
#1624
Conversation
7bb870b
to
b767d96
Compare
this.col = src; | ||
@SuppressWarnings("unchecked") | ||
public Immutable(final Collection<? extends X> src) { | ||
this.col = (Collection<X>) src; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rocket-3 instead of this, you should change the type of the col
field. You won't need any cast in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then, I'll need to do unchecked cast in Collection::iterator method.
Collection<? extends X>::iterator
return type is Iterator<? extends X>
.
Which breaks the Iterable<X>::iterator
return type Iterator<X>
contract.
I also can do this without cast, but there's the unchecked cast inside IterableOf(Scalar<Iterator<? extends X>>)
public Iterator<X> iterator() {
return new IterableOf<X>(this.col::iterator).iterator();
}
It seems messy for me because:
- Unnecessary constructioning inside method
- Unnecessary coupling on
org.cactoos.iterable.IterableOf
@victornoel, WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rocket-3 then you need to propagate the same kind of change everywhere needed until everything types correctly :) that's the purpose of this series of tickets: that the whole codebase uses generic with variance like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@victornoel, sorry, I didn't get it
We can't propagate changes to JDK
As I see, we have two ways:
- to do unchecked cast
- to rely on class which does uchecked cast
Which one you would prefer? Or you see, there's more ways?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rocket-3 We should be able to propagate as much as possible in our classes, and then normally, no cast is needed. If some casts are needed, let's put them at the last moment possible (usually inside a method implementation).
So let's go with this and see what happens with the two classes you started modifying!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@victornoel, no other way for NoNulls
, cause we have an add
and addAll
impls.
We can't add items to a Collection<? extends X>
instance by JDK design, only extract, see PECS trilemma.
By the way, changed Immutable
, see in the recent commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rocket-3 ok, in that case, for NoNulls
, because of its nature (being mutable), it's not possible to apply variance like this and thus the constructor should only take a X. If we cast, this will create errors at runtime if incorrect elements are added to such a collection. Let's also add a comment in the javadoc of the constructor so that we remember why it's like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@victornoel, wrote a test for this behaviour. Seems like no runtime exceptions, WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rocket-3 here is a failing test:
@Test
void fails() {
Collection<Double> a = new ArrayList<Double>();
Collection<Number> b = new NoNulls<>(a);
b.add(Long.valueOf(4));
Double oupsNotADouble = a.iterator().next();
}
This will throw java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double
even though it compiles correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I've removed uchecked cast, added impl. note and removed test created recently.
b9ffff0
to
4db7187
Compare
org.cactoos.collection
package
@rocket-3 nice use of |
Codecov Report
@@ Coverage Diff @@
## master #1624 +/- ##
============================================
- Coverage 90.12% 90.10% -0.02%
- Complexity 1602 1603 +1
============================================
Files 298 298
Lines 3745 3749 +4
Branches 122 122
============================================
+ Hits 3375 3378 +3
Misses 337 337
- Partials 33 34 +1
Continue to review full report at Codecov.
|
@rocket-3 the linters CI job failed, it must be fixed before we can merge this PR :) |
Also because of that |
…'mvn javadoc:javadoc' with 'implNode' tag by removing one.
@rultor merge |
@rocket-3 thx |
@victornoel OK, I'll try to merge now. You can check the progress of the merge here |
@victornoel Done! FYI, the full log is here (took me 13min) |
For #1575.
Generify parameter types in constructors of org.cactoos.collection package classses:
It's done by using unchecked casts, because there are absolutely no other ways to achive this, due to JDK interfaces limitations.