001package org.clafer.graph;
002
003import java.util.Collections;
004import java.util.HashSet;
005import java.util.Set;
006import org.clafer.common.Check;
007
008/**
009 * A mutable directed vertex.
010 *
011 * @param <V> the type of the data
012 * @author jimmy
013 */
014public class Vertex<V> {
015
016    private final V data;
017    private final Set<Vertex<V>> neighbours = new HashSet<>();
018
019    public Vertex(V data) {
020        this.data = Check.notNull(data);
021    }
022
023    /**
024     * Returns the data associated with this vertex. Two two vertices with the
025     * same data are still different vertices.
026     *
027     * @return the data associated with this vertex
028     */
029    public V getData() {
030        return data;
031    }
032
033    /**
034     * Returns the set of vertexes that have an incoming edge from this vertex.
035     *
036     * @return the neighbours of this vertex
037     */
038    public Set<Vertex<V>> getNeighbours() {
039        return Collections.unmodifiableSet(neighbours);
040    }
041
042    /**
043     * Add a new edge starting from this edge to the neighbour. If this edge
044     * already exists, then no changes occur.
045     *
046     * @param neighbour a new neighbour of this vertex
047     * @return this vertex
048     */
049    public Vertex addNeighbour(Vertex<V> neighbour) {
050        neighbours.add(neighbour);
051        return this;
052    }
053
054    /**
055     * {@inheritDoc}
056     */
057    @Override
058    public boolean equals(Object obj) {
059        return this == obj;
060    }
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public int hashCode() {
067        return data.hashCode();
068    }
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public String toString() {
075        return data.toString();
076    }
077}