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

com.ajjpj.afoundation.collection.graph.AEdgePath Maven / Gradle / Ivy

There is a newer version: 1.0-pre19
Show newest version
package com.ajjpj.afoundation.collection.graph;

import com.ajjpj.afoundation.collection.immutable.AHashSet;
import com.ajjpj.afoundation.collection.immutable.AList;
import com.ajjpj.afoundation.collection.immutable.ASet;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


/**
 * This class represents a path in a graph, including the actual edge objects it consists of
 *
 * @author arno
 */
public class AEdgePath> implements Serializable {
    private final AList edges;
    private final ASet nodes;
    private final N to;

    @SuppressWarnings ("unchecked")
    public static > AEdgePath create (E edge) {
        return new AEdgePath (AList.create (edge), AHashSet.create (edge.getFrom (), edge.getTo ()), edge.getTo ());
    }

    AEdgePath (AList edges, ASet nodes, N to) {
        this.edges = edges;
        this.nodes = nodes;
        this.to = to;
    }

    AEdgePath prepend (E edge) {
        return new AEdgePath<> (edges.cons (edge), nodes.added (edge.getFrom ()), to);
    }

    @SuppressWarnings ("unchecked")
    public List getEdges() {
        return (List) Collections.unmodifiableList (Arrays.asList (edges));
    }

    public int size() {
        return edges.size ();
    }

    public N getFrom() {
        return edges.head ().getFrom ();
    }

    public N getTo() {
        return to;
    }

    public boolean hasCycle() {
        return nodes.size () != size ()+1;
    }

    public boolean isMinimalCycle() {
        return getFrom () == getTo () && nodes.size () == size ();
    }

    public boolean hasNonMinimalCycle() {
        return hasCycle () && !isMinimalCycle ();
    }

    @Override public String toString () {
        return "AEdgePath{" +
                "edges=" + edges +
                ", nodes=" + nodes +
                ", to=" + to +
                '}';
    }

    @Override
    public boolean equals (Object o) {
        if (this == o) return true;
        if (o == null || getClass () != o.getClass ()) return false;

        AEdgePath aEdgePath = (AEdgePath) o;

        return edges.equals (aEdgePath.edges);

    }
    @Override
    public int hashCode () {
        return edges.hashCode ();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy