javax.measure mising Символ объекта и неправильное представление самого объекта - PullRequest
0 голосов
/ 19 июня 2020

У меня есть простая реализация, если javax.measure преобразует некоторые единицы длины. Преобразование значений правильное, но я получаю странный результат для представляющих единиц:

Meter
Value: 1.0
Unit: m
Symbol: m
Name: null
System Unit: null
Dimension: [L]
KM
Value: 0.001
Unit: [m?]
Symbol: null
Name: null
System Unit: null
Dimension: [L]
Foot
Value: 3.2808398950131235
Unit: (m*3048.0)/10000.0
Symbol: null
Name: null
System Unit: null
Dimension: [L]

Вместо Unit: [m?] Я бы предложил Unit: [km] и так далее

Есть идеи или подсказки, почему и что не так? Большое спасибо

Я использую следующие зависимости maven:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <javax.measur.version>1.0</javax.measur.version>
    <tec.units.version>1.0.3</tec.units.version>
    <si.uom.version>1.0</si.uom.version>
  </properties>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>


    <dependency>
      <groupId>javax.measure</groupId>
      <artifactId>unit-api</artifactId>
      <version>${javax.measur.version}</version>
    </dependency>

    <dependency>
      <groupId>tec.units</groupId>
      <artifactId>unit-ri</artifactId>
      <version>${tec.units.version}</version>
    </dependency>
 </dependencies>

У меня простой класс LengthCalculation. Я также использую перечисление для проверки моих допустимых единиц:

package org.example;

import si.uom.SI;
import tec.units.ri.quantity.Quantities;
import tec.units.ri.unit.MetricPrefix;

import javax.measure.*;
import javax.measure.quantity.Length;
import java.util.Map;

public class LengthCalculator {


  private final Unit<Length> METER = SI.METRE;
  private final Unit<Length> KM = MetricPrefix.KILO(METER);
  private final Unit<Length> MILE = METER.multiply(1609344).divide(1000);
  private final Unit<Length> FOOT = METER.multiply(3048).divide(10000);


  public enum ValidLengthUnits {
    METER(1), KM(2), MILE(3), FOOT(4);

    private final int number;

    ValidLengthUnits(int number) {
      this.number = number;
    }

    public int getNumber() {
      return number;
    }

  }

  private Quantity<Length> length;

  //CONSTRUCTOR
  public LengthCalculator() {
    this.length = Quantities.getQuantity(1, METER);
  }

  private void setLength(Quantity<Length> length) {
    this.length = length;
  }


  public void convertUnit(ValidLengthUnits value) {
    switch (value.getNumber()) {
      case 1:
        setLength(this.length.to(METER));
        break;
      case 2:
        setLength(this.length.to(KM));
        break;
      case 3:
        setLength(this.length.to(MILE));
        break;
      case 4:
        setLength(this.length.to(FOOT));
        break;
    }
  }



  public Double getLengthValue() {
    return (length.getValue().doubleValue());
  }

  public String getUnit() {
    return length.getUnit().toString();
  }

  public String getUnitSymbol() {
    return length.getUnit().getSymbol();
  }

  public String getUnitName() { return length.getUnit().getName(); }

  public String getUniSystemUnit() { return length.getUnit().getSystemUnit().getName(); }

  public Dimension getUniDimension() { return length.getUnit().getDimension();}


}

В основном классе несколько простых инициализаций и базовых c преобразований единиц:

package org.example;


public class App 
{
    public static void main( String[] args ) {

        LengthCalculator staticLength;
        staticLength = new LengthCalculator();
        System.out.println("Meter");
        System.out.println("Value: " + staticLength.getLengthValue());
        System.out.println("Unit: " + staticLength.getUnit());
        System.out.println("Symbol: " + staticLength.getUnitSymbol());
        System.out.println("Name: " + staticLength.getUnitName());
        System.out.println("System Unit: " + staticLength.getUniSystemUnit());
        System.out.println("Dimension: " + staticLength.getUniDimension());



        System.out.println("KM");
        staticLength.convertUnit(LengthCalculator.ValidLengthUnits.KM);
        System.out.println("Value: " + staticLength.getLengthValue());
        System.out.println("Unit: " + staticLength.getUnit());
        System.out.println("Symbol: " + staticLength.getUnitSymbol());
        System.out.println("Name: " + staticLength.getUnitName());
        System.out.println("System Unit: " + staticLength.getUniSystemUnit());
        System.out.println("Dimension: " + staticLength.getUniDimension());



        System.out.println("Foot");
        staticLength.convertUnit(LengthCalculator.ValidLengthUnits.METER);
        staticLength.convertUnit(LengthCalculator.ValidLengthUnits.FOOT);
        System.out.println("Value: " + staticLength.getLengthValue());
        System.out.println("Unit: " + staticLength.getUnit());
        System.out.println("Symbol: " + staticLength.getUnitSymbol());
        System.out.println("Name: " + staticLength.getUnitName());
        System.out.println("System Unit: " + staticLength.getUniSystemUnit());
        System.out.println("Dimension: " + staticLength.getUniDimension());

    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...