You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.3 KiB
Java
59 lines
1.3 KiB
Java
package reactivestreams;
|
|
|
|
import java.util.Collection;
|
|
import java.util.HashSet;
|
|
import java.util.concurrent.Flow;
|
|
import java.util.concurrent.SubmissionPublisher;
|
|
|
|
/**
|
|
* Processor consuming a flow of digit Strings and emitting a single Boolean which is true if applying 9 digits are unique.
|
|
*/
|
|
public abstract class DigitBlockChecker extends SubmissionPublisher<Boolean> implements Flow.Processor<String, Boolean> {
|
|
|
|
int currentRow;
|
|
int currentColumn;
|
|
Collection<String> digitsProcessed;
|
|
|
|
@Override
|
|
public void onSubscribe(Flow.Subscription subscription) {
|
|
currentRow = 1;
|
|
currentColumn = 0;
|
|
digitsProcessed = new HashSet<>();
|
|
subscription.request(9*9);
|
|
}
|
|
|
|
@Override
|
|
public void onNext(String digit) {
|
|
currentColumn++;
|
|
if (currentColumn > 9) {
|
|
currentRow++;
|
|
currentColumn = 1;
|
|
}
|
|
if (currentDigitApplies(currentRow, currentColumn)) {
|
|
if (digitsProcessed.contains(digit)) {
|
|
submit(false);
|
|
close();
|
|
}
|
|
digitsProcessed.add(digit);
|
|
if (digitsProcessed.size() == 9) {
|
|
submit(true);
|
|
close();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected abstract boolean currentDigitApplies(int currentRow, int currentColumn);
|
|
|
|
@Override
|
|
public void onError(Throwable throwable) {
|
|
System.err.println(throwable.fillInStackTrace());
|
|
close();
|
|
}
|
|
|
|
@Override
|
|
public void onComplete() {
|
|
close();
|
|
}
|
|
|
|
}
|