package Ġcom . hid dew ier ing a . elev ator . domain . elev ator . model Ċ Ċ import Ġcom . hid dew ier ing a . elev ator . domain . elev ator . Assign E lev ator Target Ċ import Ġcom . hid dew ier ing a . elev ator . domain . elev ator . Close Do ors Ċ import Ġcom . hid dew ier ing a . elev ator . domain . elev ator . Create E lev ator Ċ import Ġcom . hid dew ier ing a . elev ator . domain . elev ator . E lev ator Created Ċ import Ġcom . hid dew ier ing a . elev ator . domain . elev ator . E lev ator Do ors Closed Ċ import Ġcom . hid dew ier ing a . elev ator . domain . elev ator . E lev ator Do ors Opened Ċ import Ġcom . hid dew ier ing a . elev ator . domain . elev ator . E lev ator Moved To Floor Ċ import Ġcom . hid dew ier ing a . elev ator . domain . elev ator . E lev ator Target Assigned Ċ import Ġcom . hid dew ier ing a . elev ator . domain . elev ator . E lev ator Target Removed Ċ import Ġcom . hid dew ier ing a . elev ator . domain . elev ator . Open Do ors Ċ import Ġcom . hid dew ier ing a . elev ator . domain . elev ator . Remove E lev ator Target Ċ import Ġorg . ax on framework . command handling . CommandHandler Ċ import Ġorg . ax on framework . events ourcing . Event S ourcing Handler Ċ import Ġorg . ax on framework . mode lling . command . Aggregate Identifier Ċ import Ġorg . ax on framework . mode lling . command . Aggregate Lifecycle . apply Ċ import Ġorg . ax on framework . spring . stereotype . Aggregate Ċ import Ġorg . slf 4 j . LoggerFactory Ċ Ċ private Ġval Ġlogger Ġ= ĠLoggerFactory . getLogger ( E lev ator Aggregate :: class . java ) Ċ Ċ @ Aggregate Ċ class ĠE lev ator Aggregate Ġ{ ĊĊĠĠĠ Ġ@ Aggregate Identifier ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġid : ĠE lev ator Id ĊĠĠĠ Ġprivate Ġlateinit Ġvar ĠgroupId : ĠE lev ator GroupId ĊĊĠĠĠ Ġprivate Ġvar Ġfloor Ġ= Ġ 0 L ĊĠĠĠ Ġprivate Ġvar Ġdo ors Opened Ġ= Ġfalse ĊĠĠĠ Ġprivate Ġvar Ġtargets Ġ= Ġmutable Set Of < Long >() ĊĊĠĠĠ Ġconstructor () ĊĊĠĠĠ Ġ@ CommandHandler ĊĠĠĠ Ġconstructor ( command : ĠCreate E lev ator ) Ġ{ ĊĠĠĠĠĠĠĠ Ġapply ( E lev ator Created ( command . elev ator Id , Ġcommand . groupId , Ġcommand . init al Floor , Ġcommand . initial Direction )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Event S ourcing Handler ĊĠĠĠ Ġfun Ġhandle ( event : ĠE lev ator Created ) Ġ{ ĊĠĠĠĠĠĠĠ Ġthis . id Ġ= Ġevent . elev ator Id ĊĠĠĠĠĠĠĠ Ġthis . groupId Ġ= Ġevent . groupId ĊĠĠĠĠĠĠĠ Ġthis . floor Ġ= Ġevent . initial Floor ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ CommandHandler ĊĠĠĠ Ġfun Ġhandle ( command : ĠOpen Do ors ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( do ors Opened ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlogger . warn (" Do ors Ġcannot Ġbe Ġopened Ġfor Ġ${ id . id }, Ġalready Ġopen ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġapply ( E lev ator Do ors Opened ( command . elev ator Id )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Event S ourcing Handler ĊĠĠĠ Ġfun Ġhandle ( event : ĠE lev ator Do ors Opened ) Ġ{ ĊĠĠĠĠĠĠĠ Ġdo ors Opened Ġ= Ġtrue ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ CommandHandler ĊĠĠĠ Ġfun Ġhandle ( command : ĠClose Do ors ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ(! do ors Opened ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlogger . warn (" Do ors Ġcannot Ġbe Ġclosed Ġfor Ġ${ id . id }, Ġalready Ġclosed ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġapply ( E lev ator Do ors Closed ( command . elev ator Id )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Event S ourcing Handler ĊĠĠĠ Ġfun Ġhandle ( event : ĠE lev ator Do ors Closed ) Ġ{ ĊĠĠĠĠĠĠĠ Ġdo ors Opened Ġ= Ġfalse ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Event S ourcing Handler ĊĠĠĠ Ġfun Ġhandle ( event : ĠE lev ator Moved To Floor ) Ġ{ ĊĠĠĠĠĠĠĠ Ġfloor Ġ= Ġevent . floor ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ CommandHandler ĊĠĠĠ Ġfun Ġhandle ( command : ĠAssign E lev ator Target ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( targets . contains ( command . floor )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlogger . warn (" Target Ġis Ġalready Ġin Ġtargets Ġfor Ġ${ id . id }") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġapply ( E lev ator Target Assigned ( command . elev ator Id , Ġcommand . floor )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Event S ourcing Handler ĊĠĠĠ Ġfun Ġhandle ( event : ĠE lev ator Target Assigned ) Ġ{ ĊĠĠĠĠĠĠĠ Ġtargets . add ( event . floor ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ CommandHandler ĊĠĠĠ Ġfun Ġhandle ( command : ĠRemove E lev ator Target ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ(! targets . contains ( command . floor )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlogger . warn (" Target Ġis Ġnot Ġin Ġtargets Ġfor Ġ${ id . id }") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġapply ( E lev ator Target Removed ( command . elev ator Id , Ġcommand . floor )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Event S ourcing Handler ĊĠĠĠ Ġfun Ġhandle ( event : ĠE lev ator Target Removed ) Ġ{ ĊĠĠĠĠĠĠĠ Ġtargets . remove ( event . floor ) ĊĠĠĠ Ġ} Ċ } Ċ