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

com.firefly.net.tcp.codec.AbstractMessageHandler Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.net.tcp.codec;

import com.firefly.utils.function.Action1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Pengtao Qiu
 */
abstract public class AbstractMessageHandler implements MessageHandler{

    private static Logger log = LoggerFactory.getLogger("firefly-system");

    protected Action1 action;
    protected Action1 exception;

    @Override
    public void receive(R obj) {
        check();
        parse(obj);
    }

    @Override
    public MessageHandler complete(Action1 action) {
        this.action = action;
        return this;
    }

    @Override
    public MessageHandler exception(Action1 exception) {
        this.exception = exception;
        return this;
    }

    abstract protected void parse(R obj);

    private void check() {
        if (this.action == null) {
            throw new IllegalArgumentException("the complete callback is null");
        }
        if (this.exception == null) {
            exception = (t) -> log.error("parsing exception", t);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy