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

guru.nidi.graphviz.model.ImmutableGraph Maven / Gradle / Ivy

There is a newer version: 0.18.1
Show newest version
/*
 * Copyright © 2015 Stefan Niederhauser ([email protected])
 *
 * Licensed 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 guru.nidi.graphviz.model;

import guru.nidi.graphviz.attribute.*;

import java.util.*;
import java.util.function.Function;

class ImmutableGraph extends MutableGraph implements Graph {
    ImmutableGraph() {
    }

    private ImmutableGraph(boolean strict, boolean directed, boolean cluster, Label name,
                           LinkedHashSet nodes, LinkedHashSet subgraphs, List links,
                           MutableAttributed nodeAttributes,
                           MutableAttributed linkAttributes,
                           MutableAttributed graphAttributes) {
        super(strict, directed, cluster, name, nodes, subgraphs, links,
                nodeAttributes, linkAttributes, graphAttributes);
    }

    private ImmutableGraph copyOfMut() {
        return copyOfMut(this);
    }

    static ImmutableGraph copyOfMut(MutableGraph g) {
        return new ImmutableGraph(g.strict, g.directed, g.cluster, g.name,
                new LinkedHashSet<>(g.nodes), new LinkedHashSet<>(g.subgraphs), new ArrayList<>(g.links),
                g.nodeAttrs, g.linkAttrs, g.graphAttrs);
    }

    @Override
    public MutableGraph toMutable() {
        return copy();
    }

    public Graph strict() {
        return (ImmutableGraph) copyOfMut().setStrict(true);
    }

    public Graph directed() {
        return (ImmutableGraph) copyOfMut().setDirected(true);
    }

    public Graph cluster() {
        return (ImmutableGraph) copyOfMut().setCluster(true);
    }

    public Graph named(String name) {
        return (ImmutableGraph) copyOfMut().setName(name);
    }

    public Graph with(LinkSource... sources) {
        return (ImmutableGraph) copyOfMut().add(sources);
    }

    public Graph with(List sources) {
        return (ImmutableGraph) copyOfMut().add(sources);
    }

    public Graph link(LinkTarget... targets) {
        return (ImmutableGraph) copyOfMut().addLink(targets);
    }

    public Graph link(LinkTarget target) {
        return (ImmutableGraph) copyOfMut().addLink(target);
    }

    public Attributed nodeAttr() {
        return new GraphAttributed<>(MutableGraph::nodeAttrs);
    }

    public Attributed linkAttr() {
        return new GraphAttributed<>(MutableGraph::linkAttrs);
    }

    public Attributed graphAttr() {
        return new GraphAttributed<>(MutableGraph::graphAttrs);
    }

    @Override
    public List links() {
        return Collections.unmodifiableList(super.links());
    }

    @Override
    public String toString() {
        return new Serializer(this).serialize();
    }

    private class GraphAttributed implements Attributed {
        private final Function> attributeSource;

        public GraphAttributed(Function> attributeSource) {
            this.attributeSource = attributeSource;
        }

        @Override
        public Graph with(Attributes attrs) {
            return (ImmutableGraph) attributeSource.apply(copyOfMut()).add(attrs);
        }

        @Override
        public Attributes applyTo(MapAttributes attrs) {
            return attributeSource.apply(ImmutableGraph.this).applyTo(attrs);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy