/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.tinkerpop.gremlin.process.traversal.util;

import org.apache.tinkerpop.gremlin.process.traversal.Traversal;

import java.util.function.Consumer;

/**
 * This class represents the state of the output of a child {@link Traversal} where it is either productive or not.
 * It is productive if it has at least one traverser within it, even if that traverser is holding a {@code null}.
 */
public final class TraversalProduct {
    public static final TraversalProduct UNPRODUCTIVE = new TraversalProduct();
    private final Object o;
    private final boolean productive;

    private TraversalProduct() {
        this.o = null; // null is valid technically but productive=false trumps it
        this.productive = false;
    }

    TraversalProduct(final Object o) {
        this.o = o;
        this.productive = true;
    }

    public Object get() {
        return o;
    }

    public boolean isProductive() {
        return productive;
    }

    public void ifProductive(final Consumer<Object> c) {
        if (productive) c.accept(o);
    }
}
