|
|
|
@ -20,12 +20,12 @@ public class SudokuVerifier implements Flow.Subscriber<Boolean> {
|
|
|
|
|
digitPublisher = new SubmissionPublisher<>();
|
|
|
|
|
// Setup row and column checking subscribers
|
|
|
|
|
IntStream.rangeClosed(1, 9).boxed()
|
|
|
|
|
.forEach(i -> rowCheckerAndColumnChecker(digitPublisher, i));
|
|
|
|
|
.forEach(i -> rowCheckerAndColumnChecker(i));
|
|
|
|
|
// Setup square checking subscribers
|
|
|
|
|
IntStream.rangeClosed(1, 3).boxed()
|
|
|
|
|
.forEach(squareRow -> {
|
|
|
|
|
IntStream.rangeClosed(1, 3).boxed()
|
|
|
|
|
.forEach(squareColumn -> subscribeBlockChecker(digitPublisher, new SquareChecker(squareRow, squareColumn)));
|
|
|
|
|
.forEach(squareColumn -> squareChecker(squareRow, squareColumn));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -34,26 +34,36 @@ public class SudokuVerifier implements Flow.Subscriber<Boolean> {
|
|
|
|
|
digitStream.forEach(digitPublisher::submit);
|
|
|
|
|
digitPublisher.close();
|
|
|
|
|
// Wait for checking subscribers to finish
|
|
|
|
|
while (finished.get() < digitPublisher.getSubscribers().size() && solved.get()) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(5);
|
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
|
System.out.println(ex.fillInStackTrace());
|
|
|
|
|
}
|
|
|
|
|
int subscriberCount = digitPublisher.getSubscribers().size();
|
|
|
|
|
while (true) {
|
|
|
|
|
if (!(finished.get() < subscriberCount && solved.get())) break;
|
|
|
|
|
justAMoment();
|
|
|
|
|
}
|
|
|
|
|
return solved.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void rowCheckerAndColumnChecker(SubmissionPublisher<String> digitPublisher, Integer i) {
|
|
|
|
|
subscribeBlockChecker(digitPublisher, new RowChecker(i));
|
|
|
|
|
subscribeBlockChecker(digitPublisher, new ColumnChecker(i));
|
|
|
|
|
void rowCheckerAndColumnChecker(Integer i) {
|
|
|
|
|
subscribeBlockChecker(new RowChecker(i));
|
|
|
|
|
subscribeBlockChecker(new ColumnChecker(i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void squareChecker(Integer squareRow, Integer squareColumn) {
|
|
|
|
|
subscribeBlockChecker(new SquareChecker(squareRow, squareColumn));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void subscribeBlockChecker(SubmissionPublisher<String> digitPublisher, DigitBlockChecker blockChecker) {
|
|
|
|
|
void subscribeBlockChecker(DigitBlockChecker blockChecker) {
|
|
|
|
|
digitPublisher.subscribe(blockChecker);
|
|
|
|
|
blockChecker.subscribe(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void justAMoment() {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(5);
|
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
|
System.out.println(ex.fillInStackTrace());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSubscribe(Flow.Subscription subscription) {
|
|
|
|
|
subscription.request(1); // 1 Boolean result from the block checker
|
|
|
|
@ -69,8 +79,8 @@ public class SudokuVerifier implements Flow.Subscriber<Boolean> {
|
|
|
|
|
@Override
|
|
|
|
|
public void onError(Throwable throwable) {
|
|
|
|
|
System.err.println(throwable.fillInStackTrace());
|
|
|
|
|
solved.compareAndSet(true, false);
|
|
|
|
|
finished.incrementAndGet();
|
|
|
|
|
System.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|