Skip to content

Commit

Permalink
Add new iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
skjolber committed Sep 8, 2024
1 parent 1fc2fa2 commit 9502bb8
Show file tree
Hide file tree
Showing 18 changed files with 2,713 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ public void setId(String id) {
public void setItems(List<StackableItem> items) {
this.items = items;
}

public int size() {
return items.size();
}

public StackableItem get(int i) {
return items.get(i);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ public DefaultLoadableItemFilter(LoadableItems loadableItems) {

@Override
public void loaded(int index) {
LoadableItem loadableItem = loadableItems.get(index);
loadableItem.decrement();
if(loadableItem.isEmpty()) {
loadableItems.remove(index);
}
// do nothing
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Loadable {
protected final long minVolumeLimit;
protected final long minAreaLimit;

public Loadable(Stackable stackable, List<StackValue> stackValues, int count) {
public Loadable(Stackable stackable, List<StackValue> stackValues) {
this.values = stackValues;
this.stackable = stackable;

Expand Down Expand Up @@ -57,4 +57,8 @@ public long getMinVolumeLimit() {
public Stackable getStackable() {
return stackable;
}

public StackValue getStackValue(int index) {
return values.get(index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ public void decrement() {
public boolean isEmpty() {
return count == 0;
}

public int getIndex() {
return index;
}

public void decrement(int value) {
this.count = this.count - value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.github.skjolber.packing.api.packager;

import java.util.List;

/**
*
* Items which belong together, for example different parts of a single product or order.
*
*/

public class LoadableItemGroup {

private String id;
private List<LoadableItem> items;

public LoadableItemGroup(String id, List<LoadableItem> items) {
super();
this.id = id;
this.items = items;
}

public String getId() {
return id;
}

public List<LoadableItem> getItems() {
return items;
}

public void setId(String id) {
this.id = id;
}

public void setItems(List<LoadableItem> items) {
this.items = items;
}

public int size() {
return items.size();
}

public int loadableItemsCount() {
int count = 0;
for (LoadableItem loadableItem : items) {
count += loadableItem.getCount();
}
return count;
}

public boolean isEmpty() {
for (LoadableItem loadableItem : items) {
if(!loadableItem.isEmpty()) {
return false;
}
}

return true;
}

public void removeEmpty() {
for (int j = 0; j < items.size(); j++) {
LoadableItem loadableItem = items.get(j);

if(loadableItem.isEmpty()) {
items.remove(j);
j--;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.github.skjolber.packing.api.packager;

import java.util.List;

/**
*
* The items which are available for load into some particular container.
*
*/

public interface LoadableItems extends List<LoadableItem>{
public interface LoadableItems {

int size();

LoadableItem get(int index);

void remove(int index, int count);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.github.skjolber.packing.iterator;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import com.github.skjolber.packing.api.Dimension;
import com.github.skjolber.packing.api.StackValue;
import com.github.skjolber.packing.api.Stackable;
import com.github.skjolber.packing.api.StackableItem;
import com.github.skjolber.packing.api.StackableItemGroup;
import com.github.skjolber.packing.api.packager.Loadable;
import com.github.skjolber.packing.api.packager.LoadableItem;
import com.github.skjolber.packing.api.packager.LoadableItemGroup;

/**
* Builder scaffold.
*
* @see <a href=
* "https://www.sitepoint.com/self-types-with-javas-generics/">https://www.sitepoint.com/self-types-with-javas-generics/</a>
*/

public abstract class AbstractLoadableItemGroupIteratorBuilder<B extends AbstractLoadableItemGroupIteratorBuilder<B>> {

protected int maxLoadWeight = -1;
protected Predicate<Stackable> filter;
protected Dimension size;
protected List<StackableItemGroup> stackableItemGroups;

public B withSize(int dx, int dy, int dz) {
this.size = new Dimension(dx, dy, dz);

return (B)this;
}

public B withLoadSize(Dimension dimension) {
this.size = dimension;

return (B)this;
}

public B withFilter(Predicate<Stackable> filter) {
this.filter = filter;

return (B)this;
}

public B withMaxLoadWeight(int maxLoadWeight) {
this.maxLoadWeight = maxLoadWeight;

return (B)this;
}

public B withStackableItemGroups(List<StackableItemGroup> stackableItems) {
this.stackableItemGroups = stackableItems;

return (B)this;
}

protected List<LoadableItemGroup> toMatrix() {
List<LoadableItemGroup> results = new ArrayList<>(stackableItemGroups.size());

int offset = 0;

for (int i = 0; i < stackableItemGroups.size(); i++) {

StackableItemGroup group = stackableItemGroups.get(i);

List<LoadableItem> loadableItems = new ArrayList<>(group.size());
for (int k = 0; k < group.size(); k++) {
StackableItem item = group.get(k);

if(item.getCount() == 0) {
continue;
}

Stackable stackable = item.getStackable();
if(stackable.getWeight() > maxLoadWeight) {
continue;
}

if(stackable.getVolume() > size.getVolume()) {
continue;
}

List<StackValue> boundRotations = stackable.rotations(size);
if(boundRotations == null || boundRotations.isEmpty()) {
continue;
}

if(filter != null && !filter.test(stackable)) {
continue;
}

Loadable loadable = new Loadable(stackable, boundRotations);

loadableItems.add(new LoadableItem(loadable, item.getCount(), offset));

offset++;
}
if(!loadableItems.isEmpty()) {
results.add(new LoadableItemGroup(group.getId(), loadableItems));
}
}
return results;
}

public abstract LoadableItemPermutationRotationIterator build();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.github.skjolber.packing.iterator;

import java.util.List;
import java.util.function.Predicate;

import com.github.skjolber.packing.api.Dimension;
import com.github.skjolber.packing.api.StackValue;
import com.github.skjolber.packing.api.Stackable;
import com.github.skjolber.packing.api.StackableItem;
import com.github.skjolber.packing.api.packager.Loadable;
import com.github.skjolber.packing.api.packager.LoadableItem;
import com.github.skjolber.packing.api.packager.LoadableItems;

/**
* Builder scaffold.
*
* @see <a href=
* "https://www.sitepoint.com/self-types-with-javas-generics/">https://www.sitepoint.com/self-types-with-javas-generics/</a>
*/

public abstract class AbstractLoadableIteratorBuilder<B extends AbstractLoadableIteratorBuilder<B>> {

protected int maxLoadWeight = -1;
protected Predicate<Stackable> filter;
protected Dimension size;
protected List<StackableItem> stackableItems;

public B withSize(int dx, int dy, int dz) {
this.size = new Dimension(dx, dy, dz);

return (B)this;
}

public B withLoadSize(Dimension dimension) {
this.size = dimension;

return (B)this;
}

public B withFilter(Predicate<Stackable> filter) {
this.filter = filter;

return (B)this;
}

public B withMaxLoadWeight(int maxLoadWeight) {
this.maxLoadWeight = maxLoadWeight;

return (B)this;
}

public B withStackableItems(List<StackableItem> stackableItems) {
this.stackableItems = stackableItems;

return (B)this;
}

protected LoadableItem[] toMatrix() {
LoadableItem[] results = new LoadableItem[stackableItems.size()];

for (int i = 0; i < stackableItems.size(); i++) {
StackableItem item = stackableItems.get(i);

if(item.getCount() == 0) {
continue;
}

Stackable stackable = item.getStackable();
if(stackable.getWeight() > maxLoadWeight) {
continue;
}

if(stackable.getVolume() > size.getVolume()) {
continue;
}

List<StackValue> boundRotations = stackable.rotations(size);
if(boundRotations == null || boundRotations.isEmpty()) {
continue;
}

if(filter != null && !filter.test(stackable)) {
continue;
}

Loadable loadable = new Loadable(stackable, boundRotations);

results[i] = new LoadableItem(loadable, item.getCount(), i);
}
return results;
}

public abstract LoadableItemPermutationRotationIterator build();

}
Loading

0 comments on commit 9502bb8

Please sign in to comment.