package net.pizey.jclu;

/** 
 * A tuple of a real and a unit to represent a length. 
 * @author timp
 * @since 2013-08-13
 */
public class Length {

  private Double real;
  private LengthUnit lengthUnit;

  public Length(Double real, String unitSymbol) {
    this.setReal(real);
    this.setLengthUnit(LengthUnit.fromSymbol(unitSymbol));
  }

  public Double getReal() {
    return real;
  }

  public void setReal(double real) {
    this.real = new Double(real);
  }

  public String getUnitSymbol() {
    return lengthUnit.getSymbol();
  }

  public LengthUnit getLengthUnit() {
    return lengthUnit;
  }

  public void setLengthUnit(LengthUnit lengthUnit) {
    this.lengthUnit = lengthUnit;
  }
  
  private static String prettyDouble(Double d){
    String out = d.toString();
    return out.replaceFirst("\\.0", "");
  }
  @Override
  public String toString() { 
    return prettyDouble(real) + " " + lengthUnit.getSymbol(); 
  }
  
  /** Mint a Length using for example "9 in". */
  public Length fromString(String stringLength){
    String[] bits = stringLength.trim().split(" ");
    if (bits.length != 2)
      throw new IllegalArgumentException("Cannot parse " + stringLength);
    return new Length(new Double(bits[0]), bits[1]);      
  }
  
  /** Convert Length of given unit. */
  public Length toUnit(String symbol){
    // autoboxed
    return new Length(
        (real * lengthUnit.getMetres()) / LengthUnit.fromSymbol(symbol).getMetres(), 
        symbol);
  }
  
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
        + lengthUnit.hashCode();
    result = prime * result + real.hashCode();
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Length other = (Length) obj;
    if (!lengthUnit.equals(other.lengthUnit))
      return false;
    if (!real.equals(other.real))
      return false;
    return true;
  }

}

@@ -906,347 +906,30 @@
 %22 + 
-lengthUnit.getSymbol(); %0A  %7D%0A  %0A  /** Mint a Length using for example %229 in%22. */%0A  public Length fromString(String stringLength)%7B%0A    String%5B%5D bits = stringLength.trim().split(%22 %22);%0A    if (bits.length != 2)%0A      throw new IllegalArgumentException(%22Cannot parse %22 + stringLength);%0A    return new Length(new Double(bits%5B0%5D), bits%5B1%5D);     
+getLengthUnitSymbol();
  %0A  
@@ -1064,16 +1064,24 @@
    (
-r
+getR
 eal
+()
  * 
-l
+getL
 engt
@@ -1081,24 +1081,26 @@
 etLengthUnit
+()
 .getMetres()
@@ -1175,586 +1175,6 @@
 %0A  %0A
-  @Override%0A  public int hashCode() %7B%0A    final int prime = 31;%0A    int result = 1;%0A    result = prime * result%0A        + lengthUnit.hashCode();%0A    result = prime * result + real.hashCode();%0A    return result;%0A  %7D%0A%0A  @Override%0A  public boolean equals(Object obj) %7B%0A    if (this == obj)%0A      return true;%0A    if (obj == null)%0A      return false;%0A    if (getClass() != obj.getClass())%0A      return false;%0A    Length other = (Length) obj;%0A    if (!lengthUnit.equals(other.lengthUnit))%0A      return false;%0A    if (!real.equals(other.real))%0A      return false;%0A    return true;%0A  %7D%0A%0A
 %7D%0A

Remove unneeded code