私は異なる情報を収集するJavaの2つのメソッドを持っていますが、それらは両方とも同じプロセスをセットアップして実行し、それから情報を収集します-データの収集はループ内で行われ、インスタンス化する変数もループで使用されます。
Map<Integer, Integer> getResponsesWithCount(int baseCostMultiplier, int reels, int visibleSymbols, String stakeCostUuid, int totalBets) throws InsufficientFundsException {
final int stake = getStake(baseCostMultiplier, stakeCostUuid);
long balance = 10L * stake;
final TestGpasPlatform testGpasPlatform = TestGpasPlatform.create(ryotaAdapter, TestGpasPlatform.DEFAULT_MIN_BET, Math.max(stake, TestGpasPlatform.DEFAULT_MAX_BET), TestGpasPlatform.DEFAULT_MAX_WIN, ImmutableList.of(baseCostMultiplier));
final Map<Integer, Integer> responseCounts = new HashMap<>();
for (int count = 0; count < totalBets; count++) {
final Tuple2<List<Output>, TestGpasPlatform> result = playWithRealRng(baseCostMultiplier, count, reels, visibleSymbols, stakeCostUuid, testGpasPlatform);
// If we run out of balance, re-start, we want to do meaningful spins that trigger features, etc
balance = checkBalance(stake, balance, result._1(), count);
final int byteLength = result._2.getLastResponse().map(s -> s.getBytes().length).orElse(0);
responseCounts.putIfAbsent(byteLength, 0);
responseCounts.put(byteLength, responseCounts.get(byteLength) + 1);
}
return responseCounts;
}
Map<Integer, Integer> getResponsesWithPayouts(int baseCostMultiplier, int reels, int visibleSymbols, String stakeCostUuid, int totalBets) throws InsufficientFundsException{
final int stake = getStake(baseCostMultiplier, stakeCostUuid);
long balance = 10L * stake;
final TestGpasPlatform testGpasPlatform = TestGpasPlatform.create(ryotaAdapter, TestGpasPlatform.DEFAULT_MIN_BET, Math.max(stake, TestGpasPlatform.DEFAULT_MAX_BET), TestGpasPlatform.DEFAULT_MAX_WIN, ImmutableList.of(baseCostMultiplier));
final Map<Integer, Integer> responseCounts = new HashMap<>();
for (int count = 0; count < totalBets; count++) {
final Tuple2<List<Output>, TestGpasPlatform> result = playWithRealRng(baseCostMultiplier, count, reels, visibleSymbols, stakeCostUuid, testGpasPlatform);
// If we run out of balance, re-start, we want to do meaningful spins that trigger features, etc
balance = checkBalance(stake, balance, result._1(), count);
final int byteLength = result._2.getLastResponse().map(s -> s.getBytes().length).orElse(0);
final PlayData playData = result._2.getLastResponse().map(s -> new Gson().fromJson(s, GdkPlayData.class)).orElse(new GdkPlayData());
final java.util.List<SlotsActionData> actionData = playData.findLastPlay().getLastPlayInModeData().getSlotsData().getActions();
final int sumOfPayouts = actionData.stream()
.map(SlotsActionData::getPayouts)
.mapToInt(java.util.List::size)
.sum();
responseCounts.putIfAbsent(byteLength, sumOfPayouts);
}
return responseCounts;
}
これらの各メソッドの最初の6行のコードは完全に重複していますが、これをどのようにクリーンアップする必要があるか、またはどのようにクリーンアップできるかわかりません。
この問題の拡張は、収集されたデータを除いてすべて同じことを行う2つのメソッド呼び出しチェーンがあり、設計が悪いと思ったためにブール演算子を使用してこの機能を分割する代わりに、それを成し遂げるためのメソッドの新しいチェーンを実装しました。これを別の方法で行う必要がありますか?
withCount
以下のように、共通のメソッドを1つ作成してタイプを渡すことができます。
Map<Integer, Integer> getResponses(int baseCostMultiplier, int reels, int visibleSymbols, String stakeCostUuid, int totalBets, boolean withCount) throws InsufficientFundsException {
final int stake = getStake(baseCostMultiplier, stakeCostUuid);
long balance = 10L * stake;
final TestGpasPlatform testGpasPlatform = TestGpasPlatform.create(ryotaAdapter, TestGpasPlatform.DEFAULT_MIN_BET, Math.max(stake, TestGpasPlatform.DEFAULT_MAX_BET), TestGpasPlatform.DEFAULT_MAX_WIN, ImmutableList.of(baseCostMultiplier));
final Map<Integer, Integer> responseCounts = new HashMap<>();
for (int count = 0; count < totalBets; count++) {
final Tuple2<List<Output>, TestGpasPlatform> result = playWithRealRng(baseCostMultiplier, count, reels, visibleSymbols, stakeCostUuid, testGpasPlatform);
// If we run out of balance, re-start, we want to do meaningful spins that trigger features, etc
balance = checkBalance(stake, balance, result._1(), count);
final int byteLength = result._2.getLastResponse().map(s -> s.getBytes().length).orElse(0);
if(withCount) {
responseCounts.putIfAbsent(byteLength, 0);
responseCounts.put(byteLength, responseCounts.get(byteLength) + 1);
}else{
final PlayData playData = result._2.getLastResponse().map(s -> new Gson().fromJson(s, GdkPlayData.class)).orElse(new GdkPlayData());
final java.util.List<SlotsActionData> actionData = playData.findLastPlay().getLastPlayInModeData().getSlotsData().getActions();
final int sumOfPayouts = actionData.stream()
.map(SlotsActionData::getPayouts)
.mapToInt(java.util.List::size)
.sum();
responseCounts.putIfAbsent(byteLength, sumOfPayouts);
}
}
return responseCounts;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加