/*
A basic implementation of the JFrame class.
 */
package usda.weru.weps;

import java.util.*;

/**
 * The barrier class builds and shows the selected barriers from the BarPanel class 
 * for the field along various chosen directions if any.
 * 
 */
public class Barrier /*implements Cloneable*/ {

    static final String NoBarrier = "none";
    String name = NoBarrier;
    String height = "0.0";
    String porosity = "0.0";
    String width = "0.0";
//    String location = "menu";
    double hgt = 0.0;
    double por = 0.0;
    double wid = 0.0;
    boolean userDefined = false;

    /**
     * Default constructor that currently does nothing.
     */
    public Barrier() {
    }

    /**
     * Single argument constructor that currently sets the width, height, 
     * porosity of the barriers along each direction. 
     * @param inputLine The initial set-up for the barrier's details on the 
     * height, width, porosity properties along the 4 required direction 
     * if provided.
     */
    public Barrier(String inputLine) {
        StringTokenizer st = new StringTokenizer(inputLine, "|");
        if (st.countTokens() == 4) {
            name = st.nextToken().trim();
            height = st.nextToken().trim();
            width = st.nextToken().trim();
            porosity = st.nextToken().trim();
        }
        else if (st.countTokens() == 5) {
            name = st.nextToken().trim();
            height = st.nextToken().trim();
            st.nextToken();	// skip rows
            porosity = st.nextToken().trim();
            width = st.nextToken().trim();
        }
        else {
            return;
        }
        try {
            hgt = Double.parseDouble(height);
            por = Double.parseDouble(porosity);
            wid = Double.parseDouble(width);
        }
        catch (NumberFormatException e) {
            //System.err.println("BP-B: " + e);
        }
    }

//		public int hashCode () {
//			return 1;
//		}
    /**
     * Convert the object to a string.
     * @return The new converted string value.
     */
    @Override
    public String toString() {
        return name;
    }

	/**
	 *
	 * @param obj
	 * @return
	 */
	@Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Barrier other = (Barrier) obj;
        if (!this.name.equals(other.name)) {
            return false;
        }
        if (this.hgt != other.hgt) {
            return false;
        }
        if (this.por != other.por) {
            return false;
        }
        if (this.wid != other.wid) {
            return false;
        }
        if (this.userDefined != other.userDefined) {
            return false;
        }
        return true;
    }

	/**
	 *
	 * @return
	 */
	@Override
    public int hashCode() {
        int hash = 7;
        hash = 61 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 61 * hash + (this.userDefined ? 1 : 0);
        return hash;
    }


    /**
     * This method makes the barriers with the heights, widths, porosity 
     * and the name provided by the file that stores this information
     * @return The string value that is assigned/used as for the various
     * attributes of each barrier. 
     */
    String makeBarrier() {
        if (name.equals(NoBarrier)) {
            return NoBarrier + "|0.0|0.0|1.0";
        }
        return name + "|" +
                height + "|" +
                width + "|" +
                porosity;

    }
    /**
     * This method makes an exact replica of the object being referenced.
     * @return The new object that was created by this method. 
     */
//		protected Object clone() {
//			Barrier bp = new Barrier();
//			bp.name = this.name;
//			bp.height = this.height;
//			bp.porosity = this.porosity;
//			bp.width = this.width;
//			bp.userDefined = this.userDefined;
//			return bp;
//		}
}
