Skip to content

Commit

Permalink
Merge pull request #483 from refinedmods/refactor/GH-482/generics
Browse files Browse the repository at this point in the history
refactor: remove generics for ResourceList and Storage
  • Loading branch information
raoulvdberge authored Mar 9, 2024
2 parents aacba0b + b8844ec commit 1fd63d1
Show file tree
Hide file tree
Showing 424 changed files with 5,968 additions and 6,910 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.refinedmods.refinedstorage2.api.grid.operations;

import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage;
import com.refinedmods.refinedstorage2.api.storage.InsertableStorage;

import org.apiguardian.api.API;

/**
* Grid operations, used for grids to interact with the storage network.
*
* @param <T> the resource type
*/
@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.2")
public interface GridOperations<T> {
public interface GridOperations {
/**
* Tries to move a resource from the network storage to the destination.
* The amount being extracted depends on the extraction mode.
Expand All @@ -20,7 +19,7 @@ public interface GridOperations<T> {
* @param extractMode the extract mode
* @param destination the destination
*/
boolean extract(T resource, GridExtractMode extractMode, InsertableStorage<T> destination);
boolean extract(ResourceKey resource, GridExtractMode extractMode, InsertableStorage destination);

/**
* Tries to move a resource from the source to the network storage.
Expand All @@ -30,5 +29,5 @@ public interface GridOperations<T> {
* @param insertMode the insertion mode
* @param source the source
*/
boolean insert(T resource, GridInsertMode insertMode, ExtractableStorage<T> source);
boolean insert(ResourceKey resource, GridInsertMode insertMode, ExtractableStorage source);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.refinedmods.refinedstorage2.api.grid.operations;

import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage;
import com.refinedmods.refinedstorage2.api.storage.InsertableStorage;
Expand All @@ -12,10 +13,10 @@
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.2")
public class GridOperationsImpl<T> implements GridOperations<T> {
private final StorageChannel<T> storageChannel;
public class GridOperationsImpl implements GridOperations {
private final StorageChannel storageChannel;
private final Actor actor;
private final ToLongFunction<T> maxAmountProvider;
private final ToLongFunction<ResourceKey> maxAmountProvider;
private final long singleAmount;

/**
Expand All @@ -25,9 +26,9 @@ public class GridOperationsImpl<T> implements GridOperations<T> {
* @param singleAmount amount that needs to be extracted when using
* {@link GridInsertMode#SINGLE_RESOURCE} or {@link GridExtractMode#SINGLE_RESOURCE}
*/
public GridOperationsImpl(final StorageChannel<T> storageChannel,
public GridOperationsImpl(final StorageChannel storageChannel,
final Actor actor,
final ToLongFunction<T> maxAmountProvider,
final ToLongFunction<ResourceKey> maxAmountProvider,
final long singleAmount) {
this.storageChannel = storageChannel;
this.actor = actor;
Expand All @@ -36,22 +37,22 @@ public GridOperationsImpl(final StorageChannel<T> storageChannel,
}

@Override
public boolean extract(final T resource,
public boolean extract(final ResourceKey resource,
final GridExtractMode extractMode,
final InsertableStorage<T> destination) {
final InsertableStorage destination) {
final long amount = getExtractableAmount(resource, extractMode);
if (amount == 0) {
return false;
}
return TransferHelper.transfer(resource, amount, actor, storageChannel, destination, storageChannel) > 0;
}

private long getExtractableAmount(final T resource, final GridExtractMode extractMode) {
private long getExtractableAmount(final ResourceKey resource, final GridExtractMode extractMode) {
final long extractableAmount = getExtractableAmount(resource);
return adjustExtractableAmountAccordingToExtractMode(extractMode, extractableAmount);
}

private long getExtractableAmount(final T resource) {
private long getExtractableAmount(final ResourceKey resource) {
final long totalSize = storageChannel.get(resource).map(ResourceAmount::getAmount).orElse(0L);
final long maxAmount = maxAmountProvider.applyAsLong(resource);
return Math.min(totalSize, maxAmount);
Expand All @@ -67,7 +68,9 @@ private long adjustExtractableAmountAccordingToExtractMode(final GridExtractMode
}

@Override
public boolean insert(final T resource, final GridInsertMode insertMode, final ExtractableStorage<T> source) {
public boolean insert(final ResourceKey resource,
final GridInsertMode insertMode,
final ExtractableStorage source) {
final long amount = switch (insertMode) {
case ENTIRE_RESOURCE -> maxAmountProvider.applyAsLong(resource);
case SINGLE_RESOURCE -> singleAmount;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.refinedmods.refinedstorage2.api.grid.operations;

import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage;
import com.refinedmods.refinedstorage2.api.storage.InsertableStorage;

public class NoopGridOperations<T> implements GridOperations<T> {
public class NoopGridOperations implements GridOperations {
@Override
public boolean extract(final T resource,
public boolean extract(final ResourceKey resource,
final GridExtractMode extractMode,
final InsertableStorage<T> destination) {
final InsertableStorage destination) {
return false;
}

@Override
public boolean insert(final T resource,
public boolean insert(final ResourceKey resource,
final GridInsertMode insertMode,
final ExtractableStorage<T> source) {
final ExtractableStorage source) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
@FunctionalInterface
@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.6")
public interface GridResourceFactory {
Optional<GridResource> apply(ResourceAmount<?> resourceAmount);
Optional<GridResource> apply(ResourceAmount resourceAmount);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.refinedmods.refinedstorage2.api.grid.view;

import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedResource;

Expand Down Expand Up @@ -58,11 +59,10 @@ public interface GridView {
void setSortingDirection(GridSortingDirection sortingDirection);

/**
* @param <T> the resource type
* @param resource the resource
* @return the tracked resource, if present
*/
<T> Optional<TrackedResource> getTrackedResource(T resource);
Optional<TrackedResource> getTrackedResource(ResourceKey resource);

/**
* Sorts the view list.
Expand All @@ -74,12 +74,11 @@ public interface GridView {
* Applies a change to a resource. Will update the backing list, and will also update the view list (depending
* if the view is preventing sorting).
*
* @param <T> the resource type
* @param resource the resource
* @param amount the amount, can be negative or positive
* @param trackedResource the tracked resource, can be null
*/
<T> void onChange(T resource, long amount, @Nullable TrackedResource trackedResource);
void onChange(ResourceKey resource, long amount, @Nullable TrackedResource trackedResource);

/**
* @return the view list
Expand All @@ -89,7 +88,7 @@ public interface GridView {
/**
* @return a copy of the backing list
*/
ResourceList<Object> copyBackingList();
ResourceList copyBackingList();

/**
* Clears the backing list, view list and tracked resources index.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.refinedmods.refinedstorage2.api.grid.view;

import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedResource;

import javax.annotation.Nullable;
Expand All @@ -14,13 +15,12 @@ public interface GridViewBuilder {
/**
* Adds a resource in the backing and view list.
*
* @param <T> the resource type
* @param resource the resource
* @param amount the amount
* @param trackedResource the tracked resource, can be null
* @return this builder
*/
<T> GridViewBuilder withResource(T resource, long amount, @Nullable TrackedResource trackedResource);
GridViewBuilder withResource(ResourceKey resource, long amount, @Nullable TrackedResource trackedResource);

/**
* @return a {@link GridView} with the specified resources
Expand Down
Loading

0 comments on commit 1fd63d1

Please sign in to comment.