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

org.graylog2.utilities.Graphs Maven / Gradle / Ivy

There is a newer version: 6.1.4
Show newest version
/*
 * Copyright (C) 2020 Graylog, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Server Side Public License, version 1,
 * as published by MongoDB, Inc.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Server Side Public License for more details.
 *
 * You should have received a copy of the Server Side Public License
 * along with this program. If not, see
 * .
 */
package org.graylog2.utilities;

import com.google.common.graph.EndpointPair;
import com.google.common.graph.Graph;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.ImmutableGraph;
import com.google.common.graph.MutableGraph;

public final class Graphs {
    private static final ImmutableGraph EMPTY_DIRECTED_GRAPH = ImmutableGraph.copyOf(GraphBuilder.directed().build());
    private static final ImmutableGraph EMPTY_UNDIRECTED_GRAPH = ImmutableGraph.copyOf(GraphBuilder.undirected().build());

    private Graphs() {
    }

    /**
     * Returns an empty directed graph (immutable).
     *
     * @param  The class of the nodes
     * @return an empty directed graph
     */
    @SuppressWarnings("unchecked")
    public static  ImmutableGraph emptyDirectedGraph() {
        return EMPTY_DIRECTED_GRAPH;
    }

    /**
     * Returns an empty undirected graph (immutable).
     *
     * @param  The class of the nodes
     * @return an empty undirected graph
     */
    @SuppressWarnings("unchecked")
    public static  ImmutableGraph emptyUndirectedGraph() {
        return EMPTY_UNDIRECTED_GRAPH;
    }

    /**
     * Returns an empty graph (immutable) initialized with all properties queryable from {@code graph}.
     *
     * @param graph The graph to use as template for the created graph
     * @param    The class of the nodes
     * @return an empty graph
     * @see GraphBuilder#from(Graph)
     */
    public static  ImmutableGraph emptyGraph(Graph graph) {
        return ImmutableGraph.copyOf(GraphBuilder.from(graph).build());
    }

    /**
     * Returns an immutable directed graph, containing only the specified node.
     *
     * @param node The single node in the returned graph
     * @param   The class of the nodes
     * @return an immutable directed graph with a single node
     */
    public static  ImmutableGraph singletonDirectedGraph(N node) {
        final MutableGraph graph = GraphBuilder.directed().build();
        graph.addNode(node);
        return ImmutableGraph.copyOf(graph);
    }

    /**
     * Returns an immutable undirected graph, containing only the specified node.
     *
     * @param node The single node in the returned graph
     * @param   The class of the nodes
     * @return an immutable undirected graph with a single node
     */
    public static  ImmutableGraph singletonUndirectedGraph(N node) {
        final MutableGraph graph = GraphBuilder.undirected().build();
        graph.addNode(node);
        return ImmutableGraph.copyOf(graph);
    }

    /**
     * Returns an immutable graph, containing only the specified node.
     *
     * @param graph The graph to use as template for the created graph
     * @param node  The single node in the returned graph
     * @param    The class of the nodes
     * @return an immutable graph with a single node
     * @see GraphBuilder#from(Graph)
     */
    public static  ImmutableGraph singletonGraph(Graph graph, N node) {
        final MutableGraph mutableGraph = GraphBuilder.from(graph).build();
        mutableGraph.addNode(node);
        return ImmutableGraph.copyOf(mutableGraph);
    }

    /**
     * Merge all nodes and edges of two graphs.
     *
     * @param graph1 A {@link MutableGraph} into which all nodes and edges of {@literal graph2} will be merged
     * @param graph2 The {@link Graph} whose nodes and edges will be merged into {@literal graph1}
     * @param     The class of the nodes
     */
    public static  void merge(MutableGraph graph1, Graph graph2) {
        for (N node : graph2.nodes()) {
            graph1.addNode(node);
        }
        for (EndpointPair edge : graph2.edges()) {
            graph1.putEdge(edge.nodeU(), edge.nodeV());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy