Skip to content

Commit

Permalink
refactor: relay in api layer - storage
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed May 10, 2024
1 parent 0d30984 commit 315efda
Show file tree
Hide file tree
Showing 54 changed files with 1,947 additions and 1,100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ public ComponentMapFactory<C, X> copy() {
return new ComponentMapFactory<>(new LinkedHashMap<>(factories));
}

@SuppressWarnings("unchecked")
public ComponentMap<C> buildComponentMap(final X context) {
final Map<Class<? extends C>, C> components = new LinkedHashMap<>();
factories.forEach((componentType, factory) -> components.put(componentType, factory.apply(context)));
factories.forEach((componentType, factory) -> {
final C component = factory.apply(context);
components.put(componentType, component);
});
return new ComponentMap<>(components);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface InjectNetworkStorageChannel {
public @interface InjectNetworkStorageComponent {
String networkId() default "default";
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.refinedmods.refinedstorage2.api.network.node.NetworkNode;
import com.refinedmods.refinedstorage2.api.network.security.SecurityNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.storage.StorageNetworkComponent;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel;
import com.refinedmods.refinedstorage2.network.test.nodefactory.NetworkNodeFactory;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -192,7 +191,7 @@ private void setField(final Object instance, final Field field, final Object val
@Override
public boolean supportsParameter(final ParameterContext parameterContext,
final ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterContext.isAnnotated(InjectNetworkStorageChannel.class)
return parameterContext.isAnnotated(InjectNetworkStorageComponent.class)
|| parameterContext.isAnnotated(InjectNetworkEnergyComponent.class)
|| parameterContext.isAnnotated(InjectNetworkSecurityComponent.class)
|| parameterContext.isAnnotated(InjectNetwork.class);
Expand All @@ -202,8 +201,8 @@ public boolean supportsParameter(final ParameterContext parameterContext,
public Object resolveParameter(final ParameterContext parameterContext,
final ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterContext
.findAnnotation(InjectNetworkStorageChannel.class)
.map(annotation -> (Object) getNetworkStorageChannel(annotation.networkId()))
.findAnnotation(InjectNetworkStorageComponent.class)
.map(annotation -> (Object) getNetworkStorage(annotation.networkId()))
.or(() -> parameterContext
.findAnnotation(InjectNetworkEnergyComponent.class)
.map(annotation -> (Object) getNetworkEnergy(annotation.networkId())))
Expand All @@ -216,7 +215,7 @@ public Object resolveParameter(final ParameterContext parameterContext,
.orElseThrow();
}

private StorageChannel getNetworkStorageChannel(final String networkId) {
private StorageNetworkComponent getNetworkStorage(final String networkId) {
return networkMap.get(networkId).getComponent(StorageNetworkComponent.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.refinedmods.refinedstorage2.api.network.security.SecurityPolicy;
import com.refinedmods.refinedstorage2.api.network.storage.StorageNetworkComponent;
import com.refinedmods.refinedstorage2.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage2.network.test.util.FakePermissions;
import com.refinedmods.refinedstorage2.network.test.fake.FakePermissions;

public final class NetworkTestFixtures {
public static final ComponentMapFactory<NetworkComponent, Network> NETWORK_COMPONENT_MAP_FACTORY =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.refinedmods.refinedstorage2.network.test.util;
package com.refinedmods.refinedstorage2.network.test.fake;

import com.refinedmods.refinedstorage2.api.storage.Actor;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.refinedmods.refinedstorage2.network.test.util;
package com.refinedmods.refinedstorage2.network.test.fake;

import com.refinedmods.refinedstorage2.api.network.security.Permission;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.refinedmods.refinedstorage2.network.test;
package com.refinedmods.refinedstorage2.network.test.fake;

import com.refinedmods.refinedstorage2.api.resource.ResourceKey;

public enum TestResource implements ResourceKey {
public enum FakeResources implements ResourceKey {
A,
A_ALTERNATIVE,
A_ALTERNATIVE2,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.refinedmods.refinedstorage2.network.test.util;
package com.refinedmods.refinedstorage2.network.test.fake;

import com.refinedmods.refinedstorage2.api.network.security.SecurityActor;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@ParametersAreNonnullByDefault
@FieldsAndMethodsAreNonnullByDefault
package com.refinedmods.refinedstorage2.network.test.util;
package com.refinedmods.refinedstorage2.network.test.fake;

import com.refinedmods.refinedstorage2.api.core.FieldsAndMethodsAreNonnullByDefault;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer;
import com.refinedmods.refinedstorage2.api.network.security.SecurityNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.storage.StorageNetworkComponent;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel;
import com.refinedmods.refinedstorage2.network.test.nodefactory.AbstractNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.MultiStorageNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.SimpleNetworkNodeFactory;
Expand Down Expand Up @@ -129,13 +128,13 @@ void shouldAddNetworkNodeToGraph() {
}

@Test
void shouldInjectStorageChannel(
@InjectNetworkStorageChannel(networkId = "a") final StorageChannel storageChannelA,
@InjectNetworkStorageChannel(networkId = "b") final StorageChannel storageChannelB
void shouldInjectNetworkStorageComponent(
@InjectNetworkStorageComponent(networkId = "a") final StorageNetworkComponent networkStorageA,
@InjectNetworkStorageComponent(networkId = "b") final StorageNetworkComponent networkStorageB
) {
// Assert
assertThat(storageChannelA).isSameAs(a.getComponent(StorageNetworkComponent.class));
assertThat(storageChannelB).isSameAs(b.getComponent(StorageNetworkComponent.class));
assertThat(networkStorageA).isSameAs(a.getComponent(StorageNetworkComponent.class));
assertThat(networkStorageB).isSameAs(b.getComponent(StorageNetworkComponent.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import com.refinedmods.refinedstorage2.api.network.impl.node.AbstractNetworkNode;
import com.refinedmods.refinedstorage2.api.network.security.SecurityNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.storage.StorageNetworkComponent;
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.resource.filter.FilterMode;
import com.refinedmods.refinedstorage2.api.storage.AccessMode;

import java.util.HashSet;
import java.util.Set;
import java.util.function.UnaryOperator;
import javax.annotation.Nullable;

public class RelayInputNetworkNode extends AbstractNetworkNode {
Expand Down Expand Up @@ -43,6 +47,15 @@ public void setComponentTypes(final Set<RelayComponentType> componentTypes) {
updateComponents();
}

public void updateComponentType(final RelayComponentType componentType, final boolean enabled) {
if (enabled) {
componentTypes.add(componentType);
} else {
componentTypes.remove(componentType);
}
updateComponents();
}

private void updateComponents() {
if (outputNode == null) {
return;
Expand All @@ -62,6 +75,40 @@ private void updateComponents() {
: null);
}

public void setAccessMode(final AccessMode accessMode) {
if (outputNode != null) {
outputNode.setAccessMode(accessMode);
}
}

public void setPriority(final int priority) {
if (outputNode != null) {
outputNode.setPriority(priority);
}
}

public void setFilters(final Set<ResourceKey> filters) {
if (outputNode != null) {
outputNode.setFilters(filters);
}
}

public void setFilterMode(final FilterMode filterMode) {
if (outputNode != null) {
outputNode.setFilterMode(filterMode);
}
}

public void setFilterNormalizer(final UnaryOperator<ResourceKey> normalizer) {
if (outputNode != null) {
outputNode.setFilterNormalizer(normalizer);
}
}

public boolean hasComponentType(final RelayComponentType componentType) {
return componentTypes.contains(componentType);
}

@Override
public long getEnergyUsage() {
return energyUsage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@
import com.refinedmods.refinedstorage2.api.network.security.SecurityDecisionProvider;
import com.refinedmods.refinedstorage2.api.network.security.SecurityNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.storage.StorageNetworkComponent;

import com.refinedmods.refinedstorage2.api.network.storage.StorageProvider;
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.resource.filter.FilterMode;
import com.refinedmods.refinedstorage2.api.storage.AccessMode;
import com.refinedmods.refinedstorage2.api.storage.Storage;

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

public class RelayOutputNetworkNode extends AbstractNetworkNode
implements EnergyProvider, SecurityDecisionProvider {
implements EnergyProvider, SecurityDecisionProvider, StorageProvider {
private final long energyUsage;
private final RelayOutputStorage storage = new RelayOutputStorage();

@Nullable
private EnergyNetworkComponent energyDelegate;
@Nullable
private SecurityNetworkComponent securityDelegate;
@Nullable
private StorageNetworkComponent storageDelegate;

public RelayOutputNetworkNode(final long energyUsage) {
this.energyUsage = energyUsage;
Expand All @@ -36,12 +42,38 @@ void setSecurityDelegate(@Nullable final SecurityNetworkComponent securityDelega
}

void setStorageDelegate(@Nullable final StorageNetworkComponent storageDelegate) {
this.storageDelegate = storageDelegate;
this.storage.setDelegate(storageDelegate);
}

void setAccessMode(final AccessMode accessMode) {
this.storage.setAccessMode(accessMode);
}

void setPriority(final int priority) {
this.storage.setPriority(priority);
if (network != null) {
network.getComponent(StorageNetworkComponent.class).sortSources();
}
}

void setFilters(final Set<ResourceKey> filters) {
this.storage.setFilters(filters);
}

void setFilterMode(final FilterMode filterMode) {
this.storage.setFilterMode(filterMode);
}

void setFilterNormalizer(final UnaryOperator<ResourceKey> normalizer) {
this.storage.setFilterNormalizer(normalizer);
}

@Override
public long getEnergyUsage() {
return energyUsage;
if (energyDelegate != null || securityDelegate != null || storage.hasDelegate()) {
return energyUsage;
}
return 0;
}

@Override
Expand Down Expand Up @@ -83,4 +115,9 @@ public SecurityDecision isAllowed(final Permission permission, final SecurityAct
public boolean isProviderActive() {
return isActive() && securityDelegate != null;
}

@Override
public Storage getStorage() {
return storage;
}
}
Loading

0 comments on commit 315efda

Please sign in to comment.