diff --git a/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/AbstractProxyResourceList.java b/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/AbstractProxyResourceList.java index 516958e4e..0d78f05ee 100644 --- a/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/AbstractProxyResourceList.java +++ b/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/AbstractProxyResourceList.java @@ -4,7 +4,6 @@ import java.util.Collection; import java.util.Optional; -import java.util.UUID; import org.apiguardian.api.API; @@ -36,11 +35,6 @@ public Optional> get(final T resource) { return delegate.get(resource); } - @Override - public Optional> get(final UUID id) { - return delegate.get(id); - } - @Override public Collection> getAll() { return delegate.getAll(); diff --git a/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceList.java b/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceList.java index e5f677d58..3077e3d7f 100644 --- a/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceList.java +++ b/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceList.java @@ -4,7 +4,6 @@ import java.util.Collection; import java.util.Optional; -import java.util.UUID; import org.apiguardian.api.API; @@ -66,14 +65,6 @@ default Optional> remove(ResourceAmount resour */ Optional> get(T resource); - /** - * Retrieves the resource and its amount from the list, identified by ID. - * - * @param id the id - * @return the resource amount if it's present in the list, otherwise an empty {@link Optional} - */ - Optional> get(UUID id); - /** * Retrieves all resources and their amounts from the list. * diff --git a/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceListImpl.java b/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceListImpl.java index f46e0dcb0..0c90d1275 100644 --- a/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceListImpl.java +++ b/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceListImpl.java @@ -6,7 +6,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; -import java.util.UUID; import org.apiguardian.api.API; @@ -19,8 +18,6 @@ @API(status = API.Status.STABLE, since = "2.0.0-milestone.1.2") public class ResourceListImpl implements ResourceList { private final Map> entries = new HashMap<>(); - private final Map> index = new HashMap<>(); - private final Map, UUID> inverseIndex = new HashMap<>(); @Override public ResourceListOperationResult add(final T resource, final long amount) { @@ -35,19 +32,13 @@ public ResourceListOperationResult add(final T resource, final long amount) { private ResourceListOperationResult addToExisting(final ResourceAmount resourceAmount, final long amount) { resourceAmount.increment(amount); - return new ResourceListOperationResult<>(resourceAmount, amount, inverseIndex.get(resourceAmount), true); + return new ResourceListOperationResult<>(resourceAmount, amount, true); } private ResourceListOperationResult addNew(final T resource, final long amount) { final ResourceAmount resourceAmount = new ResourceAmount<>(resource, amount); - - final UUID id = UUID.randomUUID(); - - index.put(id, resourceAmount); - inverseIndex.put(resourceAmount, id); entries.put(resource, resourceAmount); - - return new ResourceListOperationResult<>(resourceAmount, amount, id, true); + return new ResourceListOperationResult<>(resourceAmount, amount, true); } @Override @@ -56,12 +47,10 @@ public Optional> remove(final T resource, final l final ResourceAmount existing = entries.get(resource); if (existing != null) { - final UUID id = inverseIndex.get(existing); - if (existing.getAmount() - amount <= 0) { - return removeCompletely(existing, id); + return removeCompletely(existing); } else { - return removePartly(amount, existing, id); + return removePartly(amount, existing); } } @@ -69,23 +58,18 @@ public Optional> remove(final T resource, final l } private Optional> removePartly(final long amount, - final ResourceAmount resourceAmount, - final UUID id) { + final ResourceAmount resourceAmount) { resourceAmount.decrement(amount); - return Optional.of(new ResourceListOperationResult<>(resourceAmount, -amount, id, true)); + return Optional.of(new ResourceListOperationResult<>(resourceAmount, -amount, true)); } - private Optional> removeCompletely(final ResourceAmount resourceAmount, - final UUID id) { - index.remove(id); - inverseIndex.remove(resourceAmount); + private Optional> removeCompletely(final ResourceAmount resourceAmount) { entries.remove(resourceAmount.getResource()); return Optional.of(new ResourceListOperationResult<>( resourceAmount, -resourceAmount.getAmount(), - id, false )); } @@ -95,11 +79,6 @@ public Optional> get(final T resource) { return Optional.ofNullable(entries.get(resource)); } - @Override - public Optional> get(final UUID id) { - return Optional.ofNullable(index.get(id)); - } - @Override public Collection> getAll() { return entries.values(); @@ -107,7 +86,6 @@ public Collection> getAll() { @Override public void clear() { - index.clear(); entries.clear(); } } diff --git a/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceListOperationResult.java b/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceListOperationResult.java index 1cc3421ce..c6aa484a9 100644 --- a/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceListOperationResult.java +++ b/refinedstorage2-resource-api/src/main/java/com/refinedmods/refinedstorage2/api/resource/list/ResourceListOperationResult.java @@ -2,8 +2,6 @@ import com.refinedmods.refinedstorage2.api.resource.ResourceAmount; -import java.util.UUID; - import org.apiguardian.api.API; /** @@ -12,12 +10,8 @@ * @param the type of resource * @param resourceAmount the current resource amount in the list * @param change the delta caused by the operation - * @param id the id of the resource in the list * @param available whether this resource is still available in the list, or if it was removed */ @API(status = API.Status.STABLE, since = "2.0.0-milestone.1.2") -public record ResourceListOperationResult(ResourceAmount resourceAmount, - long change, - UUID id, - boolean available) { +public record ResourceListOperationResult(ResourceAmount resourceAmount, long change, boolean available) { } diff --git a/refinedstorage2-resource-api/src/test/java/com/refinedmods/refinedstorage2/api/resource/list/AbstractResourceListTest.java b/refinedstorage2-resource-api/src/test/java/com/refinedmods/refinedstorage2/api/resource/list/AbstractResourceListTest.java index 188af2b78..36927cec5 100644 --- a/refinedstorage2-resource-api/src/test/java/com/refinedmods/refinedstorage2/api/resource/list/AbstractResourceListTest.java +++ b/refinedstorage2-resource-api/src/test/java/com/refinedmods/refinedstorage2/api/resource/list/AbstractResourceListTest.java @@ -5,7 +5,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Optional; -import java.util.UUID; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -30,7 +29,6 @@ void shouldAddNewResource() { final ResourceListOperationResult result = list.add("A", 10); // Assert - assertThat(result.id()).isNotNull(); assertThat(result.change()).isEqualTo(10); assertThat(result.resourceAmount().getAmount()).isEqualTo(10); assertThat(result.resourceAmount().getResource()).isEqualTo("A"); @@ -47,7 +45,6 @@ void shouldAddNewResourceWithResourceAmountDirectly() { final ResourceListOperationResult result = list.add(new ResourceAmount<>("A", 10)); // Assert - assertThat(result.id()).isNotNull(); assertThat(result.change()).isEqualTo(10); assertThat(result.resourceAmount().getAmount()).isEqualTo(10); assertThat(result.resourceAmount().getResource()).isEqualTo("A"); @@ -65,13 +62,11 @@ void shouldAddMultipleOfSameResource() { final ResourceListOperationResult result2 = list.add("A", 5); // Assert - assertThat(result1.id()).isNotNull(); assertThat(result1.change()).isEqualTo(10); assertThat(result1.resourceAmount().getAmount()).isEqualTo(15); assertThat(result1.resourceAmount().getResource()).isEqualTo("A"); assertThat(result1.available()).isTrue(); - assertThat(result2.id()).isEqualTo(result1.id()); assertThat(result2.change()).isEqualTo(5); assertThat(result1.resourceAmount().getAmount()).isEqualTo(15); assertThat(result1.resourceAmount().getResource()).isEqualTo("A"); @@ -90,19 +85,16 @@ void shouldAddMultipleOfDifferentResources() { final ResourceListOperationResult result3 = list.add("B", 3); // Assert - assertThat(result1.id()).isNotNull(); assertThat(result1.change()).isEqualTo(10); assertThat(result1.resourceAmount().getAmount()).isEqualTo(15); assertThat(result1.resourceAmount().getResource()).isEqualTo("A"); assertThat(result1.available()).isTrue(); - assertThat(result2.id()).isEqualTo(result1.id()); assertThat(result2.change()).isEqualTo(5); assertThat(result2.resourceAmount().getAmount()).isEqualTo(15); assertThat(result2.resourceAmount().getResource()).isEqualTo("A"); assertThat(result2.available()).isTrue(); - assertThat(result3.id()).isEqualTo(result3.id()); assertThat(result3.change()).isEqualTo(3); assertThat(result3.resourceAmount().getAmount()).isEqualTo(3); assertThat(result3.resourceAmount().getResource()).isEqualTo("B"); @@ -140,7 +132,7 @@ void shouldNotRemoveResourceWhenItIsNotAvailable() { @Test void shouldRemoveResourcePartly() { // Arrange - final ResourceListOperationResult result1 = list.add("A", 20); + list.add("A", 20); list.add("B", 6); // Act @@ -148,7 +140,6 @@ void shouldRemoveResourcePartly() { // Assert assertThat(result2).isPresent(); - assertThat(result2.get().id()).isEqualTo(result1.id()); assertThat(result2.get().change()).isEqualTo(-5); assertThat(result2.get().resourceAmount().getAmount()).isEqualTo(15); assertThat(result2.get().resourceAmount().getResource()).isEqualTo("A"); @@ -163,7 +154,7 @@ void shouldRemoveResourcePartly() { @Test void shouldRemoveResourcePartlyWithResourceAmountDirectly() { // Arrange - final ResourceListOperationResult result1 = list.add("A", 20); + list.add("A", 20); list.add("B", 6); // Act @@ -174,7 +165,6 @@ void shouldRemoveResourcePartlyWithResourceAmountDirectly() { // Assert assertThat(result2).isPresent(); - assertThat(result2.get().id()).isEqualTo(result1.id()); assertThat(result2.get().change()).isEqualTo(-5); assertThat(result2.get().resourceAmount().getAmount()).isEqualTo(15); assertThat(result2.get().resourceAmount().getResource()).isEqualTo("A"); @@ -189,7 +179,7 @@ void shouldRemoveResourcePartlyWithResourceAmountDirectly() { @Test void shouldRemoveResourceCompletely() { // Arrange - final ResourceListOperationResult result1 = list.add("A", 20); + list.add("A", 20); list.add("B", 6); // Act @@ -197,7 +187,6 @@ void shouldRemoveResourceCompletely() { // Assert assertThat(result2).isPresent(); - assertThat(result2.get().id()).isEqualTo(result1.id()); assertThat(result2.get().change()).isEqualTo(-20); assertThat(result2.get().resourceAmount().getAmount()).isEqualTo(20); assertThat(result2.get().resourceAmount().getResource()).isEqualTo("A"); @@ -211,7 +200,7 @@ void shouldRemoveResourceCompletely() { @Test void shouldRemoveResourceCompletelyWithResourceAmountDirectly() { // Arrange - final ResourceListOperationResult result1 = list.add("A", 20); + list.add("A", 20); list.add("B", 6); // Act @@ -222,7 +211,6 @@ void shouldRemoveResourceCompletelyWithResourceAmountDirectly() { // Assert assertThat(result2).isPresent(); - assertThat(result2.get().id()).isEqualTo(result1.id()); assertThat(result2.get().change()).isEqualTo(-20); assertThat(result2.get().resourceAmount().getAmount()).isEqualTo(20); assertThat(result2.get().resourceAmount().getResource()).isEqualTo("A"); @@ -236,7 +224,7 @@ void shouldRemoveResourceCompletelyWithResourceAmountDirectly() { @Test void shouldNotRemoveResourceWithMoreThanIsAvailable() { // Arrange - final ResourceListOperationResult result1 = list.add("A", 20); + list.add("A", 20); list.add("B", 6); // Act @@ -244,7 +232,6 @@ void shouldNotRemoveResourceWithMoreThanIsAvailable() { // Assert assertThat(result2).isPresent(); - assertThat(result2.get().id()).isEqualTo(result1.id()); assertThat(result2.get().change()).isEqualTo(-20); assertThat(result2.get().resourceAmount().getAmount()).isEqualTo(20); assertThat(result2.get().resourceAmount().getResource()).isEqualTo("A"); @@ -283,35 +270,6 @@ void shouldBeAbleToRetrieveByResourceAfterAdding() { assertThat(resourceAmount.get().getAmount()).isEqualTo(6); } - @Test - void shouldBeAbleToRetrieveByIdAfterAdding() { - // Arrange - final ResourceListOperationResult result = list.add("A", 3); - - // Act - final Optional> resourceAmount = list.get(result.id()); - - // Assert - assertThat(resourceAmount).isPresent(); - assertThat(resourceAmount.get().getResource()).isEqualTo("A"); - assertThat(resourceAmount.get().getAmount()).isEqualTo(3); - } - - @Test - void shouldStillBeAbleToRetrieveByIdWhenRemovingPartly() { - // Arrange - final ResourceListOperationResult result = list.add("A", 10); - list.remove("A", 3); - - // Act - final Optional> resourceAmount = list.get(result.id()); - - // Assert - assertThat(resourceAmount).isPresent(); - assertThat(resourceAmount.get().getResource()).isEqualTo("A"); - assertThat(resourceAmount.get().getAmount()).isEqualTo(7); - } - @Test void shouldStillBeAbleToRetrieveByResourceWhenRemovingPartly() { // Arrange @@ -340,43 +298,21 @@ void shouldNotBeAbleToRetrieveByResourceWhenRemovingCompletely() { assertThat(resourceAmount).isNotPresent(); } - @Test - void shouldNotBeAbleToRetrieveByIdWhenRemovingCompletely() { - // Arrange - final ResourceListOperationResult result = list.add("A", 10); - list.remove("A", 10); - - // Act - final Optional> resourceAmount = list.get(result.id()); - - // Assert - assertThat(resourceAmount).isNotPresent(); - } - @Test void shouldClearList() { // Arrange - final UUID id1 = list.add("A", 10).id(); - final UUID id2 = list.add("B", 5).id(); + list.add("A", 10); + list.add("B", 5); final Collection> contentsBeforeClear = new ArrayList<>(list.getAll()); - final Optional> aBeforeClear = list.get(id1); - final Optional> bBeforeClear = list.get(id2); // Act list.clear(); // Assert final Collection> contentsAfterClear = list.getAll(); - final Optional> aAfterClear = list.get(id1); - final Optional> bAfterClear = list.get(id2); assertThat(contentsBeforeClear).hasSize(2); - assertThat(aBeforeClear).isPresent(); - assertThat(bBeforeClear).isPresent(); - assertThat(contentsAfterClear).isEmpty(); - assertThat(aAfterClear).isEmpty(); - assertThat(bAfterClear).isEmpty(); } } diff --git a/refinedstorage2-resource-api/src/test/java/com/refinedmods/refinedstorage2/api/resource/list/listenable/ListenableResourceListTest.java b/refinedstorage2-resource-api/src/test/java/com/refinedmods/refinedstorage2/api/resource/list/listenable/ListenableResourceListTest.java index bdbe396eb..c7fc98868 100644 --- a/refinedstorage2-resource-api/src/test/java/com/refinedmods/refinedstorage2/api/resource/list/listenable/ListenableResourceListTest.java +++ b/refinedstorage2-resource-api/src/test/java/com/refinedmods/refinedstorage2/api/resource/list/listenable/ListenableResourceListTest.java @@ -33,7 +33,6 @@ void shouldCallListenerWhenAdding() { final ResourceListOperationResult result = sut.add("A", 10); // Assert - assertThat(result.id()).isNotNull(); assertThat(result.change()).isEqualTo(10); assertThat(result.resourceAmount().getAmount()).isEqualTo(10); assertThat(result.resourceAmount().getResource()).isEqualTo("A"); @@ -47,7 +46,6 @@ void shouldNotCallListenerWhenAddingWithoutListener() { final ResourceListOperationResult result = sut.add("A", 10); // Assert - assertThat(result.id()).isNotNull(); assertThat(result.change()).isEqualTo(10); assertThat(result.resourceAmount().getAmount()).isEqualTo(10); assertThat(result.resourceAmount().getResource()).isEqualTo("A"); @@ -66,7 +64,6 @@ void shouldCallListenerWhenRemoving() { // Assert assertThat(result).isPresent(); - assertThat(result.get().id()).isNotNull(); assertThat(result.get().change()).isEqualTo(-10); assertThat(result.get().resourceAmount().getAmount()).isEqualTo(10); assertThat(result.get().resourceAmount().getResource()).isEqualTo("A"); @@ -84,7 +81,6 @@ void shouldNotCallListenerWhenRemovingWithoutListener() { // Assert assertThat(result).isPresent(); - assertThat(result.get().id()).isNotNull(); assertThat(result.get().change()).isEqualTo(-10); assertThat(result.get().resourceAmount().getAmount()).isEqualTo(10); assertThat(result.get().resourceAmount().getResource()).isEqualTo("A");