From 69ba794d57a98fe66082340b762f1fa308cfe6e9 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 24 Jan 2022 22:06:18 +0000 Subject: [PATCH] Fix AbstractBucket partitioning (#85) --- .../lucko/helper/bucket/AbstractBucket.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/helper/src/main/java/me/lucko/helper/bucket/AbstractBucket.java b/helper/src/main/java/me/lucko/helper/bucket/AbstractBucket.java index 4f5d544d..b96c63f1 100644 --- a/helper/src/main/java/me/lucko/helper/bucket/AbstractBucket.java +++ b/helper/src/main/java/me/lucko/helper/bucket/AbstractBucket.java @@ -28,7 +28,6 @@ import com.google.common.collect.ImmutableList; import me.lucko.helper.bucket.partitioning.PartitioningStrategy; -import me.lucko.helper.utils.ImmutableCollectors; import java.util.AbstractSet; import java.util.Collection; @@ -82,14 +81,19 @@ protected AbstractBucket(int size, PartitioningStrategy strategy) { this.size = size; this.content = createSet(); - //noinspection unchecked - Set[] objs = new Set[size]; + ImmutableList.Builder> sets = ImmutableList.builder(); + ImmutableList.Builder> views = ImmutableList.builder(); + for (int i = 0; i < size; i++) { - objs[i] = createSet(); + Set set = createSet(); + sets.add(set); + + SetView view = new SetView(set, i); + views.add(view); } - this.partitions = ImmutableList.copyOf(objs); - this.partitionView = this.partitions.stream().map(SetView::new).collect(ImmutableCollectors.toList()); + this.partitions = sets.build(); + this.partitionView = views.build(); this.partitionCycle = Cycle.of(this.partitionView); } @@ -235,9 +239,9 @@ private final class SetView extends AbstractSet implements BucketPartition private final Set backing; private final int index; - private SetView(Set backing) { + private SetView(Set backing, int index) { this.backing = backing; - this.index = AbstractBucket.this.partitions.indexOf(backing); + this.index = index; } @Override