43 lines
1.7 KiB
Java
43 lines
1.7 KiB
Java
package com.github.dtschust.zork.parser.strategies;
|
|
|
|
import com.github.dtschust.zork.ZorkCommand;
|
|
import com.github.dtschust.zork.ZorkCondition;
|
|
import com.github.dtschust.zork.ZorkTrigger;
|
|
import com.github.dtschust.zork.ZorkTriggerType;
|
|
import com.github.dtschust.zork.parser.Property;
|
|
import com.github.dtschust.zork.parser.PropertyParseStrategy;
|
|
import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy;
|
|
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class ZorkTriggerParseStrategy implements TriggerPropertyParseStrategy {
|
|
|
|
private final PropertyParseStrategy<ZorkCondition> conditionStrategy;
|
|
|
|
public ZorkTriggerParseStrategy(final PropertyParseStrategy<ZorkCondition> conditionStrategy) {
|
|
this.conditionStrategy = conditionStrategy;
|
|
}
|
|
|
|
@Override
|
|
public ZorkTrigger parseTrigger(final Property source, final Property parent) {
|
|
final String typeString = parent.subPropertyValue("type", "single");
|
|
final ZorkTriggerType type = ZorkTriggerType.fromName(typeString).orElseThrow(() ->
|
|
new IllegalArgumentException(typeString + " is not a valid trigger type"));
|
|
|
|
final List<ZorkCommand> commands = source.subPropertiesByName("command").stream()
|
|
.map(Property::value)
|
|
.map(ZorkCommand::new)
|
|
.collect(Collectors.toList());
|
|
|
|
final List<ZorkCondition> conditions = source.subPropertiesByName("condition").stream()
|
|
.map(conditionStrategy::parse)
|
|
.collect(Collectors.toList());
|
|
|
|
final List<String> prints = source.subPropertyValues("print");
|
|
final List<String> actions = source.subPropertyValues("action");
|
|
|
|
return new ZorkTrigger(type, conditions, commands, prints, actions);
|
|
}
|
|
}
|