-
Notifications
You must be signed in to change notification settings - Fork 46
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
part1 and part2 done by Gladun Nikita #43
Open
nikitos19
wants to merge
1
commit into
java8-course:master
Choose a base branch
from
nikitos19:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,54 +3,66 @@ | |
import java.util.Spliterator; | ||
import java.util.Spliterators; | ||
import java.util.function.Consumer; | ||
import java.util.function.DoubleConsumer; | ||
|
||
public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSpliterator<IndexedDoublePair> { | ||
|
||
|
||
private final OfDouble inner; | ||
private int currentIndex; | ||
private long currentIndex; | ||
|
||
public ZipWithIndexDoubleSpliterator(OfDouble inner) { | ||
this(0, inner); | ||
} | ||
|
||
private ZipWithIndexDoubleSpliterator(int firstIndex, OfDouble inner) { | ||
private ZipWithIndexDoubleSpliterator(long firstIndex, OfDouble inner) { | ||
super(inner.estimateSize(), inner.characteristics()); | ||
currentIndex = firstIndex; | ||
this.inner = inner; | ||
} | ||
|
||
@Override | ||
public int characteristics() { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
return inner.characteristics(); | ||
} | ||
|
||
@Override | ||
public boolean tryAdvance(Consumer<? super IndexedDoublePair> action) { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
DoubleConsumer consumer = value -> action.accept(new IndexedDoublePair(currentIndex, value)); | ||
if(inner.tryAdvance(consumer)){ | ||
currentIndex++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void forEachRemaining(Consumer<? super IndexedDoublePair> action) { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
DoubleConsumer consumer = value -> action.accept(new IndexedDoublePair(currentIndex,value)); | ||
inner.forEachRemaining(consumer); | ||
currentIndex++; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect. Add test with |
||
} | ||
|
||
@Override | ||
public Spliterator<IndexedDoublePair> trySplit() { | ||
// TODO | ||
// if (inner.hasCharacteristics(???)) { | ||
// use inner.trySplit | ||
// } else | ||
|
||
return super.trySplit(); | ||
if(inner.hasCharacteristics(Spliterator.SUBSIZED)){ | ||
OfDouble ofDouble = inner.trySplit(); | ||
if(ofDouble == null){ | ||
return null; | ||
} | ||
final ZipWithIndexDoubleSpliterator zipWithIndexDoubleSpliterator = | ||
new ZipWithIndexDoubleSpliterator(currentIndex, ofDouble); | ||
currentIndex += ofDouble.estimateSize(); | ||
return zipWithIndexDoubleSpliterator; | ||
} | ||
else { | ||
return super.trySplit(); | ||
} | ||
} | ||
|
||
@Override | ||
public long estimateSize() { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
return inner.estimateSize(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package part1; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import spliterators.part1.exercise.RectangleSpliterator; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.StreamSupport; | ||
|
||
/** | ||
* Created by nikita on 4/11/2017. | ||
*/ | ||
public class RectangleSpliteratorTest { | ||
|
||
private int[][] getRandomArray(final int outerLength, final int innerLenght) { | ||
final int[][] result = new int[outerLength][innerLenght]; | ||
|
||
for (int i = 0; i < outerLength; i++) { | ||
for(int j = 0; j< innerLenght; j++){ | ||
result[i][j] = j; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@Test | ||
public void trySplit(){ | ||
final int[][] array = getRandomArray(2,4); | ||
final long actual = StreamSupport.stream(new RectangleSpliterator(array), true).count(); | ||
final long expected = Arrays.stream(array).parallel().flatMapToInt(Arrays::stream).count(); | ||
final long actual2 = StreamSupport.stream(new RectangleSpliterator(array), true).skip(5).count(); | ||
|
||
Assert.assertEquals(expected, actual); | ||
Assert.assertEquals(3, actual2); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/test/java/part2/ZipWithIndexDoubleSpliteratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package part2; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import spliterators.part2.exercise.IndexedDoublePair; | ||
import spliterators.part2.exercise.ZipWithIndexDoubleSpliterator; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Spliterator; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
/** | ||
* Created by nikita on 4/13/2017. | ||
*/ | ||
public class ZipWithIndexDoubleSpliteratorTest { | ||
|
||
final double[] array = {1.4, 2.2, 4.9, 5.3, 8.7, 9.1}; | ||
|
||
@Test | ||
public void testCount(){ | ||
Spliterator.OfDouble spliterator = Arrays.spliterator(array); | ||
|
||
final long actual = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(spliterator), true).count(); | ||
|
||
Assert.assertEquals(6, actual); | ||
} | ||
|
||
@Test | ||
public void tetsToList(){ | ||
final List<Double> expected = Arrays.asList(1.4, 2.2, 4.9, 5.3, 8.7, 9.1); | ||
final List<Double> actual = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(Arrays.spliterator(array)), true) | ||
.map(IndexedDoublePair::getValue) | ||
.collect(Collectors.toList()); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
public void testIndexedDoublePair(){ | ||
final List<IndexedDoublePair> expected = Arrays.asList( | ||
new IndexedDoublePair(0, 1.4), | ||
new IndexedDoublePair(1, 2.2), | ||
new IndexedDoublePair(2, 4.9), | ||
new IndexedDoublePair(3, 5.3), | ||
new IndexedDoublePair(4, 8.7), | ||
new IndexedDoublePair(5, 9.1) | ||
); | ||
|
||
List<IndexedDoublePair> actual = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(Arrays.spliterator(array)), true) | ||
.collect(Collectors.toList()); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Move this check to the beginning of the method to reduce nesting.