view src/main/java/christie/datagear/command/CommandBuilder.java @ 216:176d0b94c1c5

del debug
author akahori
date Tue, 12 Mar 2019 21:53:40 +0900
parents 3bddbfd6ea02
children e8f6c35e6b69
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();

                }
            }
        }
    }

}