Skip to content

Commit

Permalink
Merge pull request #359 from refinedmods/refactor/GH-76/filter-builder
Browse files Browse the repository at this point in the history
refactor: introduce builder for FilterWithFuzzyMode
  • Loading branch information
raoulvdberge authored Mar 31, 2023
2 parents f2af25d + 83d536a commit 596cf60
Show file tree
Hide file tree
Showing 20 changed files with 222 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public long getEnergyUsage() {
return energyUsage;
}

public <T> void setTemplate(@Nullable final TypedTemplate<T> template) {
this.template = template;
public <T> void setFilterTemplate(@Nullable final TypedTemplate<T> filterTemplate) {
this.template = filterTemplate;
}

public DetectorMode getMode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void doWork() {
schedulingMode.execute(templates, transferStrategy, network, actor);
}

public void setTemplates(final List<Object> newTemplates) {
public void setFilterTemplates(final List<Object> newTemplates) {
templates.clear();
templates.addAll(newTemplates);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void setUp(@InjectNetworkStorageChannel final StorageChannel<String> storageChan
@Test
void testWithoutNetwork() {
// Act
sut.setTemplate(new TypedTemplate<>("A", NetworkTestFixtures.STORAGE_CHANNEL_TYPE));
sut.setFilterTemplate(new TypedTemplate<>("A", NetworkTestFixtures.STORAGE_CHANNEL_TYPE));
sut.setNetwork(null);

// Assert
Expand All @@ -55,7 +55,7 @@ void testWithoutNetwork() {
@Test
void testWithoutActiveness() {
// Act
sut.setTemplate(new TypedTemplate<>("A", NetworkTestFixtures.STORAGE_CHANNEL_TYPE));
sut.setFilterTemplate(new TypedTemplate<>("A", NetworkTestFixtures.STORAGE_CHANNEL_TYPE));
sut.setActive(false);

// Assert
Expand All @@ -78,7 +78,7 @@ void testWithoutTemplate() {
@EnumSource(DetectorMode.class)
void testWithTemplateButWithoutResourceInNetwork(final DetectorMode mode) {
// Arrange
sut.setTemplate(new TypedTemplate<>("A", NetworkTestFixtures.STORAGE_CHANNEL_TYPE));
sut.setFilterTemplate(new TypedTemplate<>("A", NetworkTestFixtures.STORAGE_CHANNEL_TYPE));
sut.setMode(mode);

// Act
Expand Down Expand Up @@ -121,7 +121,7 @@ void testModes(final DetectorMode mode,
final boolean expectedActivated,
@InjectNetworkStorageChannel final StorageChannel<String> storageChannel) {
// Arrange
sut.setTemplate(new TypedTemplate<>("A", NetworkTestFixtures.STORAGE_CHANNEL_TYPE));
sut.setFilterTemplate(new TypedTemplate<>("A", NetworkTestFixtures.STORAGE_CHANNEL_TYPE));
sut.setMode(mode);
sut.setAmount(comparisonAmount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void shouldUseFirstSuccessfulStrategy(
createTransferStrategy(destination, 10),
createTransferStrategy(destination, 10)
)));
sut.setTemplates(List.of("A"));
sut.setFilterTemplates(List.of("A"));

// Act
sut.doWork();
Expand Down Expand Up @@ -110,7 +110,7 @@ void shouldNotTransferWithoutSchedulingMode(

final Storage<String> destination = new InMemoryStorageImpl<>();

sut.setTemplates(List.of("A", "B"));
sut.setFilterTemplates(List.of("A", "B"));
sut.setTransferStrategy(createTransferStrategy(destination, 1));
sut.setSchedulingMode(null);

Expand All @@ -134,7 +134,7 @@ void shouldNotTransferWithoutStrategy(@InjectNetworkStorageChannel final Storage

final Storage<String> destination = new InMemoryStorageImpl<>();

sut.setTemplates(List.of("A", "B"));
sut.setFilterTemplates(List.of("A", "B"));

// Act
sut.doWork();
Expand All @@ -158,7 +158,7 @@ void shouldNotTransferIfInactive(@InjectNetworkStorageChannel final StorageChann
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 1);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A", "B"));
sut.setFilterTemplates(List.of("A", "B"));
sut.setActive(false);

// Act
Expand All @@ -183,7 +183,7 @@ void shouldNotTransferWithoutTemplates(@InjectNetworkStorageChannel final Storag
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 1);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of());
sut.setFilterTemplates(List.of());

// Act
sut.doWork();
Expand All @@ -205,7 +205,7 @@ void shouldNotTransferIfNoResourcesAreAvailable(
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 10);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A", "B"));
sut.setFilterTemplates(List.of("A", "B"));

// Act
sut.doWork();
Expand Down Expand Up @@ -233,8 +233,8 @@ void shouldTransferWithLimitedSpaceInDestination(
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 10);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("C"));
sut.setTemplates(List.of("A", "B"));
sut.setFilterTemplates(List.of("C"));
sut.setFilterTemplates(List.of("A", "B"));

// Act & assert
sut.doWork();
Expand Down Expand Up @@ -277,7 +277,7 @@ void shouldNotTransferIfThereIsNoSpaceInTheDestination(
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 5);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A", "B"));
sut.setFilterTemplates(List.of("A", "B"));

// Act
sut.doWork();
Expand Down Expand Up @@ -305,7 +305,7 @@ void shouldTransferSingleResourceEvenIfTransferQuotaHasNotBeenMet(
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 10);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A", "B"));
sut.setFilterTemplates(List.of("A", "B"));

// Act
sut.doWork();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void shouldTransfer(@InjectNetworkStorageChannel final StorageChannel<String> st
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 1);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A"));
sut.setFilterTemplates(List.of("A"));

// Act
sut.doWork();
Expand Down Expand Up @@ -70,7 +70,7 @@ void shouldUseNextResourceIfFirstOneIsNotAvailableInSameCycle(
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 10);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A", "B"));
sut.setFilterTemplates(List.of("A", "B"));

// Act
sut.doWork();
Expand Down Expand Up @@ -104,7 +104,7 @@ public long insert(final String resource, final long amount, final Action action
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 20);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A", "B", "C"));
sut.setFilterTemplates(List.of("A", "B", "C"));

// Act & assert
sut.doWork();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void shouldTransfer(@InjectNetworkStorageChannel final StorageChannel<String> st
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 5);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("B", "A"));
sut.setFilterTemplates(List.of("B", "A"));

// Act & assert
sut.doWork();
Expand Down Expand Up @@ -78,7 +78,7 @@ void shouldUseNextResourceIfFirstOneIsNotAvailableInSameCycle(
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 10);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A", "B"));
sut.setFilterTemplates(List.of("A", "B"));

// Act & assert
sut.doWork();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void shouldTransfer(@InjectNetworkStorageChannel final StorageChannel<String> st
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 5);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A", "B"));
sut.setFilterTemplates(List.of("A", "B"));

// Act & assert
sut.doWork();
Expand Down Expand Up @@ -97,7 +97,7 @@ void shouldNotTransferIfThereAreNoResourcesInSource() {
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 5);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A", "B"));
sut.setFilterTemplates(List.of("A", "B"));

// Act & assert
sut.doWork();
Expand All @@ -117,7 +117,7 @@ void shouldUseNextResourceIfFirstOneIsNotAvailableInSameCycle(
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 10);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A", "B", "C", "D"));
sut.setFilterTemplates(List.of("A", "B", "C", "D"));

// Act & assert
sut.doWork();
Expand Down Expand Up @@ -194,7 +194,7 @@ void shouldResetRoundRobinStateAfterChangingTemplates(
final ExporterTransferStrategy strategy = createTransferStrategy(destination, 5);

sut.setTransferStrategy(strategy);
sut.setTemplates(List.of("A", "B", "C"));
sut.setFilterTemplates(List.of("A", "B", "C"));

// Act & assert
sut.doWork();
Expand All @@ -221,7 +221,7 @@ void shouldResetRoundRobinStateAfterChangingTemplates(
);

// Now C would be the next one, but we expect to go back to A.
sut.setTemplates(List.of("A", "C"));
sut.setFilterTemplates(List.of("A", "C"));
sut.doWork();

assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,46 @@

import com.refinedmods.refinedstorage2.api.storage.TypedTemplate;
import com.refinedmods.refinedstorage2.platform.api.resource.FuzzyModeNormalizer;
import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.filter.FilteredResourceFilterContainer;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.filter.ResourceFilterContainer;

import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import javax.annotation.Nullable;

import net.minecraft.nbt.CompoundTag;

// TODO: Refactor constructors.
public final class FilterWithFuzzyMode {
private static final String TAG_FUZZY_MODE = "fm";
private static final String TAG_RESOURCE_FILTER = "rf";

private final ResourceFilterContainer filterContainer;
@Nullable
private final Runnable listener;
private final Consumer<Set<TypedTemplate<?>>> templatesAcceptor;
private final Consumer<List<TypedTemplate<?>>> orderedTemplatesAcceptor;
@Nullable
private final Consumer<Set<TypedTemplate<?>>> uniqueTemplatesAcceptor;
@Nullable
private final Consumer<List<TypedTemplate<?>>> templatesAcceptor;

private boolean fuzzyMode;

public FilterWithFuzzyMode(final PlatformStorageChannelType<?> storageChannelType,
final Runnable listener,
final Consumer<Set<TypedTemplate<?>>> templatesAcceptor,
final Consumer<List<TypedTemplate<?>>> orderedTemplatesAcceptor) {
this.filterContainer = new FilteredResourceFilterContainer(
9,
this::filterContainerChanged,
storageChannelType
);
public FilterWithFuzzyMode(final ResourceFilterContainer filterContainer,
@Nullable final Runnable listener,
@Nullable final Consumer<Set<TypedTemplate<?>>> uniqueTemplatesAcceptor,
@Nullable final Consumer<List<TypedTemplate<?>>> templatesAcceptor) {
this.filterContainer = filterContainer;
this.listener = listener;
this.uniqueTemplatesAcceptor = uniqueTemplatesAcceptor;
this.templatesAcceptor = templatesAcceptor;
this.orderedTemplatesAcceptor = orderedTemplatesAcceptor;
}

public FilterWithFuzzyMode(final PlatformStorageChannelType<?> storageChannelType,
final Runnable listener,
final Consumer<Set<TypedTemplate<?>>> templatesAcceptor,
final Consumer<List<TypedTemplate<?>>> orderedTemplatesAcceptor,
final int size,
final long maxAmount) {
this.filterContainer = new FilteredResourceFilterContainer(
size,
this::filterContainerChanged,
storageChannelType,
maxAmount
);
this.listener = listener;
this.templatesAcceptor = templatesAcceptor;
this.orderedTemplatesAcceptor = orderedTemplatesAcceptor;
}

public FilterWithFuzzyMode(final Runnable listener,
final Consumer<Set<TypedTemplate<?>>> templatesAcceptor,
final Consumer<List<TypedTemplate<?>>> orderedTemplatesAcceptor) {
this.filterContainer = new ResourceFilterContainer(9, this::filterContainerChanged);
this.listener = listener;
this.templatesAcceptor = templatesAcceptor;
this.orderedTemplatesAcceptor = orderedTemplatesAcceptor;
this.filterContainer.setListener(this::filterContainerChanged);
}

private void filterContainerChanged() {
loadTemplates();
listener.run();
if (listener != null) {
listener.run();
}
}

public ResourceFilterContainer getFilterContainer() {
Expand All @@ -80,8 +54,11 @@ public boolean isFuzzyMode() {

public void setFuzzyMode(final boolean fuzzyMode) {
this.fuzzyMode = fuzzyMode;
loadTemplates(); // we need to reload the templates as the normalizer will give different outputs now.
listener.run();
// We need to reload the templates as the normalizer will give different outputs now.
loadTemplates();
if (listener != null) {
listener.run();
}
}

public void load(final CompoundTag tag) {
Expand All @@ -95,8 +72,12 @@ public void load(final CompoundTag tag) {
}

private void loadTemplates() {
templatesAcceptor.accept(filterContainer.getUniqueTemplates());
orderedTemplatesAcceptor.accept(filterContainer.getTemplates());
if (uniqueTemplatesAcceptor != null) {
uniqueTemplatesAcceptor.accept(filterContainer.getUniqueTemplates());
}
if (templatesAcceptor != null) {
templatesAcceptor.accept(filterContainer.getTemplates());
}
}

public void save(final CompoundTag tag) {
Expand Down
Loading

0 comments on commit 596cf60

Please sign in to comment.