minor cleanup

This commit is contained in:
djmil 2023-09-21 23:15:57 +02:00
parent 9004e8a408
commit 2a026de472
2 changed files with 8 additions and 35 deletions

View File

@ -73,10 +73,10 @@ public class GameCommand implements Command {
return move; return move;
} }
/* /*
* Session initiator/respondent * Session initiator/respondent
*/ */
public MemberX500Name getCounterparty(StateAndRef<GameState> gameStateSar) { public MemberX500Name getCounterparty(StateAndRef<GameState> gameStateSar) {
final GameState gameState = gameStateSar.getState().getContractState(); final GameState gameState = gameStateSar.getState().getContractState();
return gameState.getOpponentName(getInitiator(gameState)); return gameState.getOpponentName(getInitiator(gameState));
@ -191,9 +191,7 @@ public class GameCommand implements Command {
requireThat(inActors.containsAll(outActors) && outActors.containsAll(inActors), IN_OUT_PARTICIPANTS); requireThat(inActors.containsAll(outActors) && outActors.containsAll(inActors), IN_OUT_PARTICIPANTS);
final var activePlayerName = getInitiator(inGameBoardState); final var activePlayerName = getInitiator(inGameBoardState);
requireThat(inGameBoardState.getActivePlayerName().compareTo(activePlayerName) == 0, SURRENDER_ACTOR + "expected " +inGameBoardState.getActivePlayerName() +" actual " +activePlayerName);
final var expectedWinnerName = inGameBoardState.getOpponentName(activePlayerName); final var expectedWinnerName = inGameBoardState.getOpponentName(activePlayerName);
requireThat(outGameResultState.getWinnerName().compareTo(expectedWinnerName) == 0, requireThat(outGameResultState.getWinnerName().compareTo(expectedWinnerName) == 0,
"Expected winner "+expectedWinnerName.getCommonName() +", proposed winner " +outGameResultState.getWinnerName().getCommonName()); "Expected winner "+expectedWinnerName.getCommonName() +", proposed winner " +outGameResultState.getWinnerName().getCommonName());
@ -237,7 +235,6 @@ public class GameCommand implements Command {
static final String SURRENDER_INPUT_STATE = "SURRENDER command should have exactly one GameBoardState input state"; static final String SURRENDER_INPUT_STATE = "SURRENDER command should have exactly one GameBoardState input state";
static final String SURRENDER_OUTPUT_STATE = "SURRENDER command should have exactly one GameResultState output state"; static final String SURRENDER_OUTPUT_STATE = "SURRENDER command should have exactly one GameResultState output state";
static final String SURRENDER_ACTOR = "Only active player can SURRENDER";
static final String MOVE_INPUT_STATE = "MOVE command should have exactly one GameBoardState input state"; static final String MOVE_INPUT_STATE = "MOVE command should have exactly one GameBoardState input state";
static final String MOVE_OUTPUT_STATE = "MOVE command should have exactly one GameBoardState output state"; static final String MOVE_OUTPUT_STATE = "MOVE command should have exactly one GameBoardState output state";

View File

@ -11,49 +11,25 @@ public class UtxoLedgerTransactionUtil {
return single(utxoTrx.getCommands(clazz), clazz); return single(utxoTrx.getCommands(clazz), clazz);
} }
// public static <T extends ContractState> StateAndRef<T> getSingleReferenceSar(UtxoLedgerTransaction utxoTrx, Class<T> clazz) {
// return singleSar(utxoTrx.getReferenceStateAndRefs(clazz), clazz);
// }
public static <T extends GameState> T getSingleInputState(UtxoLedgerTransaction utxoTrx, Class<T> clazz) { public static <T extends GameState> T getSingleInputState(UtxoLedgerTransaction utxoTrx, Class<T> clazz) {
return single(utxoTrx.getInputStates(clazz), clazz); return single(utxoTrx.getInputStates(clazz), clazz);
} }
// public static <T extends ContractState> StateAndRef<T> getSingleInputSar(UtxoLedgerTransaction utxoTrx, Class<T> clazz) {
// return singleSar(utxoTrx.getInputStateAndRefs(clazz), clazz);
// }
public static <T extends GameState> T getSingleOutputState(UtxoLedgerTransaction utxoTrx, Class<T> clazz) { public static <T extends GameState> T getSingleOutputState(UtxoLedgerTransaction utxoTrx, Class<T> clazz) {
return single(utxoTrx.getOutputStates(clazz), clazz); return single(utxoTrx.getOutputStates(clazz), clazz);
} }
// public static <T extends Command> Optional<T> getOptionalCommand(UtxoLedgerTransaction utxoTrx, Class<T> clazz) {
// return optional(utxoTrx.getCommands(clazz), clazz);
// }
// public static <T extends ContractState> Optional<T> getOptionalReferenceState(UtxoLedgerTransaction utxoTrx, Class<T> clazz) {
// return optional(utxoTrx.getReferenceStates(clazz), clazz);
// }
// public static <T extends ContractState> Optional<T> getOptionalInputState(UtxoLedgerTransaction utxoTrx, Class<T> clazz) {
// return optional(utxoTrx.getInputStates(clazz), clazz);
// }
// public static <T extends ContractState> Optional<T> getOptionalOutputState(UtxoLedgerTransaction utxoTrx, Class<T> clazz) {
// return optional(utxoTrx.getOutputStates(clazz), clazz);
// }
private static <T> Optional<T> optional(List<T> list, Class<T> clazz) {
return list
.stream()
.reduce((a, b) -> {throw new IllegalStateException(MULTIPLE_INSTANCES_OF +clazz.getName());});
}
private static <T> T single(List<T> list, Class<T> clazz) { private static <T> T single(List<T> list, Class<T> clazz) {
return optional(list, clazz) return optional(list, clazz)
.orElseThrow( () -> new IllegalStateException(NO_INSTANCES_OF +clazz.getName()) ); .orElseThrow( () -> new IllegalStateException(NO_INSTANCES_OF +clazz.getName()) );
} }
private static <T> Optional<T> optional(List<T> list, Class<T> clazz) {
return list
.stream()
.reduce((a, b) -> {throw new IllegalStateException(MULTIPLE_INSTANCES_OF +clazz.getName());});
}
private static String MULTIPLE_INSTANCES_OF = "Multiple instances of "; private static String MULTIPLE_INSTANCES_OF = "Multiple instances of ";
private static String NO_INSTANCES_OF = "No instances of "; private static String NO_INSTANCES_OF = "No instances of ";
} }