Skip to content

Commit 05ca049

Browse files
committed
Add javadocs to SplitInfo, and make the unit tests a bit more specific with what they check
1 parent 2e3a9e1 commit 05ca049

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

src/main/java/mekanism/common/lib/distribution/SplitInfo.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,63 @@
22

33
public abstract class SplitInfo<TYPE extends Number & Comparable<TYPE>> {
44

5+
/**
6+
* Number of targets to split the contents among.
7+
*/
58
protected int toSplitAmong;
9+
/**
10+
* Represents whether the amount per target distribution has changed. This may happen if a target doesn't need as much as we are willing to offer it in the split.
11+
*/
612
public boolean amountPerChanged = false;
13+
/**
14+
* Determines whether the number of targets to split amount should be decreased.
15+
*
16+
* @implNote This is only set to false briefly when handling accepting contents with remainders to allow them to accept some of the contents without being marked as
17+
* fully accounted for.
18+
*/
719
protected boolean decrementTargets = true;
820

921
protected SplitInfo(int totalTargets) {
1022
this.toSplitAmong = totalTargets;
1123
}
1224

25+
/**
26+
* Marks the given amount as being accounted for and "sent". Decrements {@link #getUnsent() how much we have left to send} and increments
27+
* {@link #getTotalSent() how much we have sent}. If {@link #decrementTargets} is true, this also will reduce the number of targets to split among, and recalculate
28+
* how much we can provide each target.
29+
*
30+
* @param amountNeeded Amount needed by the target and that we are accounting as having been sent to the target.
31+
*/
1332
public abstract void send(TYPE amountNeeded);
1433

34+
/**
35+
* {@return the "share" each target should get when distributing in an even split}
36+
*/
1537
public abstract TYPE getShareAmount();
1638

39+
/**
40+
* Gets the "share" including a potential remainder that targets should get when handling remainders. This is used for actually sending providing the split share to
41+
* any targets that can accept more than we are able to offer in an even split. In general this number will either be equal to {@link #getShareAmount()} or greater
42+
* than it by one while we still have an excess remainder.
43+
*
44+
* @return the "share" plus any potential remainder.
45+
*/
1746
public abstract TYPE getRemainderAmount();
1847

48+
/**
49+
* {@return the amount of contents that has not been sent anywhere yet}
50+
*/
1951
public abstract TYPE getUnsent();
2052

53+
/**
54+
* {@return true if the value is equal to zero}
55+
*
56+
* @param value Value to check
57+
*/
2158
public abstract boolean isZero(TYPE value);
2259

60+
/**
61+
* {@return the total amount of contents that have been sent}
62+
*/
2363
public abstract TYPE getTotalSent();
2464
}

src/main/java/mekanism/common/lib/distribution/Target.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public void sendRemainingSplit(SplitInfo<TYPE> splitInfo) {
8383
splitInfo.decrementTargets = true;
8484
}
8585
}
86+
//TODO: If we remove buffers maybe we should evaluate not caring if we don't actually send the full excess remainder?
87+
// Given ideally we wouldn't attempting to insert the excess remainder to handlers as a second call to the handler on the same tick
8688
if (!splitInfo.isZero(splitInfo.getUnsent())) {
8789
//If we still have some of a remainder after trying to evenly distribute the remainder just send it to the first target willing to accept it
8890
// This might happen if one of the destinations was only able to accept part of the remaining amount, though in general that case will be

src/test/java/mekanism/common/lib/distribution/DistributionTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,26 @@ void testCorrectRemainder() {
8686
void testCorrectFallbackRemainder() {
8787
int toSend = 9;
8888
IntegerTarget availableAcceptors = new IntegerTarget();
89-
availableAcceptors.addHandler(new SpecificAmountIntegerHandler(8));
90-
availableAcceptors.addHandler(new LyingAmountIntegerHandler(1, 10));
89+
IntegerHandler specificHandler = new SpecificAmountIntegerHandler(8);
90+
IntegerHandler lyingHandler = new LyingAmountIntegerHandler(1, 10);
91+
availableAcceptors.addHandler(specificHandler);
92+
availableAcceptors.addHandler(lyingHandler);
9193
Assertions.assertEquals(toSend, EmitUtils.sendToAcceptors(availableAcceptors, toSend, toSend));
94+
Assertions.assertEquals(1, lyingHandler.getAccepted());
95+
Assertions.assertEquals(8, specificHandler.getAccepted());
9296
}
9397

9498
@Test
9599
@DisplayName("Test to check if the remainder is able to be sent when having to fall back using the reversed order for the handlers")
96100
void testCorrectFallbackRemainderAltOrder() {
97101
int toSend = 9;
98102
IntegerTarget availableAcceptors = new IntegerTarget();
99-
availableAcceptors.addHandler(new LyingAmountIntegerHandler(1, 10));
100-
availableAcceptors.addHandler(new SpecificAmountIntegerHandler(8));
103+
IntegerHandler specificHandler = new SpecificAmountIntegerHandler(8);
104+
IntegerHandler lyingHandler = new LyingAmountIntegerHandler(1, 10);
105+
availableAcceptors.addHandler(lyingHandler);
106+
availableAcceptors.addHandler(specificHandler);
101107
Assertions.assertEquals(toSend, EmitUtils.sendToAcceptors(availableAcceptors, toSend, toSend));
108+
Assertions.assertEquals(1, lyingHandler.getAccepted());
109+
Assertions.assertEquals(8, specificHandler.getAccepted());
102110
}
103111
}

0 commit comments

Comments
 (0)