UnitsConverter.java

  1. /* Copyright 2002-2022 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.     /** Days to seconds converter. */
  44.     public static final UnitsConverter DAYS_TO_SECONDS =
  45.                     new UnitsConverter(Unit.DAY, Unit.SECOND);

  46.     /** Kilometres to metres converter. */
  47.     public static final UnitsConverter KILOMETRES_TO_METRES =
  48.                     new UnitsConverter(Unit.KILOMETRE, Unit.METRE);

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

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

  55.     /** Source unit. */
  56.     private final Unit from;

  57.     /** Destination unit. */
  58.     private final Unit to;

  59.     /** Conversion factor. */
  60.     private final double factor;

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

  74.     /** Get the source unit.
  75.      * @return source unit
  76.      */
  77.     public Unit getFrom() {
  78.         return from;
  79.     }

  80.     /** Get the destination unit.
  81.      * @return destination unit
  82.      */
  83.     public Unit getTo() {
  84.         return to;
  85.     }

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

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public String toString() {
  96.         return from.getName() + " → " + to.getName();
  97.     }

  98. }