view src/main/java/christie/datagear/command/CommandBuilder.java @ 251:e8f6c35e6b69

minor fix
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 25 Jan 2020 14:45:25 +0900
parents 176d0b94c1c5
children be7063c0684b
line wrap: on
line source

package christie.datagear.command;

import christie.codegear.CodeGear;
import christie.daemon.Connection;
import christie.datagear.dg.DataGear;

public class CommandBuilder {
    protected CommandType type; // need
    protected String key = null;
    protected String toDgmName = null;// for take
    protected String fromDgmName = "local";//for remotetake/reply
    protected Integer cgmID = null;// for local meta
    protected CodeGear cg = null;//for localtake
    protected DataGear dg = null;//for put/localtake/reply
    protected Class clazz = null;// for remote
    protected Connection connection = null;//for reply

    private CommandFactory factory = new CommandFactory();

    public CommandBuilder init(CommandType type){
        this.type = type;
        this.key = null;
        this.toDgmName = null;
        this.fromDgmName = "local";
        this.cgmID = null;
        this.cg = null;
        this.dg = null;
        this.clazz = null;
        this.connection = null;
        return this;
    }


    public CommandBuilder key(String key){
        this.key = key;
        return this;
    }

    public CommandBuilder toDgmName(String toDgmName){
        this.toDgmName = toDgmName;
        return this;
    }

    public CommandBuilder fromDgmName(String fromDgmName){
        this.fromDgmName = fromDgmName;
        return this;
    }

    public CommandBuilder cgmID(int cgmID){
        this.cgmID = cgmID;
        return this;
    }

    public CommandBuilder cg(CodeGear cg){
        this.cg = cg;
        return this;
    }

    public CommandBuilder dg(DataGear dg){
        this.dg = dg;
        return this;
    }

    public CommandBuilder clazz(Class clazz){
        this.clazz = clazz;
        return this;
    }

    public CommandBuilder connection(Connection connection){
        this.connection = connection;
        return this;
    }

    public Command build() {
        if(type == null) throw new NullPointerException();
        return factory.getCommand(type, this);
    }

    private class CommandFactory {

        public Command getCommand(CommandType type, CommandBuilder cb) {
            switch (type) {
                case PUT:
                    //check need param
                    checkNull(cb.key, cb.dg);
                    return new PutCommand(cb);
                case TAKE:
                    //check need param
                    checkNull(cb.cgmID, cb.cg, cb.toDgmName, cb.key, cb.dg);
                    return new TakeCommand(cb);
                case PEEK:
                    checkNull(cb.cgmID, cb.cg, cb.toDgmName, cb.key, cb.dg);
                    return new PeekCommand(cb);
                case REMOTETAKE:
                    if (cb.fromDgmName.equals("local"))
                        throw new NullPointerException();
                    checkNull(cb.key, cb.connection, cb.clazz);
                    return new RemoteTakeCommand(cb);
                case REMOTEPEEK:
                    if (cb.fromDgmName.equals("local"))
                        throw new NullPointerException();
                    checkNull(cb.key, cb.connection, cb.clazz);
                    return new RemotePeekCommand(cb);
                case REPLY:
                    //if (cb.fromDgmName.equals("local"))
                    //    throw new NullPointerException();
                    checkNull(cb.key, cb.connection, cb.dg);
                    return new ReplyCommand(cb);
                case CLOSE:
                    checkNull(cb.connection);
                    return new CloseCommand(cb);
                case FINISH:
                    return new FinishCommand(cb);
            }
            return null;
        }

        public void checkNull(Object... params) {
            for(Object param: params) {
                if(param == null) {
                    throw new NullPointerException();
                }
            }
        }
    }

}