1
\$\begingroup\$

This would be for a MUD client, which has elements of a sort of telnet bot.

Here, there's string parsing for a logged file for each line -- starting with the most recent. Once a "trigger" is invoked the parser and actions should, for the sake of simplicity, stop.

I'm considering a records class for the triggers, but for now have the trigger and result in a map as below.

Not terribly concerned about resource efficiency, as this ultimately deals with text files, but more with extensibility and flexibility.

package net.bounceme.dur.files;

import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BotActions {

    private final static Logger log = Logger.getLogger(BotActions.class.getName());

    private Map<String, String> triggers = null;
    private boolean triggered = false;

    private BotActions() {
    }

    public BotActions(Map<String, String> triggers) {
        this.triggers = triggers;
    }

    private void pullTrigger(String line, Map.Entry<String, String> entry) {
        log.info(line);
        log.info(entry.toString());
    }

    private void triggers(String line) {
        Pattern pattern = null;
        Matcher matcher = null;

        Iterator<Map.Entry<String, String>> triggerEntries = triggers.entrySet().iterator();

        while (triggerEntries.hasNext() && !triggered) {
            Map.Entry<String, String> entry = triggerEntries.next();
            pattern = Pattern.compile(entry.getKey());
            matcher = pattern.matcher(line);
            if (matcher.matches()) {
                pullTrigger(line, entry);
                triggered = true;
            }
        }
    }

    public void everyLine(List<String> list) {
        ListIterator listIterator = list.listIterator(list.size());
        while (listIterator.hasPrevious() && !triggered) {
            triggers(listIterator.previous().toString());
        }
    }

}

I'm thinking really the text lines are a stack while the triggers are more a queue, although there's more flexibility on the triggers sequence. But, just because of how the log file is initially parsed, those don't really seem options.

Regardless, interested in all comments.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.