Skip to content

Commit

Permalink
Add fool-proofed duration check
Browse files Browse the repository at this point in the history
  • Loading branch information
skjolber committed Jul 23, 2024
1 parent 411e159 commit 8e9bb2f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

public class PackagerInterruptSupplierBuilder {

public static final NegativePackagerInterruptSupplier NOOP = new NegativePackagerInterruptSupplier();
public static final NegativePackagerInterruptSupplier NEGATIVE = new NegativePackagerInterruptSupplier();
public static final PositivePackagerInterruptSupplier POSITIVE = new PositivePackagerInterruptSupplier();

private long deadline = Long.MAX_VALUE;
private BooleanSupplier interrupt = null;
Expand All @@ -33,27 +34,33 @@ public PackagerInterruptSupplierBuilder withScheduledThreadPoolExecutor(Schedule
}

public PackagerInterruptSupplier build() {

if(deadline == Long.MAX_VALUE || deadline == -1L) {
// no deadline
if(interrupt != null) {
return new DefaultPackagerInterrupt(interrupt);
}
return NOOP;
return NEGATIVE;
}


long delay = deadline - System.currentTimeMillis();
if(delay <= 0) {
return POSITIVE; // i.e. time is already up
}

if(scheduledThreadPoolExecutor == null) {
throw new IllegalStateException("Expected scheduler");
}

if(interrupt == null) {
DeadlineCheckPackagerInterruptSupplier supplier = new DeadlineCheckPackagerInterruptSupplier();
ScheduledFuture<?> schedule = scheduledThreadPoolExecutor.schedule(supplier, deadline - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
ScheduledFuture<?> schedule = scheduledThreadPoolExecutor.schedule(supplier, delay, TimeUnit.MILLISECONDS);
supplier.setFuture(schedule);
return supplier;
}

DelegateDeadlineCheckPackagerInterruptSupplier supplier = new DelegateDeadlineCheckPackagerInterruptSupplier(interrupt);
ScheduledFuture<?> schedule = scheduledThreadPoolExecutor.schedule(supplier, deadline - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
ScheduledFuture<?> schedule = scheduledThreadPoolExecutor.schedule(supplier, delay, TimeUnit.MILLISECONDS);
supplier.setFuture(schedule);
return supplier;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.skjolber.packing.deadline;

public class PositivePackagerInterruptSupplier implements PackagerInterruptSupplier {

@Override
public boolean getAsBoolean() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected P packSingle(List<Integer> containerItemIndexes, PackagerAdapter<P> ad
}

public List<Container> packList(List<StackableItem> products, List<ContainerItem> containers, int limit) {
return pack(products, containers, limit, PackagerInterruptSupplierBuilder.NOOP);
return pack(products, containers, limit, PackagerInterruptSupplierBuilder.NEGATIVE);
}

/**
Expand Down

0 comments on commit 8e9bb2f

Please sign in to comment.