Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: current
    • Fix Version/s: Version 6.0
    • Component/s: www
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: All

    • Issuezilla Id:
      111

      Description

      Transformations of ProductUnits are not formatted using prefixes but
      multiplicative factors. E.g. after applying the patch from issue #110 it should
      be possible to format KILO(DataRate.UNIT) as "kbit/s"

      — example code —
      import static javax.measure.unit.SI.BIT;
      import static javax.measure.unit.SI.KILO;

      import javax.measure.quantity.DataRate;
      import javax.measure.unit.ProductUnit;
      import javax.measure.unit.SI;
      import javax.measure.unit.Unit;

      public class DataRateTest {

      public static void main(String[] args)

      { // unexpected format Unit<DataRate> dataRateUnit=KILO(DataRate.UNIT); System.out.println("KILO(BIT/SECOND): "+dataRateUnit); // works dataRateUnit=new ProductUnit<DataRate>(KILO(BIT).divide(SI.SECOND)); System.out.println("(KILOBIT)/SECOND: "+dataRateUnit); // works, uses 2nd form dataRateUnit=Unit.valueOf("Mbit/s").asType(DataRate.class); System.out.println("\"Mbit/s\": "+dataRateUnit); }

      }
      — end example code —

      — current result —
      KILO(BIT/SECOND): (bit/s)*1000
      (KILOBIT)/SECOND: kbit/s
      "Mbit/s": Mbit/s
      — end current result —

      — expected result —
      KILO(BIT/SECOND): kbit/s
      (KILOBIT)/SECOND: kbit/s
      "Mbit/s": Mbit/s
      — end expected result —

      TransformedUnits should be formatted using prefixes if the ProductUnit parent is
      not too complex.

      — simple but not recommended solution —
      Index: src/javax/measure/unit/Unit.java
      ===================================================================
      RCS file: /cvs/jscience/src/javax/measure/unit/Unit.java,v
      retrieving revision 1.2
      diff -u -r1.2 Unit.java
      — src/javax/measure/unit/Unit.java 3 Oct 2007 07:14:42 -0000 1.2
      +++ src/javax/measure/unit/Unit.java 25 Nov 2008 10:06:27 -0000
      @@ -336,6 +336,10 @@
      }
      if (operation == UnitConverter.IDENTITY)
      return this;
      + if(this instanceof ProductUnit)

      { + ProductUnit<Q> pu=(ProductUnit<Q>) this; + if(pu.transformFirstTerm(operation)) return pu; + }

      return new TransformedUnit<Q>(this, operation);
      }

      Index: src/javax/measure/unit/ProductUnit.java
      ===================================================================
      RCS file: /cvs/jscience/src/javax/measure/unit/ProductUnit.java,v
      retrieving revision 1.2
      diff -u -r1.2 ProductUnit.java
      — src/javax/measure/unit/ProductUnit.java 3 Oct 2007 07:14:41 -0000 1.2
      +++ src/javax/measure/unit/ProductUnit.java 25 Nov 2008 10:06:27 -0000
      @@ -467,5 +467,20 @@
      private static final long serialVersionUID = 1L;
      }

      + /**
      + * If possible, transforms the first term of the product unit.
      + * @param operation the converter from the transformed unit to this unit
      + * @return <code>true</code> if the transformation was performed.
      + */
      + boolean transformFirstTerm(UnitConverter operation) {
      + if(_elements.length==0) return false;
      + Element e=_elements[0];
      + if(e.getPow()==1 && e.getRoot()==1)

      { // only perform for linear units to have clear formatting + _elements[0]=new Element(e.getUnit().transform(operation),1,1); + return true; + }

      + return false;
      + }
      +
      private static final long serialVersionUID = 1L;
      }
      \ No newline at end of file
      — end simple but not recommended solution —

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            jscience-issues
            Reporter:
            helgeboehme
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: