view src/main/java/christie/datagear/command/CommandBuilder.java @ 213:e486c13d9ea9

add CommandBuilder
author akahori
date Tue, 12 Mar 2019 13:35:06 +0900
parents
children 3bddbfd6ea02
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;
    protected String toDgmName;// for take
    protected String fromDgmName;//for remotetake/reply
    protected int cgmID = -1;// 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 Object _lock = new Object();
    private Boolean sync = false;

    public synchronized CommandBuilder init(CommandType type){
        //lock();
        this.type = type;
        this.key = null;
        this.toDgmName = null;
        this.fromDgmName = "local";
        this.cgmID = -1;
        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 CommandFactory.getCommand(type, this);
    }

    public void lock(){
        synchronized (_lock){
            while(sync){
                try {
                    _lock.wait();
                } catch (InterruptedException e) {
                }
            }
        }
        sync = true;
    }

    public void unLock(){
        synchronized (_lock){
            _lock.notify();
            sync = false;
        }
    }

    private static class CommandFactory{

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

}