code: @Override
    public Behavior initialBehavior(Optional<BlogState> snapshotState) {
      if (snapshotState.isPresent() && !snapshotState.get().isEmpty()) {
        // behavior after snapshot must be restored by initialBehavior
        return becomePostAdded(snapshotState.get());
      } else {
        // Behavior consist of a State and defined event handlers and command handlers.
        BehaviorBuilder b = newBehaviorBuilder(BlogState.EMPTY);
  
        // Command handlers are invoked for incoming messages (commands).
        // A command handler must "return" the events to be persisted (if any).
        b.setCommandHandler(AddPost.class, (AddPost cmd, CommandContext<AddPostDone> ctx) -> {
          if (cmd.getContent().getTitle() == null || cmd.getContent().getTitle().equals("")) {
            ctx.invalidCommand("Title must be defined");
            return ctx.done();
          }
  
          final PostAdded postAdded = new PostAdded(entityId(), cmd.getContent());
          return ctx.thenPersist(postAdded, (PostAdded evt) ->
          // After persist is done additional side effects can be performed
              ctx.reply(new AddPostDone(entityId())));
        });
  
        // Event handlers are used both when when persisting new events and when replaying
        // events.
        b.setEventHandlerChangingBehavior(PostAdded.class, evt ->
          becomePostAdded(new BlogState(Optional.of(evt.getContent()), false)));
  
        return b.build();
      }
    }
docs: #inject