-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from refinedmods/develop
v2.0.0-milestone.1.4
- Loading branch information
Showing
493 changed files
with
11,762 additions
and
6,023 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...i/src/main/java/com/refinedmods/refinedstorage2/api/core/component/ComponentAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.refinedmods.refinedstorage2.api.core.component; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.4") | ||
public interface ComponentAccessor<C> { | ||
<I extends C> I getComponent(Class<I> componentType); | ||
} |
26 changes: 26 additions & 0 deletions
26
...re-api/src/main/java/com/refinedmods/refinedstorage2/api/core/component/ComponentMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.refinedmods.refinedstorage2.api.core.component; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.4") | ||
public class ComponentMap<C> implements ComponentAccessor<C> { | ||
private final Map<Class<? extends C>, C> map; | ||
|
||
public ComponentMap(LinkedHashMap<Class<? extends C>, C> map) { | ||
this.map = Collections.unmodifiableMap(map); | ||
} | ||
|
||
public Collection<C> getComponents() { | ||
return map.values(); | ||
} | ||
|
||
@Override | ||
public <I extends C> I getComponent(Class<I> componentType) { | ||
return (I) map.get(componentType); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...src/main/java/com/refinedmods/refinedstorage2/api/core/component/ComponentMapFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.refinedmods.refinedstorage2.api.core.component; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.function.Function; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.4") | ||
public class ComponentMapFactory<C, X> { | ||
private final LinkedHashMap<Class<? extends C>, Function<X, C>> factories; | ||
|
||
public ComponentMapFactory() { | ||
this(new LinkedHashMap<>()); | ||
} | ||
|
||
private ComponentMapFactory(LinkedHashMap<Class<? extends C>, Function<X, C>> factories) { | ||
this.factories = factories; | ||
} | ||
|
||
public void addFactory(Class<? extends C> componentType, Function<X, C> factory) { | ||
factories.put(componentType, factory); | ||
} | ||
|
||
public ComponentMapFactory<C, X> copy() { | ||
return new ComponentMapFactory<>(new LinkedHashMap<>(factories)); | ||
} | ||
|
||
public ComponentMap<C> buildComponentMap(X context) { | ||
LinkedHashMap<Class<? extends C>, C> components = new LinkedHashMap<>(); | ||
factories.forEach((componentType, factory) -> components.put(componentType, factory.apply(context))); | ||
return new ComponentMap<>(components); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...test/java/com/refinedmods/refinedstorage2/api/core/component/ComponentMapFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.refinedmods.refinedstorage2.api.core.component; | ||
|
||
import com.refinedmods.refinedstorage2.test.Rs2Test; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@Rs2Test | ||
class ComponentMapFactoryTest { | ||
@Test | ||
void Test_should_register_factory_and_build_map_correctly() { | ||
// Arrange | ||
ComponentMapFactory<TestComponent, String> sut = new ComponentMapFactory<>(); | ||
|
||
sut.addFactory(TestComponent1.class, TestComponent1::new); | ||
sut.addFactory(TestComponent3.class, TestComponent3::new); | ||
|
||
// Act | ||
ComponentMap<TestComponent> map = sut.buildComponentMap("TEST"); | ||
|
||
// Assert | ||
assertThat(map.getComponent(TestComponent1.class).getGivenContext()).isEqualTo("C1 TEST"); | ||
assertThat(map.getComponent(TestComponent2.class)).isNull(); | ||
assertThat(map.getComponent(TestComponent3.class).getGivenContext()).isEqualTo("C3 TEST"); | ||
assertThat(map.getComponents()).usingRecursiveFieldByFieldElementComparator().containsExactly( | ||
new TestComponent1("TEST"), | ||
new TestComponent3("TEST") | ||
); | ||
} | ||
|
||
@Test | ||
void Test_should_copy_correctly() { | ||
// Arrange | ||
ComponentMapFactory<TestComponent, String> original = new ComponentMapFactory<>(); | ||
original.addFactory(TestComponent1.class, TestComponent1::new); | ||
original.addFactory(TestComponent2.class, TestComponent2::new); | ||
|
||
// Act | ||
ComponentMapFactory<TestComponent, String> copied = original.copy(); | ||
copied.addFactory(TestComponent3.class, TestComponent3::new); | ||
|
||
// Assert | ||
assertThat(original.buildComponentMap("original").getComponents()).hasSize(2); | ||
assertThat(copied.buildComponentMap("copied").getComponents()).hasSize(3); | ||
} | ||
|
||
private interface TestComponent { | ||
String getGivenContext(); | ||
} | ||
|
||
private abstract static class TestComponentImpl implements TestComponent { | ||
private final String componentId; | ||
private final String givenContext; | ||
|
||
public TestComponentImpl(String componentId, String givenContext) { | ||
this.componentId = componentId; | ||
this.givenContext = givenContext; | ||
} | ||
|
||
@Override | ||
public String getGivenContext() { | ||
return componentId + " " + givenContext; | ||
} | ||
} | ||
|
||
private static class TestComponent1 extends TestComponentImpl { | ||
public TestComponent1(String givenContext) { | ||
super("C1", givenContext); | ||
} | ||
} | ||
|
||
private static class TestComponent2 extends TestComponentImpl { | ||
public TestComponent2(String givenContext) { | ||
super("C2", givenContext); | ||
} | ||
} | ||
|
||
private static class TestComponent3 extends TestComponentImpl { | ||
public TestComponent3(String givenContext) { | ||
super("C3", givenContext); | ||
} | ||
} | ||
} |
Oops, something went wrong.