view datagear/command/CommandBuilder.cs @ 20:3aaa77e12493

update
author riono <e165729@ie.u-ryukyu.ac.jp>
date Tue, 01 Dec 2020 20:23:09 +0900
parents 4a3115ba746d
children
line wrap: on
line source

using System;
using Christie_net.codegear;
using Christie_net.daemon;
using Christie_net.datagear.dg;

namespace Christie_net.datagear.command {
public class CommandBuilder {
    protected internal CommandType type;
    protected internal string key = null;
    protected internal string toDgmName = null; // for take
    protected internal string fromDgmName = "local"; // for remotetake/reply
    protected internal int? cgmID = null; // for local meta
    protected internal CodeGear cg = null; // for localtake
    protected internal DataGear<object> dg = null; // for put/localtake/reply
    protected internal Type clazz = null; // for remote
    protected internal 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 codeGear) {
        this.cg = codeGear;
        return this;
    }

    public CommandBuilder Dg(DataGear<object> dg) {
        this.dg = dg;
        return this;
    }

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

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

    public Command Build() {
        if (type == null) {
            throw new NullReferenceException();
        }
        return factory.GetCommand(type, this);
    }
    
    private class CommandFactory {
        public Command GetCommand(CommandType type, CommandBuilder cb) {
            switch (type) {
                case CommandType.PUT:
                    CheckNull(cb.key, cb.dg);
                    return new PutCommand(cb);
                case CommandType.TAKE:
                    CheckNull(cb.cgmID, cb.cg, cb.toDgmName, cb.key, cb.dg);
                    return new TakeCommand(cb);
                case CommandType.PEEK:
                    CheckNull(cb.cgmID, cb.cg, cb.toDgmName, cb.key, cb.dg);
                    return new PeekCommand(cb);
                case CommandType.REMOTETAKE:
                    if (cb.fromDgmName.Equals("local")) {
                        throw new NullReferenceException();
                    }
                    CheckNull(cb.key, cb.connection, cb.clazz);
                    return new RemoteTakeCommand(cb);
                case CommandType.REMOTEPEEK:
                    if (cb.fromDgmName.Equals("local")) {
                        throw new NullReferenceException();
                    }
                    CheckNull(cb.key, cb.connection, cb.clazz);
                    return new RemotePeedCommand(cb);
                case CommandType.REPLY:
                    CheckNull(cb.key, cb.connection, cb.dg);
                    return new ReplyCommand(cb);
                case CommandType.CLOSE:
                    CheckNull(cb.connection);
                    return new CloseCommand(cb);
                case CommandType.FINISH:
                    return new FinishCommand(cb);
            }
            return null;
        }

        public void CheckNull(params object[] param) {
            foreach (var variable in param) {
                if (variable == null) {
                    throw new NullReferenceException();
                }
            }
        }
    }
    
}
}