All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cucumber.gherkin.AstNode Maven / Gradle / Ivy

There is a newer version: 30.0.4
Show newest version
package io.cucumber.gherkin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.cucumber.gherkin.Parser.RuleType;
import static io.cucumber.gherkin.Parser.TokenType;

class AstNode {
    private final Map> subItems = new HashMap>();
    public final RuleType ruleType;

    public AstNode(RuleType ruleType) {
        this.ruleType = ruleType;
    }

    public void add(RuleType ruleType, Object obj) {
        List items = subItems.computeIfAbsent(ruleType, k -> new ArrayList<>());
        items.add(obj);
    }

    public  T getSingle(RuleType ruleType, T defaultResult) {
        List items = getItems(ruleType);
        return (T) (items.isEmpty() ? defaultResult : items.get(0));
    }

    public  List getItems(RuleType ruleType) {
        List items = (List) subItems.get(ruleType);
        if (items == null) {
            return Collections.emptyList();
        }
        return items;
    }

    public Token getToken(TokenType tokenType) {
        RuleType ruleType = RuleType.cast(tokenType);
        return getSingle(ruleType, new Token(null, null));
    }

    public List getTokens(TokenType tokenType) {
        return getItems(RuleType.cast(tokenType));
    }
}