UnitsConverter.java

  1. /* Copyright 2002-2024 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.utils.units;

  18. import org.orekit.errors.OrekitException;
  19. import org.orekit.errors.OrekitMessages;

  20. /** Converter between units.
  21.  * <p>
  22.  * Instances of this class are immutable.
  23.  * </p>
  24.  * @author Luc Maisonobe
  25.  * @since 11.0
  26.  */
  27. public class UnitsConverter {

  28.     /** Identity converter. */
  29.     public static final UnitsConverter IDENTITY =
  30.                     new UnitsConverter(Unit.ONE, Unit.ONE);

  31.     /** Percents to units converter. */
  32.     public static final UnitsConverter PERCENTS_TO_UNIT =
  33.                     new UnitsConverter(Unit.PERCENT, Unit.ONE);

  34.     /** Arcseconds to radians converter. */
  35.     public static final UnitsConverter ARC_SECONDS_TO_RADIANS =
  36.                     new UnitsConverter(Unit.parse("as"), Unit.RADIAN);

  37.     /** Milli arcseconds to radians converter. */
  38.     public static final UnitsConverter MILLI_ARC_SECONDS_TO_RADIANS =
  39.                     new UnitsConverter(Unit.parse("mas"), Unit.RADIAN);

  40.     /** Milli seconds to seconds converter. */
  41.     public static final UnitsConverter MILLI_SECONDS_TO_SECONDS =
  42.                     new UnitsConverter(Unit.parse("ms"), Unit.SECOND);

  43.     /** Nano Teslas to Tesla converter. */
  44.     public static final UnitsConverter NANO_TESLAS_TO_TESLAS =
  45.                     new UnitsConverter(Unit.parse("nT"), Unit.TESLA);

  46.     /** Days to seconds converter. */
  47.     public static final UnitsConverter DAYS_TO_SECONDS =
  48.                     new UnitsConverter(Unit.DAY, Unit.SECOND);

  49.     /** Kilometres to metres converter. */
  50.     public static final UnitsConverter KILOMETRES_TO_METRES =
  51.                     new UnitsConverter(Unit.KILOMETRE, Unit.METRE);

  52.     /** Square kilometres to square metres converter. */
  53.     public static final UnitsConverter KILOMETRES_2_TO_METRES_2 =
  54.                     new UnitsConverter(Unit.parse("km²"), Unit.parse("m²"));

  55.     /** km³/s² to m³/s² converter. */
  56.     public static final UnitsConverter KM3_P_S2_TO_M3_P_S2 =
  57.                     new UnitsConverter(Unit.parse("km³/s²"), Unit.parse("m³/s²"));

  58.     /** Source unit. */
  59.     private final Unit from;

  60.     /** Destination unit. */
  61.     private final Unit to;

  62.     /** Conversion factor. */
  63.     private final double factor;

  64.     /** Simple constructor.
  65.      * @param from source unit
  66.      * @param to destination unit
  67.      */
  68.     public UnitsConverter(final Unit from, final Unit to) {
  69.         this.from = from;
  70.         this.to   = to;
  71.         if (!from.sameDimension(to)) {
  72.             throw new OrekitException(OrekitMessages.INCOMPATIBLE_UNITS,
  73.                                       from.getName(), to.getName());
  74.         }
  75.         this.factor = from.getScale() / to.getScale();
  76.     }

  77.     /** Get the source unit.
  78.      * @return source unit
  79.      */
  80.     public Unit getFrom() {
  81.         return from;
  82.     }

  83.     /** Get the destination unit.
  84.      * @return destination unit
  85.      */
  86.     public Unit getTo() {
  87.         return to;
  88.     }

  89.     /** Convert a value.
  90.      * @param value value in the {@link #getFrom() source unit}
  91.      * @return value converted in the {@link #getTo() destination unit}
  92.      */
  93.     public double convert(final double value) {
  94.         return factor * value;
  95.     }

  96.     /** {@inheritDoc} */
  97.     @Override
  98.     public String toString() {
  99.         return from.getName() + " → " + to.getName();
  100.     }

  101. }