forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdeographicSpliterator.java
More file actions
59 lines (42 loc) · 1.46 KB
/
IdeographicSpliterator.java
File metadata and controls
59 lines (42 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package modern.challenge;
import java.util.Spliterator;
import java.util.function.Consumer;
public class IdeographicSpliterator implements Spliterator<Character> {
private final String str;
private int position;
public IdeographicSpliterator(String str) {
this.str = str;
}
@Override
public boolean tryAdvance(Consumer<? super Character> c) {
c.accept(str.charAt(position));
position++;
return position < str.length();
}
@Override
public Spliterator<Character> trySplit() {
int remLength = str.length() - position;
if (remLength < 5) { // cannot split under 5 characters
return null;
}
for (int splitPosition = remLength / 2 + position;
splitPosition < str.length(); splitPosition++) {
if (Character.isIdeographic(str.charAt(splitPosition))) {
Spliterator<Character> spliterator
= new IdeographicSpliterator(str.substring(position, splitPosition));
System.out.println("Split successfully at character: " + str.charAt(splitPosition));
position = splitPosition;
return spliterator;
}
}
return null;
}
@Override
public long estimateSize() {
return str.length() - position;
}
@Override
public int characteristics() {
return ORDERED + IMMUTABLE + NONNULL + SIZED + SUBSIZED;
}
}