ErrorRange.java [src/java/d/soils/wwe02_wepssoilinput] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2017, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package d.soils.wwe02_wepssoilinput;

import d.util.Utils;

/**
 *
 * @author maxerdwien
 */
public class ErrorRange {

  String title;
  double loValue;
  double hiValue;


  public String getTitle() {
    return title;
  }


  public void setTitle(String title) {
    this.title = title;
  }


  public double getLoValue() {
    return loValue;
  }


  public double getHiValue() {
    return hiValue;
  }


  /**
   * @param lo
   */
  public void setLowValue(double lo) {
    loValue = lo;
  }


  /**
   * @param high
   */
  public void setHighValue(double high) {
    hiValue = high;
  }


  /**
   * @param ti
   * @param lo
   * @param hi
   */
  /* entry for integer or double */
  public ErrorRange(String ti, double lo, double hi) {
    title = ti;
    loValue = lo;
    hiValue = hi;
  }


  /**
   * @param ti
   */
  /* entry for string */
  public ErrorRange(String ti) {
    title = ti;
    loValue = 0;
    hiValue = -1;
  }


  /**
   *
   * @param label
   * @return
   */
  public int checkRange(String label) {
    double range = 0.0;

    if (loValue == 0 && hiValue == -1) {		// it's a string
      if (label == null)
        return -3;

      if (label.trim().length() > 0)
        return 0;

      return -3;
    }
////System.out.println("checkRange: " + title + " " + label + " " + loValue + " " + hiValue);
    if (label == null)
      return -2;

    try {										// it's a number
      range = Double.parseDouble(label);
      if (range < loValue || range > hiValue)
        return (range < loValue) ? -1 : 1;

    } catch (NumberFormatException e) {
      return -2;
    }
    return 0;
  }


  /**
   *
   * @param value
   * @return
   */
  public int checkRange(double value) {
    if (Double.isNaN(value)) 
      return -1;
    
    if (value < loValue || value > hiValue) 
      return (value < loValue) ? -1 : 1;
    
    return 0;
  }


  /**
   *
   * @param idx
   * @return
   */
  public String adjValue(int idx) {
    if (idx == -3) 
      return "no value given";
    
    double rtn = 0;
    switch (idx) {
      case -1:
        rtn = loValue;
        break;
      case 1:
        rtn = hiValue;
        break;
      case -2:
        rtn = (loValue + hiValue) / 2;
        break;
    }
    int numdigits = (int) -(Math.log(rtn) / Math.log(10.0)) + 4;
    if (numdigits < 0) 
      numdigits = 0;
    
    return Utils.formatDouble(rtn, numdigits);
  }


  /**
   *
   * @param inpval
   * @return
   */
  public double adjValue(double inpval) {
    if (inpval < loValue) 
      return loValue;
    
    if (inpval > hiValue) 
      return hiValue;
    
    return inpval;
  }


  /**
   *
   * @return
   */
  public String getToolTipText() {
    if (loValue > hiValue) {
      return title;
    } else {
      return title + " " + loValue + " - " + hiValue;
    }
  }

}