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

es.usc.citius.hipster.thirdparty.graphs.jung.JUNGHipsterGraphAdapter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 CITIUS , University of Santiago de Compostela.
 *
 *    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 es.usc.citius.hipster.thirdparty.graphs.jung;

import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.EdgeType;
import es.usc.citius.hipster.graph.DirectedEdge;
import es.usc.citius.hipster.graph.GraphEdge;
import es.usc.citius.hipster.graph.HipsterGraph;
import es.usc.citius.hipster.graph.UndirectedEdge;
import es.usc.citius.hipster.util.Function;
import es.usc.citius.hipster.util.F;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

/**
 * An adapter to adapt a JUNG graph to a general HipsterGraph interface.
 * 
 * @author Pablo Rodríguez Mier <[email protected]>
 */
public class JUNGHipsterGraphAdapter implements HipsterGraph {
    protected Graph graph;

    public JUNGHipsterGraphAdapter(Graph graph) {
        this.graph = graph;
    }

    protected Iterable> adapt(final Iterable iterable){
        return new Iterable>() {
            @Override
            public Iterator> iterator() {
                return F.map(iterable.iterator(), new Function>() {
                    @Override
                    public GraphEdge apply(E edge) {
                        if (graph.getEdgeType(edge).equals(EdgeType.DIRECTED)) {
                            return new DirectedEdge(graph.getSource(edge), graph.getDest(edge), edge);
                        }
                        return new UndirectedEdge(graph.getSource(edge), graph.getDest(edge), edge);
                    }
                });
            }
        };
    }
    @Override
    public Iterable> edges() {
        final Collection edges = graph.getEdges();
        if (edges == null || edges.isEmpty()){
            return Collections.emptyList();
        }
        return adapt(edges);
    }

    @Override
    public Iterable vertices() {
        return graph.getVertices();
    }

    @Override
    public Iterable> edgesOf(V vertex) {
        final Collection edges = graph.getIncidentEdges(vertex);
        if (edges == null || edges.isEmpty()){
            return Collections.emptyList();
        }
        return adapt(edges);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy