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

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

  81.     }

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

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

  102. }