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

com.googlecode.blaisemath.graph.NodeSetInGraph Maven / Gradle / Ivy

The newest version!
/*
 * NodeSetInGraph.java
 * Created on May 23, 2013
 */

package com.googlecode.blaisemath.graph;

/*
 * #%L
 * BlaiseGraphTheory
 * --
 * Copyright (C) 2009 - 2016 Elisha Peterson
 * --
 * 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.
 * #L%
 */



import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.Sets;
import java.util.Set;
import javax.annotation.Nullable;

/**
 * Data structure describing a collection of nodes in a graph. This is a useful way to pass
 * a reference to both the collection of nodes and the graph.
 * @param  node type
 * 
 * @author petereb1
 */
public class NodeSetInGraph {

    private final Set nodes;
    private final Graph graph;

    public NodeSetInGraph(Set nodes, @Nullable Graph graph) {
        checkNotNull(nodes);
        this.graph = graph;
        // allow for nodes not in graph, but remove them
        this.nodes = Sets.newLinkedHashSet(nodes);
        if (graph != null) {
            this.nodes.retainAll(graph.nodes());
        }
    }
    
    /**
     * Factory method for creating a node set.
     * @param  node type
     * @param nodes nodes
     * @return node set data structure
     */
    public static  NodeSetInGraph create(E... nodes) {
        return new NodeSetInGraph(Sets.newHashSet(nodes), null);
    }

    public Set getNodes() {
        return nodes;
    }

    @Nullable 
    public Graph getGraph() {
        return graph;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy