All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tinkerpop.gremlin.structure.strategy.ReadOnlyGraphStrategy Maven / Gradle / Ivy

package com.tinkerpop.gremlin.structure.strategy;

import com.tinkerpop.gremlin.structure.Edge;
import com.tinkerpop.gremlin.structure.Property;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.util.function.STriFunction;

import java.util.Collections;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

/**
 * This {@link GraphStrategy} prevents the graph from being modified and will throw a
 * {@link UnsupportedOperationException} if an attempt is made to do so.
 *
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
public class ReadOnlyGraphStrategy implements GraphStrategy {
    @Override
    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
        return readOnlyFunction();
    }

    @Override
    public UnaryOperator> getAddEdgeStrategy(final Strategy.Context ctx) {
        return readOnlyTriFunction();
    }

    @Override
    public  UnaryOperator>> getElementProperty(Strategy.Context ctx) {
        return readOnlyBiFunction();
    }

    @Override
    public UnaryOperator> getElementPropertiesSetter(Strategy.Context ctx) {
        return (f) -> (t) -> {
            throw Exceptions.graphUsesReadOnlyStrategy();
        };
    }

    @Override
    public UnaryOperator> getRemoveElementStrategy(final Strategy.Context ctx) {
        return readOnlySupplier();
    }

    @Override
    public  UnaryOperator> getRemovePropertyStrategy(final Strategy.Context> ctx) {
        return readOnlySupplier();
    }

    @Override
    public UnaryOperator> getVariableSetStrategy(Strategy.Context ctx) {
        return (f) -> (k, v) -> {
            throw Exceptions.graphUsesReadOnlyStrategy();
        };
    }

    @Override
    public UnaryOperator>> getVariableAsMapStrategy(Strategy.Context ctx) {
        return (f) -> () -> Collections.unmodifiableMap(f.get());
    }

    @Override
    public String toString() {
        return ReadOnlyGraphStrategy.class.getSimpleName().toLowerCase();
    }

    public static  UnaryOperator> readOnlySupplier() {
        return (f) -> () -> {
            throw Exceptions.graphUsesReadOnlyStrategy();
        };
    }

    public static  UnaryOperator> readOnlyFunction() {
        return (f) -> (t) -> {
            throw Exceptions.graphUsesReadOnlyStrategy();
        };
    }

    public static  UnaryOperator>> readOnlyBiFunction() {
        return (f) -> (t, u) -> {
            throw Exceptions.graphUsesReadOnlyStrategy();
        };
    }

    public static  UnaryOperator> readOnlyTriFunction() {
        return (f) -> (t, u, v) -> {
            throw Exceptions.graphUsesReadOnlyStrategy();
        };
    }

    public static class Exceptions {
        public static UnsupportedOperationException graphUsesReadOnlyStrategy() {
            return new UnsupportedOperationException(String.format("Graph uses %s and is therefore unmodifiable", ReadOnlyGraphStrategy.class));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy