GraphBuilder.java

package edu.hawaii.ics.yucheng;

import java.security.InvalidParameterException;

/**
 * A class that builds a graph.
 */
class GraphBuilder {

    /**
     * Returns true if a valid exceeds a range.
     * 
     * @param value The value.
     * 
     * @param low The low value (inclusive).
     * 
     * @param high The high value (inclusive).
     * 
     * @return True indicates the value exceeds the range.
     */
    private static boolean exceeds(final int value, final int low, final int high) {
        return value < low || value > high;
    }

    /** The height of the mesh graph. */
    public final int        height;

    /** The base location. */
    private Vertex          myBase;

    /** The initial battery capacity. */
    private int             myCapacity;

    /** The array of battery consumption values. */
    private final int[]     myConsumptions;

    /** The array of obstacles. */
    private final boolean[] myObstacles;

    /** The array of priority values. */
    private final int[]     myPriorities;

    /** The width of the mesh graph. */
    public final int        width;


    /**
     * Initializes a new instance of the class.
     * 
     * @param width The width of the graph.
     * 
     * @param height The height of the graph.
     */
    public GraphBuilder(final int width, final int height) {

        if (width < 1)
            throw new InvalidParameterException("width");
        if (height < 1)
            throw new InvalidParameterException("height");

        this.width = width;
        this.height = height;
        myConsumptions = new int[width * height];
        myObstacles = new boolean[width * height];
        myPriorities = new int[width * height];
        myCapacity = 0;
        myBase = new Vertex(0, 0);
    }


    /**
     * Returns the base location.
     * 
     * @return The base location.
     */
    public Vertex base() {
        return myBase;
    }


    /**
     * Returns the initial battery capacity.
     * 
     * @return The initial battery capacity.
     */
    public int capacity() {
        return myCapacity;
    }


    /**
     * Returns the battery consumption value at a specified location.
     * 
     * @param x The x coordinate.
     * 
     * @param y The y coordinate.
     * 
     * @return The battery consumption value.
     */
    public int consumption(final int x, final int y) {
        return myConsumptions[indexOf(x, y)];
    }


    /**
     * Returns the array index at a specified location.
     * 
     * @param x The x coordinate.
     * 
     * @param y The y coordinate.
     * 
     * @return The array index.
     */
    private int indexOf(final int x, final int y) {

        if (exceeds(x, 0, width - 1))
            throw new IndexOutOfBoundsException("x");
        if (exceeds(y, 0, height - 1))
            throw new IndexOutOfBoundsException("y");

        return width * y + x;
    }


    /**
     * Returns true if a specified location is an obstacle.
     * 
     * @param x The x coordinate.
     * 
     * @param y The y coordinate.
     * 
     * @return True indicates the specified location is an obstacle.
     */
    public boolean obstacle(final int x, final int y) {
        return myObstacles[indexOf(x, y)];
    }


    /**
     * Returns the priority value at a specified location.
     * 
     * @param x The x coordinate.
     * 
     * @param y The y coordinate.
     * 
     * @return Returns the priority value at a specified location.
     */
    public int priority(final int x, final int y) {
        return myPriorities[indexOf(x, y)];
    }


    /**
     * Sets the base location.
     * 
     * @param x The x coordinate.
     * 
     * @param y The y coordinate.
     */
    public void setBase(final int x, final int y) {
        indexOf(x, y);
        myBase = new Vertex(x, y);
    }


    /**
     * Sets the initial battery capacity.
     * 
     * @param capacity The battery capacity.
     */
    public void setCapacity(final int capacity) {
        if (capacity < 0)
            throw new IllegalArgumentException("capacity");
        myCapacity = capacity;
    }


    /**
     * Sets the battery consumption value at a specified location.
     * 
     * @param x The x coordinate.
     * 
     * @param y The y coordinate.
     * 
     * @param value The battery consumption value.
     */
    public void setConsumption(final int x, final int y, final int value) {
        if (value < 0)
            throw new IllegalArgumentException("value");
        myConsumptions[indexOf(x, y)] = value;
    }


    /**
     * Sets or unsets an obstacle at a specified location.
     * 
     * @param x The x coordinate.
     * 
     * @param y The y coordinate.
     * 
     * @param value True indicates there is an obstacle at the location.
     */
    public void setObstacle(final int x, final int y, final boolean value) {
        myObstacles[indexOf(x, y)] = value;
    }


    /**
     * Sets the priority value at a specified location.
     * 
     * @param x The x coordinate.
     * 
     * @param y The y coordinate.
     * 
     * @param value The priority value.
     */
    public void setPriority(final int x, final int y, final int value) {
        if (value < 0)
            throw new IllegalArgumentException("value");
        myPriorities[indexOf(x, y)] = value;
    }


    /**
     * Returns an immutable graph based on this class data.
     * 
     * @return A graph.
     */
    public Graph toGraph() {
        return new Graph(this);
    }
}
Valid HTML 4.01 Valid CSS