Defer the given action.

@param metadata metadata associated with the action
@param action action to defer
@param future future that will be bound to the action
@param <T> return type of the action
@return a deferred action container
766665
private <T> DeferredAction<M> deferAction(
      final M metadata, final Supplier<? extends CompletionStage<T>> action,
      final CompletableFuture<T> future
  ) {
    return new DeferredAction<>(metadata, () -> {
      final CompletionStage<? extends T> resultFuture;

      try {
        resultFuture = action.get();
      } catch (final Exception e) {
        future.completeExceptionally(e);
        markProcessed(metadata);
        return;
      }

      resultFuture.handleAsync((result, e) -> {
        if (e != null) {
          future.completeExceptionally(e);
        } else {
          future.complete(result);
        }

        markProcessed(metadata);
        return null;
      }, executor);
    });
  }
1312960
@Weight(value = Weight.Unit.VARIABLE, comment = "Depends on the current call stack depth")
  public static void processDeferredActions() {
    final int stackDepth = ThreadUtils.stackDepth();

    final List<Deferred> list = REGISTRY.get();
    final Iterator<Deferred> iterator = list.iterator();

    while (iterator.hasNext()) {
      final Deferred deferred = iterator.next();
      if (deferred.getStackDepth() >= stackDepth) {
        try {
          deferred.executeDeferred();
        } catch (Exception ex) {
          final UnexpectedProcessingError error = new UnexpectedProcessingError("Error during deferred action processing", ex);
          MetaErrorListeners.fireError(error.getMessage(), error);
        } finally {
          iterator.remove();
        }
      }
    }
    if (list.isEmpty()) {
      REGISTRY.remove();
    }
  }
804063
static <T> ContinuableFuture<T> execute(final Callable<T> action) {
        return execute(new FutureTask<>(action), SERIAL_EXECUTOR);
    }
1320999
public void forEach(final MetaData metaData)
    {
        int counterId = 0;

        final AtomicBuffer metaDataBuffer = this.metaDataBuffer;

        for (int i = 0, capacity = metaDataBuffer.capacity(); i < capacity; i += METADATA_LENGTH)
        {
            final int recordStatus = metaDataBuffer.getIntVolatile(i);
            if (RECORD_ALLOCATED == recordStatus)
            {
                final int typeId = metaDataBuffer.getInt(i + TYPE_ID_OFFSET);
                final String label = labelValue(metaDataBuffer, i);
                final DirectBuffer keyBuffer = new UnsafeBuffer(metaDataBuffer, i + KEY_OFFSET, MAX_KEY_LENGTH);

                metaData.accept(counterId, typeId, keyBuffer, label);
            }
            else if (RECORD_UNUSED == recordStatus)
            {
                break;
            }

            counterId++;
        }
    }
1106135
@Override
	protected void submitRedoMeta(final int futureUse) {
		if (!useRedo) {
			return;
		}
		createRedoThread();
		final ByteBuffer buf = bufstack.pop();
		buf.putInt(0x0C0C0C0C); // META HEADER/FOOTER
		buf.flip();
		if (useRedoThread) {
			try {
				redoQueue.put(buf);
			} catch (InterruptedException e) {
				log.error("InterruptedException in submitRedoMeta(" + futureUse + ")", e);
			}
		} else {
			redoStore.write(buf);
			bufstack.push(buf);
		}
	}
749262
static ContinuableFuture<Void> executeOnUiThread(final Try.Runnable<? extends Exception> action, final long delay) {
        return execute(new FutureTask<Void>(FN.toCallable(action)), _UI_EXECUTOR, delay);
    }
1162270
static <T> ContinuableFuture<T> executeOnUiThread(final Callable<T> action, final long delay) {
        return execute(new FutureTask<>(action), _UI_EXECUTOR, delay);
    }
112669
public CmdLineAction onSwitch(Consumer<@NonNull String> action) {
		SwitchCmdLineAction switchAction = new SwitchCmdLineAction(action);

		this.switchActions.add(switchAction);
		return switchAction;
	}
797621
public void forEach(final IntConsumer action) {
		requireNonNull(action);

		final int size = _size;
		for (int i = 0; i < size; ++i) {
			action.accept(_data[i]);
		}
	}
820907
private static IAction createAction(String action) {
        IAction actn = null;
        
        if (!StringUtils.isEmpty(action)) {
            if ((actn = ActionRegistry.getRegisteredAction(action)) == null) {
                actn = ActionUtil.createAction(null, action);
            }
        }
        
        return actn;
    }
498618
public void addViewAction(final String actionId, final ActorConsumer<?, ?> action) {
        parser.getData().addActorConsumer(actionId, action);
    }
1090821
public Deferred<Boolean> processTSMetaThroughTrees(final TSMeta meta) {
    if (config.enable_tree_processing()) {
      return TreeBuilder.processAllTrees(this, meta);
    }
    return Deferred.fromResult(false);
  }
867457
@Override
    public <T> T action(EntityTypeActionOperation<T> entityTypeActionOperation)
            throws ServiceException {
        try {
            return service.action(entityTypeActionOperation);
        } catch (UniformInterfaceException e) {
            throw processCatch(new ServiceException(e));
        } catch (ClientHandlerException e) {
            throw processCatch(new ServiceException(e));
        }
    }
530612
public void ordered(Consumer<T> action) {
        TopoSorter<T> sorter = new TopoSorter<>(this);
        sorter.ordered(action);
    }
1147188
private void checkAndCancelExpiredAction(final Action action) {
        if (action != null && action.hasMaintenanceSchedule() && action.isMaintenanceScheduleLapsed()) {
            try {
                controllerManagement.cancelAction(action.getId());
            } catch (final CancelActionNotAllowedException e) {
                LOG.info("Cancel action not allowed exception :{}", e);
            }
        }
    }
573037
@Override
    @Deprecated
    public void forEachRemaining(java.util.function.Consumer<? super Pair<A, B>> action) {
        super.forEachRemaining(action);
    }
254963
private void generateCancel(final ActionRef action, Featurable menu)
    {
        menu.getFeature(Actionable.class).setAction(() ->
        {
            clearMenus();
            final Collection<ActionRef> parents = previous.get(action);
            createMenus(parents, parents);
        });
    }