PrefixedUnit.java

  1. /* Copyright 2002-2025 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 java.util.Arrays;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;

  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitMessages;

  24. /** {@link Unit} with a {@link Prefix}.
  25.  * @author Luc Maisonobe
  26.  * @since 11.0
  27.  */
  28. class PrefixedUnit extends Unit {

  29.     /** Serializable UID. */
  30.     private static final long serialVersionUID = 20210407L;

  31.     /** Allowed units with SI prefixes, with various aliases for angles, year, sfu, and tecu. */
  32.     private static final Map<String, PrefixedUnit> ALLOWED;

  33.     static {
  34.         final List<Unit> base = Arrays.asList(Unit.SECOND,
  35.                                               Unit.MINUTE,
  36.                                               Unit.HOUR,
  37.                                               Unit.DAY,
  38.                                               Unit.DAY.alias("day"),
  39.                                               Unit.YEAR,
  40.                                               Unit.YEAR.alias("yr"),
  41.                                               Unit.HERTZ,
  42.                                               Unit.METRE,
  43.                                               Unit.GRAM, // only case were we must use a derived unit
  44.                                               Unit.AMPERE,
  45.                                               Unit.RADIAN,
  46.                                               Unit.DEGREE,
  47.                                               Unit.DEGREE.alias("◦"),
  48.                                               Unit.DEGREE.alias("deg"),
  49.                                               Unit.ARC_MINUTE,
  50.                                               Unit.ARC_MINUTE.alias("'"),
  51.                                               Unit.ARC_SECOND,
  52.                                               Unit.ARC_SECOND.alias("''"),
  53.                                               Unit.ARC_SECOND.alias("\""),
  54.                                               Unit.ARC_SECOND.alias("as"), // must be after second to override atto-seconds
  55.                                               Unit.REVOLUTION,
  56.                                               Unit.NEWTON,
  57.                                               Unit.PASCAL, // must be after year to override peta-years
  58.                                               Unit.BAR,
  59.                                               Unit.JOULE,
  60.                                               Unit.WATT,
  61.                                               Unit.COULOMB,
  62.                                               Unit.VOLT,
  63.                                               Unit.OHM,
  64.                                               Unit.TESLA,
  65.                                               Unit.SOLAR_FLUX_UNIT,
  66.                                               Unit.SOLAR_FLUX_UNIT.alias("SFU"),
  67.                                               Unit.SOLAR_FLUX_UNIT.alias("sfu"),
  68.                                               Unit.TOTAL_ELECTRON_CONTENT_UNIT,
  69.                                               Unit.TOTAL_ELECTRON_CONTENT_UNIT.alias("tecu"),
  70.                                               Unit.EARTH_RADII,
  71.                                               Unit.CYCLE);
  72.         ALLOWED = new HashMap<>(base.size() * Prefix.values().length);
  73.         for (final Unit unit : base) {
  74.             ALLOWED.put(unit.getName(), new PrefixedUnit(null, unit));
  75.             for (final Prefix prefix : Prefix.values()) {
  76.                 final PrefixedUnit pu = new PrefixedUnit(prefix, unit);
  77.                 ALLOWED.put(pu.getName(), pu);
  78.             }
  79.         }

  80.         // units that don't accept any prefix
  81.         for (final Unit noPrefix : Arrays.asList(Unit.PERCENT, Unit.ONE, Unit.ONE.alias("#"))) {
  82.             ALLOWED.put(noPrefix.getName(), new PrefixedUnit(null, noPrefix));
  83.         }

  84.     }

  85.     /** Simple constructor.
  86.      * @param prefix SI prefix (may be null)
  87.      * @param unit base unit
  88.      */
  89.     PrefixedUnit(final Prefix prefix, final Unit unit) {
  90.         super((prefix == null) ? unit.getName()  : (prefix.getSymbol() + unit.getName()),
  91.               (prefix == null) ? unit.getScale() : (prefix.getFactor() * unit.getScale()),
  92.               unit.getMass(), unit.getLength(), unit.getTime(), unit.getCurrent(), unit.getAngle());
  93.     }

  94.     /** Get one of the allowed prefixed unit.
  95.      * @param name name of the prefixed unit
  96.      * @return prefixed unit with that name
  97.      */
  98.     public static PrefixedUnit valueOf(final String name) {
  99.         final PrefixedUnit prefixedUnit = ALLOWED.get(name);
  100.         if (prefixedUnit == null) {
  101.             throw new OrekitException(OrekitMessages.UNKNOWN_UNIT, name);
  102.         }
  103.         return prefixedUnit;
  104.     }

  105. }