-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:skjolber/3d-bin-container-packing…
… into support
- Loading branch information
Showing
41 changed files
with
1,301 additions
and
1,171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
api/src/main/java/com/github/skjolber/packing/api/Packager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 23 additions & 6 deletions
29
...ain/java/com/github/skjolber/packing/deadline/DeadlineCheckPackagerInterruptSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,34 @@ | ||
package com.github.skjolber.packing.deadline; | ||
|
||
public class DeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier { | ||
import java.io.Closeable; | ||
import java.util.concurrent.ScheduledFuture; | ||
|
||
protected final long deadline; | ||
public class DeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier, Runnable, Closeable { | ||
|
||
public DeadlineCheckPackagerInterruptSupplier(long deadline) { | ||
super(); | ||
this.deadline = deadline; | ||
// this is not entirely accurate for multi-threading, but close enough | ||
// (should have been volatile) | ||
protected boolean expired = false; | ||
protected ScheduledFuture<?> future; | ||
|
||
public DeadlineCheckPackagerInterruptSupplier() { | ||
} | ||
|
||
@Override | ||
public boolean getAsBoolean() { | ||
return System.currentTimeMillis() > deadline; | ||
return expired; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
this.expired = true; | ||
} | ||
|
||
public void close() { | ||
future.cancel(true); | ||
} | ||
|
||
public void setFuture(ScheduledFuture<?> future) { | ||
this.future = future; | ||
} | ||
|
||
} |
29 changes: 23 additions & 6 deletions
29
.../com/github/skjolber/packing/deadline/DelegateDeadlineCheckPackagerInterruptSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,38 @@ | ||
package com.github.skjolber.packing.deadline; | ||
|
||
import java.io.Closeable; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.function.BooleanSupplier; | ||
|
||
public class DelegateDeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier { | ||
public class DelegateDeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier, Runnable, Closeable { | ||
|
||
// this is not entirely accurate for multi-threading, but close enough | ||
// (should have been volatile) | ||
protected boolean expired = false; | ||
protected ScheduledFuture<?> future; | ||
protected final BooleanSupplier delegate; | ||
protected final long deadline; | ||
|
||
public DelegateDeadlineCheckPackagerInterruptSupplier(long deadline, BooleanSupplier delegate) { | ||
|
||
public DelegateDeadlineCheckPackagerInterruptSupplier(BooleanSupplier delegate) { | ||
super(); | ||
this.deadline = deadline; | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public boolean getAsBoolean() { | ||
return delegate.getAsBoolean() || System.currentTimeMillis() > deadline; | ||
return expired || delegate.getAsBoolean(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
this.expired = true; | ||
} | ||
|
||
public void close() { | ||
future.cancel(true); | ||
} | ||
|
||
public void setFuture(ScheduledFuture<?> future) { | ||
this.future = future; | ||
} | ||
|
||
} |
34 changes: 0 additions & 34 deletions
34
...m/github/skjolber/packing/deadline/DelegateNthDeadlineCheckPackagerInterruptSupplier.java
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
.../java/com/github/skjolber/packing/deadline/NthDeadlineCheckPackagerInterruptSupplier.java
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
core/src/main/java/com/github/skjolber/packing/deadline/NthPackagerInterruptSupplier.java
This file was deleted.
Oops, something went wrong.
7 changes: 6 additions & 1 deletion
7
core/src/main/java/com/github/skjolber/packing/deadline/PackagerInterruptSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
package com.github.skjolber.packing.deadline; | ||
|
||
import java.io.Closeable; | ||
|
||
@FunctionalInterface | ||
public interface PackagerInterruptSupplier { | ||
public interface PackagerInterruptSupplier extends Closeable { | ||
|
||
/** | ||
* Gets a result. | ||
* | ||
* @return a result | ||
*/ | ||
boolean getAsBoolean(); | ||
|
||
default void close() { | ||
} | ||
} |
45 changes: 32 additions & 13 deletions
45
.../src/main/java/com/github/skjolber/packing/deadline/PackagerInterruptSupplierBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,68 @@ | ||
package com.github.skjolber.packing.deadline; | ||
|
||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.BooleanSupplier; | ||
|
||
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 int checkpointsPerDeadlineCheck = 1; | ||
private BooleanSupplier interrupt = null; | ||
private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor; | ||
|
||
public static PackagerInterruptSupplierBuilder builder() { | ||
return new PackagerInterruptSupplierBuilder(); | ||
} | ||
|
||
public PackagerInterruptSupplierBuilder withDeadline(long deadline, int checkpointsPerDeadlineCheck) { | ||
public PackagerInterruptSupplierBuilder withDeadline(long deadline) { | ||
this.deadline = deadline; | ||
this.checkpointsPerDeadlineCheck = checkpointsPerDeadlineCheck; | ||
return this; | ||
} | ||
|
||
public PackagerInterruptSupplierBuilder withInterrupt(BooleanSupplier interrupt) { | ||
this.interrupt = interrupt; | ||
return this; | ||
} | ||
|
||
public PackagerInterruptSupplierBuilder withScheduledThreadPoolExecutor(ScheduledThreadPoolExecutor executor) { | ||
this.scheduledThreadPoolExecutor = executor; | ||
return this; | ||
} | ||
|
||
public PackagerInterruptSupplier build() { | ||
if(checkpointsPerDeadlineCheck == Integer.MAX_VALUE || checkpointsPerDeadlineCheck == -1 || deadline == Long.MAX_VALUE || deadline == -1L) { | ||
|
||
if(deadline == Long.MAX_VALUE || deadline == -1L) { | ||
// no deadline | ||
if(interrupt != null) { | ||
return new DefaultPackagerInterrupt(interrupt); | ||
} | ||
return NOOP; | ||
return NEGATIVE; | ||
} | ||
|
||
if(checkpointsPerDeadlineCheck == 1) { | ||
if(interrupt == null) { | ||
return new DeadlineCheckPackagerInterruptSupplier(deadline); | ||
} | ||
return new DelegateDeadlineCheckPackagerInterruptSupplier(deadline, interrupt); | ||
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) { | ||
return new NthDeadlineCheckPackagerInterruptSupplier(deadline, checkpointsPerDeadlineCheck); | ||
DeadlineCheckPackagerInterruptSupplier supplier = new DeadlineCheckPackagerInterruptSupplier(); | ||
ScheduledFuture<?> schedule = scheduledThreadPoolExecutor.schedule(supplier, delay, TimeUnit.MILLISECONDS); | ||
supplier.setFuture(schedule); | ||
return supplier; | ||
} | ||
return new DelegateNthDeadlineCheckPackagerInterruptSupplier(deadline, checkpointsPerDeadlineCheck, interrupt); | ||
|
||
DelegateDeadlineCheckPackagerInterruptSupplier supplier = new DelegateDeadlineCheckPackagerInterruptSupplier(interrupt); | ||
ScheduledFuture<?> schedule = scheduledThreadPoolExecutor.schedule(supplier, delay, TimeUnit.MILLISECONDS); | ||
supplier.setFuture(schedule); | ||
return supplier; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...src/main/java/com/github/skjolber/packing/deadline/PositivePackagerInterruptSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.