Class GraphBuilderImpl<V,​E>

  • Type Parameters:
    V - the type of the decorated vertex value
    E - the type of the decorated edge value
    All Implemented Interfaces:
    GraphBuilder<V,​E>

    public class GraphBuilderImpl<V,​E>
    extends Object
    implements GraphBuilder<V,​E>

    Implementation of a graph builder (it creates directed weighted edges)

    Vertices are identified using an ID, which is extracted using a key mapper. the default key mapper tries to extract the key from a method that is annotation with GraphId, make sure, that this method has a consistent state. If no GraphId can be found the decorated vertex value is used as a key. If there are multiple methods that are annotated with GraphId the first one is used. Please be aware this could lead to unwanted behaviour, and should therefore be avoided.

    If an edge between vertex a and b is created, it first checks if there is already an edge, if this is the case the weight of the edge is incremented by one. The provided data element will be ignored, but callback functions are called.

    Since:
    1.0
    Author:
    Andreas Pointner
    • Method Detail

      • getDefaultKeyMapper

        public static <V> Function<V,​Object> getDefaultKeyMapper()
        Creates a new instance of the default key mapper, which either uses a method that is annotated with GraphId or uses the element itself as a fallback.
        Type Parameters:
        V - the type of the decorated vertex
        Returns:
        an function representing the key mapper.
      • create

        public static <V,​E> GraphBuilder<V,​E> create()
        Creates a new graph builder using the default key mapper as well as the default factory (GraphFactoryFactory.getDefaultFactory()
        Type Parameters:
        V - the type of the decorated vertex value
        E - the type of the decorated edge value
        Returns:
        a new instance of the GraphBuilder
      • create

        public static <V,​E> GraphBuilder<V,​E> create​(GraphFactory graphFactory)
        Creates a new graph builder using the default key mapper
        Type Parameters:
        V - the type of the decorated vertex value
        E - the type of the decorated edge value
        Parameters:
        graphFactory - specify the graph factory that should be used
        Returns:
        a new instance of the GraphBuilder
      • create

        public static <V,​E> GraphBuilder<V,​E> create​(Function<V,​Object> keyMapper)
        Creates a new graph builder using the default factory (GraphFactoryFactory.getDefaultFactory()
        Type Parameters:
        V - the type of the decorated vertex value
        E - the type of the decorated edge value
        Parameters:
        keyMapper - specify the key mapper that should be used
        Returns:
        a new instance of the GraphBuilder
      • create

        public static <V,​E> GraphBuilder<V,​E> create​(Function<V,​Object> keyMapper,
                                                                 GraphFactory graphFactory)
        Creates a new graph builder
        Type Parameters:
        V - the type of the decorated vertex value
        E - the type of the decorated edge value
        Parameters:
        graphFactory - specify the graph factory that should be used
        keyMapper - specify the key mapper that should be used
        Returns:
        a new instance of the GraphBuilder
      • create

        public static <V,​E> GraphBuilder<V,​E> create​(Function<V,​Object> keyMapper,
                                                                 GraphFactory graphFactory,
                                                                 BinaryOperator<V> merger)
        Creates a new graph builder.
        Type Parameters:
        V - the type of the decorated vertex value
        E - the type of the decorated edge value
        Parameters:
        graphFactory - specify the graph factory that should be used
        keyMapper - specify the key mapper that should be used
        merger - specific the merger that should be used
        Returns:
        a new instance of the GraphBuilder
      • create

        public static <V,​E> GraphBuilder<V,​E> create​(Function<V,​Object> keyMapper,
                                                                 BinaryOperator<V> merger)
        Creates a new graph builder.
        Type Parameters:
        V - the type of the decorated vertex value
        E - the type of the decorated edge value
        Parameters:
        keyMapper - specify the key mapper that should be used
        merger - specific the merger that should be used
        Returns:
        a new instance of the GraphBuilder
      • getOrAddVertex

        public Vertex<V,​E> getOrAddVertex​(V value)
        Description copied from interface: GraphBuilder
        Method to add a new vertex to the graph builder. This method takes a value, which is used as the decorated value for the created vertex. If there is already another vertex, that confirms to the value then an existing instance of a vertex may be reused.
        Specified by:
        getOrAddVertex in interface GraphBuilder<V,​E>
        Parameters:
        value - the value of the decorated element
        Returns:
        the new created or cached vertex
      • from

        public EdgeBuilder<V,​E> from​(V value)
        Description copied from interface: GraphBuilder
        Start to create a new Edge to the vertex that decorates value. If there is no vertex that decorated the value yet, a new vertex will be created, as it would be using GraphBuilder.addVertex(Object) method
        Specified by:
        from in interface GraphBuilder<V,​E>
        Parameters:
        value - the value of the decorated element
        Returns:
        a EdgeBuilder
      • fromByKey

        public EdgeBuilder<V,​E> fromByKey​(Object key)
        Description copied from interface: GraphBuilder
        Start to create a new Edge to the vertex that is identified by the given key. Make sure to add the vertex first, otherwise this method may fail.
        Specified by:
        fromByKey in interface GraphBuilder<V,​E>
        Parameters:
        key - the key which identifies the vertex
        Returns:
        a EdgeBuilder
      • toGraph

        public Graph<V,​E> toGraph()
        Description copied from interface: GraphBuilder
        Creates the graph based on the vertices and edges defines in that graph builder. This makes the GraphBuilder as "finished" which means, that all further operations on the graph builder will result in an IllegalStateException.
        Specified by:
        toGraph in interface GraphBuilder<V,​E>
        Returns:
        a Graph
      • updateKey

        public <T> void updateKey​(V element,
                                  T key,
                                  BiConsumer<V,​T> mapping)
        Description copied from interface: GraphBuilder
        Allows to update the key for a given node value in the list. The key must only be updated via this method to maintain a consistent state. The element that is passed to this function must be part of the list and must have the old key. The mapping function will be called with the element and the new key, and must update the elements key value.
        Specified by:
        updateKey in interface GraphBuilder<V,​E>
        Type Parameters:
        T - the type of the key
        Parameters:
        element - the element where the key should be updated
        key - the new key
        mapping - the mapping function to set the new key
      • addGraphCreationCallback

        public void addGraphCreationCallback​(Consumer<Graph<V,​E>> callback)
        Description copied from interface: GraphBuilder
        This method allows adding callback methods, that are applied once the toGraph method is called. One the callback method is added there is no more way to remove it and it will be executed on the graph creation. This method can be used for e.g. adding MetaTags:
        
             graphBuilder.addGraphCreationCallback(g -> g.addMetaTag(new MetaTagImpl<>("key", "value")));
         
        Specified by:
        addGraphCreationCallback in interface GraphBuilder<V,​E>
        Parameters:
        callback - the callback method to be added
      • addSubGraph

        public Graph<V,​E> addSubGraph​(Graph<V,​E> subGraph)
        Description copied from interface: GraphBuilder

        Adds the subgraph to this graph, and returns a new resulting graph, that represents a view on the elements of the all vertices and edges.

        Note: subGraph is no longer valid afterwards, as the resulting vertices and edges are removed from the graph and added to the new graph.

        Specified by:
        addSubGraph in interface GraphBuilder<V,​E>
        Parameters:
        subGraph - the subgraph that should be added
        Returns:
        the resulting snapshot on the subgraph