0
0
Fork 0

small refinements, still flaky

paul
paul 5 years ago
parent ffe5c8d609
commit 7c7590b6f1

@ -12,12 +12,14 @@ public abstract class DigitBlockChecker extends SubmissionPublisher<Boolean> imp
int currentRow; int currentRow;
int currentColumn; int currentColumn;
Flow.Subscription subscription;
Collection<String> digitsProcessed; Collection<String> digitsProcessed;
@Override @Override
public void onSubscribe(Flow.Subscription subscription) { public void onSubscribe(Flow.Subscription subscription) {
currentRow = 1; currentRow = 1;
currentColumn = 0; currentColumn = 0;
this.subscription = subscription;
digitsProcessed = new HashSet<>(); digitsProcessed = new HashSet<>();
subscription.request(9*9); subscription.request(9*9);
} }
@ -32,12 +34,12 @@ public abstract class DigitBlockChecker extends SubmissionPublisher<Boolean> imp
if (currentDigitApplies(currentRow, currentColumn)) { if (currentDigitApplies(currentRow, currentColumn)) {
if (digitsProcessed.contains(digit)) { if (digitsProcessed.contains(digit)) {
submit(false); submit(false);
close(); subscription.cancel();
} }
digitsProcessed.add(digit); digitsProcessed.add(digit);
if (digitsProcessed.size() == 9) { if (digitsProcessed.size() == 9) {
submit(true); submit(true);
close(); subscription.cancel();
} }
} }
} }
@ -47,7 +49,7 @@ public abstract class DigitBlockChecker extends SubmissionPublisher<Boolean> imp
@Override @Override
public void onError(Throwable throwable) { public void onError(Throwable throwable) {
System.err.println(throwable.fillInStackTrace()); System.err.println(throwable.fillInStackTrace());
close(); closeExceptionally(throwable);
} }
@Override @Override

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

Loading…
Cancel
Save