GraphSolution.java

package edu.hawaii.ics.yucheng;

/**
 * An immutable class representing a solution to a graph.
 */
class GraphSolution {

    /**
     * Verifies a pointer is not null.
     * 
     * @param pointer The pointer object.
     * 
     * @param name The name of the parameter.
     */
    private static void verifyPointer(final Object pointer, final String name) {
        if (pointer == null)
            throw new NullPointerException(name);
    }

    /** The sum of the priorities of the path. */
    public final int       goodness;

    /** The arrival path. */
    private final Vertex[] myArrival;

    /** The departure path. */
    private final Vertex[] myDeparture;

    /** The sequence path. */
    private final Vertex[] mySequence;


    /**
     * Initializes a new instance of the class.
     * 
     * @param graph The graph.
     * 
     * @param solution The solution path.
     * 
     * @param departure The departure path.
     * 
     * @param arrival The arrival path.
     */
    public GraphSolution(final Graph graph, final Vertex[] solution,
        final Vertex[] departure, final Vertex[] arrival) {

        verifyPointer(graph, "graph");
        verifyPointer(solution, "solution");
        verifyPointer(departure, "departure");
        verifyPointer(arrival, "arrival");

        mySequence = new Vertex[solution.length];
        for (int i = 0; i < solution.length; i++)
            mySequence[i] = solution[i];

        myDeparture = new Vertex[departure.length];
        for (int i = 0; i < departure.length; i++)
            myDeparture[i] = departure[i];

        myArrival = new Vertex[arrival.length];
        for (int i = 0; i < arrival.length; i++)
            myArrival[i] = arrival[i];

        goodness = computeGoodness(graph);
    }


    /**
     * Returns the arrival vertex at the specified path.
     * 
     * @param index The index of the vertex.
     * 
     * @return The arrival vertex.
     */
    public Vertex arrivalAt(final int index) {
        return myArrival[index];
    }


    /**
     * Returns the length of the arrival path.
     * 
     * @return The length.
     */
    public int arrivalLength() {
        return myArrival.length;
    }


    /**
     * Computes the goodness field.
     * 
     * @return The length.
     */
    private int computeGoodness(final Graph graph) {
        int sum = 0;
        for (final Vertex vertex : mySequence)
            sum += graph.priority(vertex.x, vertex.y);
        return sum;
    }


    /**
     * Returns the departure vertex at the specified path.
     * 
     * @param index The index of the vertex.
     * 
     * @return The departure vertex.
     */
    public Vertex departureAt(final int index) {
        return myDeparture[index];
    }


    /**
     * Returns the length of the departure path.
     * 
     * @return The length.
     */
    public int departureLength() {
        return myDeparture.length;
    }


    /**
     * Returns the solution sequence vertex at the specified path.
     * 
     * @param index The index of the vertex.
     * 
     * @return The solution sequence vertex.
     */
    public Vertex sequenceAt(final int index) {
        return mySequence[index];
    }


    /**
     * Returns the length of the solution sequence path.
     * 
     * @return The length.
     */
    public int sequenceLength() {
        return mySequence.length;
    }


    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        builder.append("goodness = " + goodness + "\n");
        builder.append("sequence = ...\n");
        for (int i = 0; i < mySequence.length; i++)
            builder.append("  [" + (i + 1) + "] = " + mySequence[i] + "\n");
        return builder.toString();
    }
}
Valid HTML 4.01 Valid CSS