
io.molr.commons.domain.dto.MissionStateDto Maven / Gradle / Ivy
package io.molr.commons.domain.dto;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.molr.commons.domain.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.*;
public class MissionStateDto {
public final String result;
public final Map> strandAllowedCommands;
public final Map strandCursorBlockIds;
public final Map strandRunStates;
public final Map> parentToChildrenStrands;
public final Set strands;
public final Map blockResults;
public final Map blockRunStates;
public final Map> blockIdAllowedCommands;
public final Set breakpoints;
public final Set allowedMissionCommands;
private MissionStateDto(String result, Map> strandAllowedCommands, Map strandCursorBlockIds, Map strandRunStates, Set strands, Map> parentToChildrenStrands, Map blockResults, Map blockRunStates, Map> blockIdAllowedCommands, Set breakpoints, Set allowedMissionCommands) {
this.result = result;
this.strandAllowedCommands = strandAllowedCommands;
this.strandCursorBlockIds = strandCursorBlockIds;
this.strandRunStates = strandRunStates;
this.strands = strands;
this.parentToChildrenStrands = parentToChildrenStrands;
this.blockResults = blockResults;
this.blockRunStates = blockRunStates;
this.blockIdAllowedCommands = blockIdAllowedCommands;
this.breakpoints = breakpoints;
this.allowedMissionCommands = allowedMissionCommands;
}
public MissionStateDto() {
this(null, emptyMap(), emptyMap(), emptyMap(), emptySet(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptySet(), emptySet());
}
public static final MissionStateDto from(MissionState missionState) {
Set allStrands = missionState.allStrands();
Set strandDtos = allStrands.stream().map(StrandDto::from).collect(toSet());
Map strandCursors = allStrands.stream()
.filter(s -> missionState.cursorBlockIdIn(s).isPresent())
.collect(toMap(Strand::id, s -> missionState.cursorBlockIdIn(s).get()));
Map runStates = allStrands.stream()
.collect(toMap(Strand::id, s -> missionState.runStateOf(s).name()));
Map> parentToChildrenStrands = new HashMap<>();
Map> allowedCommands = new HashMap<>();
for (Strand strand : allStrands) {
Set commands = missionState.allowedCommandsFor(strand);
if (!commands.isEmpty()) {
allowedCommands.put(strand.id(), commands.stream().map(StrandCommand::name).collect(toSet()));
}
List children = missionState.childrenOf(strand);
if (!children.isEmpty()) {
parentToChildrenStrands.put(strand.id(), children.stream().map((Strand::id)).collect(toList()));
}
}
Map> blockIdAllowedCommands = new HashMap<>();
missionState.blockIdsToAllowedCommands().forEach((id, allowedCmds)->{
blockIdAllowedCommands.put(id, ImmutableSet.copyOf(allowedCmds.stream().map(cmd -> cmd.name()).collect(Collectors.toSet())));
});
Set breakpointBlockIds = missionState.breakpointBlockIds();
Set allowedMissionCommands = missionState.allowedMissionCommands().stream().map(MissionCommand::name).collect(toSet());
return new MissionStateDto(missionState.result().name(), allowedCommands, strandCursors, runStates, strandDtos, parentToChildrenStrands, toNameMap(missionState.blockIdsToResult()), toNameMap(missionState.blockIdsToRunState()), blockIdAllowedCommands, breakpointBlockIds, allowedMissionCommands);
}
private static > Map toNameMap(Map inMap) {
return inMap.entrySet().stream()
.collect(toMap(e -> e.getKey(), e -> e.getValue().name()));
}
public MissionState toMissionState() {
Map idsToStrand = strands.stream().collect(toMap(s -> s.id, StrandDto::toStrand));
MissionState.Builder builder = MissionState.builder(Result.valueOf(result));
Map childrenToParentStrandId = childToParent();
for (StrandDto strandDto : strands) {
Strand strand = strandDto.toStrand();
String cursorBlock = ofNullable(strandCursorBlockIds.get(strandDto.id)).orElse(null);
RunState state = RunState.valueOf(strandRunStates.get(strandDto.id));
Set commandNames = ofNullable(strandAllowedCommands.get(strandDto.id)).orElse(emptySet());
Set commands = commandNames.stream().map(StrandCommand::valueOf).collect(toSet());
String parentStrandId = childrenToParentStrandId.get(strand.id());
Strand parentStrand = parentStrandId == null ? null : idsToStrand.get(parentStrandId);
builder.add(strand, state, cursorBlock, parentStrand, commands);
}
blockResults.entrySet().forEach(e -> builder.blockResult(e.getKey(), Result.valueOf(e.getValue())));
blockRunStates.entrySet().forEach(e -> builder.blockRunState(e.getKey(), RunState.valueOf(e.getValue())));
breakpoints.forEach(breakpoint -> builder.addBreakpoint(breakpoint));
blockIdAllowedCommands.forEach((blockId, commands) ->{
commands.stream().forEach(command -> builder.addAllowedCommand(blockId, BlockCommand.valueOf(command)));
});
allowedMissionCommands.stream().map(MissionCommand::valueOf).forEach(builder::addAllowedCommand);
return builder.build();
}
private ImmutableMap childToParent() {
ImmutableMap.Builder childrenToParentBuilder = ImmutableMap.builder();
for (Map.Entry> entry : this.parentToChildrenStrands.entrySet()) {
for (String child : entry.getValue()) {
String parent = entry.getKey();
childrenToParentBuilder.put(child, parent);
}
}
return childrenToParentBuilder.build();
}
@Override
public String toString() {
return "MissionStateDto{" +
"result='" + result + '\'' +
", strandAllowedCommands=" + strandAllowedCommands +
", strandCursorBlockIds=" + strandCursorBlockIds +
", strandRunStates=" + strandRunStates +
", parentToChildrenStrands=" + parentToChildrenStrands +
", strands=" + strands +
", blockResults=" + blockResults +
", blockRunStates=" + blockRunStates +
", blockIdAllowedCommands=" + blockIdAllowedCommands +
", breakpoints=" + breakpoints +
", allowedMissionCommands=" + allowedMissionCommands +
'}';
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy