public static <T extends Traversal.Admin<?, ?>> T addHasContainer(final T traversal, final HasContainer hasContainer) {
        if (traversal.getEndStep() instanceof HasContainerHolder) {
            ((HasContainerHolder) traversal.getEndStep()).addHasContainer(hasContainer);
            return traversal;
        } else
            return (T) traversal.addStep(new HasStep<>(traversal, hasContainer));
    }
Used to left-fold a {@link HasContainer} to a {@link HasContainerHolder} if it exists. Else, append a {@link HasStep}.

@param traversal    the traversal to fold or append.
@param hasContainer the container to add left or append.
@param <T>          the traversal type
@return the has container folded or appended traversal
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalHelper.java