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

#68 Implement withFirst for primitive stream #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

canthonyl
Copy link

Interesting project! I've implemented 68 to add withFirst for primitive stream ex.

Please have a look. Unit Test case will follow.

@coveralls
Copy link

coveralls commented Mar 25, 2018

Coverage Status

Coverage decreased (-0.3%) to 98.239% when pulling 9777300 on canthonyl:issue/68 into 5b1e479 on amaembo:master.

canthonyl
@amaembo
Copy link
Owner

amaembo commented Mar 26, 2018

Thank you for your contribution! I don't like too much code duplication. Also code coverage decrease is too high. I suggest to put all the spliterators inside single class. Like WithFirstSpliterator could be made as abstract class and specific implementations named OfRef, OfInt, OfLong and OfDouble could be created as nested classes.

private static final int STATE_NONE = 0;
private static final int STATE_FIRST_READ = 1;
private static final int STATE_INIT = 2;
private static final int STATE_EMPTY = 3;
Copy link
Owner

Choose a reason for hiding this comment

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

These constants can be shared between all implementations in abstract class

private Spliterator.OfDouble source;
private DoubleWithFirstSpliterator prefix;
private volatile double first;
private volatile int state = STATE_NONE;
Copy link
Owner

Choose a reason for hiding this comment

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

Fields lock and state could be shared as well

}
}

private void release() {
Copy link
Owner

Choose a reason for hiding this comment

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

Methods acquire and release could be shared


private ReentrantLock lock;
private Spliterator.OfDouble source;
private DoubleWithFirstSpliterator prefix;
Copy link
Owner

Choose a reason for hiding this comment

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

Prefix could probably be a shared field of abstract type

if (prefixState != STATE_NONE)
return;
if (prefix != null) {
prefix.doInit();
Copy link
Owner

Choose a reason for hiding this comment

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

doInit() could be an abstract method of supertype, so prefix.doInit() could be called

prefixState = prefix.state;
}
if (prefixState == STATE_FIRST_READ || prefixState == STATE_INIT) {
first = prefix.first;
Copy link
Owner

Choose a reason for hiding this comment

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

For prefix.first probably it's ok to perform a type cast...

private static final int STATE_EMPTY = 3;

private ReentrantLock lock;
private Spliterator.OfDouble source;
Copy link
Owner

Choose a reason for hiding this comment

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

source could be a shared field with parameter type, so WithFirstSpliterator is parameterized with <T, SPLTR extends CloneableSpliterator<T>> extends CloneableSpliterator<SPLTR> and source field is declared as SPLTR source.

}

@Override
public Spliterator.OfDouble trySplit() {
Copy link
Owner

Choose a reason for hiding this comment

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

Ideally the whole trySplit() method implementation should be shared in parent abstract class.

@@ -322,6 +322,11 @@ public DoubleStreamEx sorted() {
return new DoubleStreamEx(stream().sorted(), context);
}

public DoubleStreamEx withFirst(DoubleBinaryOperator mapper) {
Copy link
Owner

Choose a reason for hiding this comment

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

JavaDoc is missing for new API methods

@@ -963,6 +963,42 @@ S doClone() {
}
}


static abstract class IntCloneableSpliterator<S extends IntCloneableSpliterator<?>> implements Spliterator.OfInt,
Copy link
Owner

Choose a reason for hiding this comment

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

For me it seems that these three new classes are unnecessary. Looks like CloneableSpliterator can do the thing. No?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants