示例#1
0
  /**
   * Convert a measure from one units to another. The conversion is between absolute the units
   * should be one of the absolute units(CM, IN, MM, PT, PC).
   *
   * @param measure the numeric measure of the dimension.
   * @param fromUnits unit of the measure, it must be one of the absolute unit.
   * @param targetUnits the desired units, it must be one of the absolute unit.
   * @return <code>DimensionValue</code> in the target unit.
   */
  public static DimensionValue convertTo(double measure, String fromUnits, String targetUnits) {

    if (targetUnits.equalsIgnoreCase(fromUnits)) return new DimensionValue(measure, fromUnits);

    double targetMeasure = 0.0;

    if (DesignChoiceConstants.UNITS_IN.equalsIgnoreCase(targetUnits)) {
      if (DesignChoiceConstants.UNITS_CM.equalsIgnoreCase(fromUnits))
        targetMeasure = measure / CM_PER_INCH;
      else if (DesignChoiceConstants.UNITS_MM.equalsIgnoreCase(fromUnits))
        targetMeasure = measure / CM_PER_INCH / 10;
      else if (DesignChoiceConstants.UNITS_PT.equalsIgnoreCase(fromUnits))
        targetMeasure = measure / POINTS_PER_INCH;
      else if (DesignChoiceConstants.UNITS_PC.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * POINTS_PER_PICA / POINTS_PER_INCH;
      else throw new IllegalArgumentException("\"fromUnits\"" + ILLEGAL_UNIT); // $NON-NLS-1$
    } else if (DesignChoiceConstants.UNITS_CM.equalsIgnoreCase(targetUnits)) {
      if (DesignChoiceConstants.UNITS_IN.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * CM_PER_INCH;
      else if (DesignChoiceConstants.UNITS_MM.equalsIgnoreCase(fromUnits))
        targetMeasure = measure / 10;
      else if (DesignChoiceConstants.UNITS_PT.equalsIgnoreCase(fromUnits))
        targetMeasure = measure / POINTS_PER_CM;
      else if (DesignChoiceConstants.UNITS_PC.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * POINTS_PER_PICA / POINTS_PER_CM;
      else throw new IllegalArgumentException("\"fromUnits\"" + ILLEGAL_UNIT); // $NON-NLS-1$
    } else if (DesignChoiceConstants.UNITS_MM.equalsIgnoreCase(targetUnits)) {
      if (DesignChoiceConstants.UNITS_IN.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * CM_PER_INCH * 10;
      else if (DesignChoiceConstants.UNITS_CM.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * 10;
      else if (DesignChoiceConstants.UNITS_PT.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * 10 / POINTS_PER_CM;
      else if (DesignChoiceConstants.UNITS_PC.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * POINTS_PER_PICA * 10 / POINTS_PER_CM;
      else throw new IllegalArgumentException("\"fromUnits\"" + ILLEGAL_UNIT); // $NON-NLS-1$
    } else if (DesignChoiceConstants.UNITS_PT.equalsIgnoreCase(targetUnits)) {
      if (DesignChoiceConstants.UNITS_IN.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * POINTS_PER_INCH;
      else if (DesignChoiceConstants.UNITS_CM.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * POINTS_PER_CM;
      else if (DesignChoiceConstants.UNITS_MM.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * POINTS_PER_CM / 10;
      else if (DesignChoiceConstants.UNITS_PC.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * POINTS_PER_PICA;
      else throw new IllegalArgumentException("\"fromUnits\"" + ILLEGAL_UNIT); // $NON-NLS-1$
    } else if (DesignChoiceConstants.UNITS_PC.equalsIgnoreCase(targetUnits)) {
      if (DesignChoiceConstants.UNITS_IN.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * POINTS_PER_INCH / POINTS_PER_PICA;
      else if (DesignChoiceConstants.UNITS_CM.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * POINTS_PER_CM / POINTS_PER_PICA;
      else if (DesignChoiceConstants.UNITS_MM.equalsIgnoreCase(fromUnits))
        targetMeasure = measure * POINTS_PER_CM / 10 / POINTS_PER_PICA;
      else if (DesignChoiceConstants.UNITS_PT.equalsIgnoreCase(fromUnits))
        targetMeasure = measure / POINTS_PER_PICA;
      else throw new IllegalArgumentException("\"fromUnits\"" + ILLEGAL_UNIT); // $NON-NLS-1$
    } else throw new IllegalArgumentException("\"targetUnits\"" + ILLEGAL_UNIT); // $NON-NLS-1$

    return new DimensionValue(targetMeasure, targetUnits);
  }
示例#2
0
 /**
  * Return if the given unit is an absolute unit or not. The following units defined in <code>
  * DesignChoiceConstants</code> are considered as absolute:
  *
  * <ul>
  *   <li>UNITS_IN
  *   <li>UNITS_CM
  *   <li>UNITS_MM
  *   <li>UNITS_PT
  *   <li>UNITS_PC
  * </ul>
  *
  * @param unit a given unit.
  * @return <code>true</code> if the unit is an absolute unit like cm, in, mm, pt and pc. Return
  *     <code>false</code> if the unit is not an absolute unit.( it can be an relative unit like
  *     "%", or even an unrecognized unit. )
  */
 public static final boolean isAbsoluteUnit(String unit) {
   return DesignChoiceConstants.UNITS_IN.equalsIgnoreCase(unit)
       || DesignChoiceConstants.UNITS_CM.equalsIgnoreCase(unit)
       || DesignChoiceConstants.UNITS_MM.equalsIgnoreCase(unit)
       || DesignChoiceConstants.UNITS_PT.equalsIgnoreCase(unit)
       || DesignChoiceConstants.UNITS_PC.equalsIgnoreCase(unit);
 }