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

com.github.siwenyan.potluck.GuestSkip Maven / Gradle / Ivy

package com.github.siwenyan.potluck;

import com.github.siwenyan.common.*;
import com.github.siwenyan.dish_parser.*;

public class GuestSkip extends AbstractSimpleGuest {
    private static final String FLAVOR_SKIP = "skip";
    private static final String FLAVOR_SKIP_MARK = "skipMark";
    private static final String KEY_BY_MARK = "byMark";

    @Override
    public Feedback have(IDish dish) {
        String flavor = dish.getFlavor().getFlavorName();
        if (flavor.equals(FLAVOR_SKIP)) {
            if (!myTable.hasSupply()) {
                return new Feedback(Feedback.EStatus.YAMI, "No command to skip.");
            }

            if (dish.containsElement(KEY_BY_MARK)) {
                return skipByMark(dish);

            } else {
                return skipByCount(dish);
            }
        } else if (flavor.equals(FLAVOR_SKIP_MARK)) {
            return Feedback.yami();
        }
        return Feedback.yaki();
    }

    private Feedback skipByCount(IDish dish) {
        String sSteps = dish.getStringElement(Constants.DEFAULT_KEY);
        int steps = StringTools.parsePositiveInteger(sSteps);
        if (sSteps.isEmpty()) {
            steps = 1;
        }
        if (steps <= 0) {
            return Feedback.yaki("Bad parameter.");
        }

        for (int i = 0; i < steps; i++) {
            if (myTable.hasSupply()) {
                myTable.removeSupply(0);
            } else {
                break;
            }
        }

        return Feedback.yami();
    }

    private Feedback skipByMark(IDish dish) {
        try {
            String byMark = this.dynamic(dish.getStringElement(KEY_BY_MARK)).trim();
            if (byMark.isEmpty()) {
                return Feedback.yaki("Bad parameter.");
            }

            ISupply cloneTableSupply = myTable.cloneSupply();
            int size = cloneTableSupply.size();
            int markIndex = -1;
            for (int i = 0; i < size; i++) {
                ISupply markSupply = cloneTableSupply.getSupply(i);
                Flavor f = markSupply.getFlavor(0);
                String fName = f.getFlavorName().trim();
                if (fName.equals(FLAVOR_SKIP_MARK)) {
                    IDish markDish = myTable.defaultCook(markSupply);
                    String mark = markDish.getStringElement(Constants.DEFAULT_KEY).trim();
                    if (mark.equals(byMark)) {
                        markIndex = i;
                        break;
                    }

                }
            }

            if (markIndex >= 0) {
                for (int i = 0; i <= markIndex; i++) {
                    myTable.removeSupply(0);
                }
                return Feedback.yami();
            } else {
                return new Feedback(Feedback.EStatus.FAIL, "No such mark: " + byMark);
            }
        } catch (BadSmellException e) {
            return new Feedback(Feedback.EStatus.YAKI, e.getMessage());
        }
    }

    public String[] getFlavors() {
        return new String[]{FLAVOR_SKIP, FLAVOR_SKIP_MARK};
    }

    public static void makeSkipMarkSupply(ISupply emptySupply, String mark) {
        try {
            emptySupply.setLines(new String[]{FLAVOR_SKIP_MARK + Constants.OUTPUT_FLAVOR_SEPARATOR
                    + SyntaxSimple.SYMBOL_QUOTE + mark + SyntaxSimple.SYMBOL_QUOTE});
        } catch (BadSmellException e) {
            // do nothing
        }
    }

    public static String makeSkipMarkSupplyLine(String mark) {
        return FLAVOR_SKIP_MARK + Constants.OUTPUT_FLAVOR_SEPARATOR + SyntaxSimple.SYMBOL_QUOTE + mark
                + SyntaxSimple.SYMBOL_QUOTE;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy