protected PersistentQuantity doInvertSign(final PersistentQuantity q)
     throws PersistenceException {
   final Fraction amount = FractionManager.getTheFractionManager().invertSign(q.getAmount());
   final PersistentQuantity res = Quantity.createQuantity(amount, q.getUnit());
   getThis().getQuantities().add(res);
   return res;
 }
Esempio n. 2
0
  /**
   * Liest aus der Einheit einer Quantität die Referenz-Konfiguration aus und gibt das Ergebnis als
   * Map zurück. Atomare Einheiten werden als Map mit einem Eintrag zurückgeliefert, bei dem die
   * Einheit der Schlüssel ist und der Exponent "1" als Wert explizit gesetzt wird. Bei
   * zusammengesetzten Einheiten wird die refs-Assoziation ausgelesen und direkt in die Map
   * übertragen.
   *
   * @param arg
   * @return
   * @throws PersistenceException
   */
  private SummableHashMap<PersistentUnit> computeReferences(final PersistentQuantity arg)
      throws PersistenceException {
    return arg.getUnit()
        .accept(
            new AbsUnitReturnVisitor<SummableHashMap<PersistentUnit>>() {

              @Override
              public SummableHashMap<PersistentUnit> handleUnit(final PersistentUnit unit)
                  throws PersistenceException {
                final SummableHashMap<PersistentUnit> result =
                    new SummableHashMap<PersistentUnit>();
                result.getMap().put(unit, new Long(1));
                return result;
              }

              @Override
              public SummableHashMap<PersistentUnit> handleCompUnit(
                  final PersistentCompUnit compUnit) throws PersistenceException {
                final SummableHashMap<PersistentUnit> result =
                    new SummableHashMap<PersistentUnit>();
                final Iterator<PersistentReference> i = compUnit.getRefs().iterator();
                while (i.hasNext()) {
                  final PersistentReference current = i.next();
                  result.getMap().put(current.getRef(), current.getExponent());
                }
                return result;
              }
            });
  }
  @Override
  public void convert(final PersistentQuantity quantity, final PersistentAbsUnit unit)
      throws model.NotComputableException, PersistenceException {
    PersistentAbsQuantity typeToReplace = quantity;

    final Iterator<PersistentCompoundQuantity> i = quantity.inverseGetParts().iterator();
    if (i.hasNext()) {
      typeToReplace = i.next();
    }

    final Fraction resultAmount = getThis().convertAmount(quantity, unit);
    quantity.setAmount(resultAmount);
    quantity.setUnit(unit);
    final PersistentAbsQuantity newQuantity = getThis().concludeQuantity(quantity);

    replaceInQuantityList(typeToReplace, newQuantity, false);
  }
  /**
   * testet die Addition mit der gleichen Einheit.
   *
   * @throws PersistenceException
   * @throws NotComputableException
   */
  @Test
  public void testBasicCalculation_Same_Unit_add()
      throws PersistenceException, NotComputableException {
    final PersistentQuantity two = quanMan.createQuantity(meter, Fraction.parse("2"));
    final PersistentQuantity minusOne = quanMan.createQuantity(meter, Fraction.parse("-1"));
    final PersistentQuantity eight = quanMan.createQuantity(meter, Fraction.parse("8"));

    /*
     * Test 1: -1 +2 = 1
     */
    final PersistentQuantity test1_act = (PersistentQuantity) quanMan.add(minusOne, two);
    final PersistentQuantity test1_exp = quanMan.createQuantity(meter, Fraction.parse("1"));
    assertEquals(test1_exp.getUnit(), test1_act.getUnit());
    assertEquals(test1_exp.getAmount(), test1_act.getAmount());

    /*
     * Test 2: 8 + -1 = 7
     */
    final PersistentQuantity test2_act = (PersistentQuantity) quanMan.add(eight, minusOne);
    final PersistentQuantity test2_exp = quanMan.createQuantity(meter, Fraction.parse("7"));
    assertEquals(test1_exp.getUnit(), test1_act.getUnit());
    assertEquals(test1_exp.getAmount(), test1_act.getAmount());
  }
  /**
   * testet die Multiplikation mit der gleichen Einheit .
   *
   * @throws PersistenceException
   * @throws NotComputableException
   */
  @Test
  public void testBasicCalculation_Same_Unit_mul()
      throws PersistenceException, NotComputableException {
    final PersistentQuantity two = quanMan.createQuantity(meter, Fraction.parse("2"));
    final PersistentQuantity eight = quanMan.createQuantity(meter, Fraction.parse("8"));

    /*
     * Test 1: 2m * 8m = 16m^2 (simple)
     */
    final PersistentQuantity test1_act = (PersistentQuantity) quanMan.mul(two, eight);
    final PersistentQuantity test1_exp = quanMan.createQuantity(squareMeter, Fraction.parse("16"));
    assertEquals(test1_exp.getUnit(), test1_act.getUnit());
    assertEquals(test1_exp.getAmount(), test1_act.getAmount());
  }
 @Override
 public PersistentAbsUnitType handleQuantity(final PersistentQuantity quantity)
     throws PersistenceException {
   return quantity.getUnit().getType();
 }
  @Override
  public common.Fraction convertAmount(
      final PersistentQuantity quantity, final PersistentAbsUnit unit)
      throws model.NotComputableException, PersistenceException {
    if (!(quantity.getUnit().getType().equals(unit.getType()))) {
      throw new NotComputableException(ExceptionConstants.WRONG_UNIT_TYPE_FOR_CONVERSION);
    }
    final Fraction resultAmount =
        quantity
            .getUnit()
            .accept(
                new AbsUnitReturnExceptionVisitor<Fraction, NotComputableException>() {

                  @Override
                  public Fraction handleUnit(final PersistentUnit unit1)
                      throws PersistenceException, NotComputableException {
                    final PersistentConversion conversion1 = unit1.getMyConversion();
                    if (conversion1 == null) {
                      throw new NotComputableException(ExceptionConstants.NO_CONVERSION + unit1);
                    }
                    final Fraction amount = conversion1.convertToDefault(quantity.getAmount());
                    final PersistentConversion conversion2 =
                        ((PersistentUnit) unit).getMyConversion();
                    if (conversion2 == null) {
                      throw new NotComputableException(ExceptionConstants.NO_CONVERSION + unit);
                    }
                    return conversion2.convertFromDefault(amount);
                  }

                  @Override
                  public Fraction handleCompUnit(final PersistentCompUnit compUnit)
                      throws PersistenceException, NotComputableException {
                    Iterator<PersistentReference> iterator = compUnit.getRefs().iterator();
                    Fraction amount = quantity.getAmount();
                    while (iterator.hasNext()) {
                      final PersistentReference ref = iterator.next();
                      final PersistentConversion conversion = ref.getRef().getMyConversion();
                      if (conversion == null) {
                        throw new NotComputableException(
                            ExceptionConstants.NO_CONVERSION + ref.getRef());
                      }

                      amount = conversion.convertToDefault(amount);
                      try {
                        amount = amount.pow(ref.getExponent());
                      } catch (final Throwable e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                      }
                    }
                    iterator = ((PersistentCompUnit) unit).getRefs().iterator();
                    while (iterator.hasNext()) {
                      final PersistentReference ref = iterator.next();
                      final PersistentConversion conversion = ref.getRef().getMyConversion();
                      if (conversion == null) {
                        throw new NotComputableException(
                            ExceptionConstants.NO_CONVERSION + ref.getRef());
                      }

                      amount = conversion.convertFromDefault(amount);
                      try {
                        amount = amount.pow(ref.getExponent());
                      } catch (final Throwable e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                      }
                    }
                    return amount;
                  }
                });
    return resultAmount;
  }
  /**
   * testet die Division mit der gleichen Einheit .
   *
   * @throws PersistenceException
   * @throws NotComputableException
   */
  @Test
  public void testBasicCalculation_Same_Unit_div()
      throws PersistenceException, NotComputableException {
    final PersistentQuantity two = quanMan.createQuantity(meter, Fraction.parse("2"));
    final PersistentQuantity eight = quanMan.createQuantity(squareMeter, Fraction.parse("8"));

    final PersistentQuantity fiveMeter = quanMan.createQuantity(meter, Fraction.parse("5"));
    final PersistentQuantity twoSeconds = quanMan.createQuantity(second, Fraction.parse("2"));

    /*
     * Test 16m^2 / 8m = 2m (simple)
     */
    final PersistentQuantity test1_act = (PersistentQuantity) quanMan.div(eight, two);
    final PersistentQuantity test1_exp = quanMan.createQuantity(meter, Fraction.parse("4"));
    assertEquals(test1_exp.getUnit(), test1_act.getUnit());
    assertEquals(test1_exp.getAmount(), test1_act.getAmount());

    /*
     * Test 5m / 2s = 5/2 m/s
     */
    final PersistentQuantity test2_act = (PersistentQuantity) quanMan.div(fiveMeter, twoSeconds);
    final PersistentQuantity test2_exp =
        quanMan.createQuantity(meterPerSecond, Fraction.parse("5/2"));
    assertEquals(test2_exp.getUnit(), test2_act.getUnit());
    assertEquals(test2_exp.getAmount(), test2_act.getAmount());
  }