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

org.jgrapht.graph.ParanoidGraph Maven / Gradle / Ivy

There is a newer version: 1.5.2
Show newest version
/*
 * (C) Copyright 2007-2016, by John V Sichi and Contributors.
 *
 * JGraphT : a free Java graph-theory library
 *
 * This program and the accompanying materials are dual-licensed under
 * either
 *
 * (a) the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation, or (at your option) any
 * later version.
 *
 * or (per the licensee's choosing)
 *
 * (b) the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation.
 */
package org.jgrapht.graph;

import java.util.*;

import org.jgrapht.*;

/**
 * ParanoidGraph provides a way to verify that objects added to a graph obey the standard
 * equals/hashCode contract. It can be used to wrap an underlying graph to be verified. Note that
 * the verification is very expensive, so ParanoidGraph should only be used during debugging.
 *
 * @param  the graph vertex type
 * @param  the graph edge type
 *
 * @author John Sichi
 */
public class ParanoidGraph
    extends GraphDelegator
{
    private static final long serialVersionUID = 5075284167422166539L;

    /**
     * Create a new paranoid graph.
     * 
     * @param g the underlying wrapped graph
     */
    public ParanoidGraph(Graph g)
    {
        super(g);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean addEdge(V sourceVertex, V targetVertex, E e)
    {
        verifyAdd(edgeSet(), e);
        return super.addEdge(sourceVertex, targetVertex, e);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean addVertex(V v)
    {
        verifyAdd(vertexSet(), v);
        return super.addVertex(v);
    }

    private static  void verifyAdd(Set set, T t)
    {
        for (T o : set) {
            if (o == t) {
                continue;
            }
            if (o.equals(t) && (o.hashCode() != t.hashCode())) {
                throw new IllegalArgumentException(
                    "ParanoidGraph detected objects " + "o1 (hashCode=" + o.hashCode()
                        + ") and o2 (hashCode=" + t.hashCode() + ") where o1.equals(o2) "
                        + "but o1.hashCode() != o2.hashCode()");
            }
        }
    }
}

// End ParanoidGraph.java




© 2015 - 2025 Weber Informatics LLC | Privacy Policy