1 /* 2 * Copyright (C) 2019, HuntLabs 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 module hunt.database.base.impl.command.CommandBase; 19 20 import hunt.database.base.impl.command.CommandResponse; 21 import hunt.logging; 22 23 24 interface ICommand { 25 26 bool handlerExist(); 27 28 void fail(Throwable err); 29 30 void notifyResponse(ICommandResponse response); 31 } 32 33 /** 34 * @author <a href="mailto:julien@julienviet.com">Julien Viet</a> 35 */ 36 abstract class CommandBase(R) : ICommand { 37 38 ResponseHandler!R handler; 39 40 final bool handlerExist() { return handler !is null; } 41 42 final void fail(Throwable err) { 43 handler(failedResponse!R(err)); 44 } 45 46 void notifyResponse(ICommandResponse response) { 47 if(handler !is null) { 48 CommandResponse!(R) r = cast(CommandResponse!(R))response; 49 if(r is null) { 50 warningf("Can't cast %s", (cast(Object)response).toString()); 51 } else { 52 handler(r); 53 } 54 } 55 } 56 }