1   /* Copyright 2002-2021 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  
19  import java.util.Arrays;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.orekit.errors.OrekitException;
25  import org.orekit.errors.OrekitMessages;
26  
27  /** {@link Unit} with a {@link Prefix}.
28   * @author Luc Maisonobe
29   * @since 11.0
30   */
31  class PrefixedUnit extends Unit {
32  
33      /** Serializable UID. */
34      private static final long serialVersionUID = 20210407L;
35  
36      /** Allowed units with SI prefixes, with various aliases for angles, year, sfu, and tecu. */
37      private static final Map<String, PrefixedUnit> ALLOWED;
38  
39      static {
40          final List<Unit> base = Arrays.asList(Unit.SECOND,
41                                                Unit.MINUTE,
42                                                Unit.HOUR,
43                                                Unit.DAY,
44                                                Unit.YEAR,
45                                                Unit.YEAR.alias("yr"),
46                                                Unit.HERTZ,
47                                                Unit.METRE,
48                                                Unit.GRAM, // only case were we must use a derived unit
49                                                Unit.AMPERE,
50                                                Unit.RADIAN,
51                                                Unit.DEGREE,
52                                                Unit.DEGREE.alias("◦"),
53                                                Unit.DEGREE.alias("deg"),
54                                                Unit.ARC_MINUTE,
55                                                Unit.ARC_MINUTE.alias("'"),
56                                                Unit.ARC_SECOND,
57                                                Unit.ARC_SECOND.alias("''"),
58                                                Unit.ARC_SECOND.alias("\""),
59                                                Unit.ARC_SECOND.alias("as"), // must be after second to override atto-seconds
60                                                Unit.REVOLUTION,
61                                                Unit.NEWTON,
62                                                Unit.PASCAL, // must be after year to override peta-years
63                                                Unit.BAR,
64                                                Unit.JOULE,
65                                                Unit.WATT,
66                                                Unit.COULOMB,
67                                                Unit.VOLT,
68                                                Unit.OHM,
69                                                Unit.TESLA,
70                                                Unit.SOLAR_FLUX_UNIT,
71                                                Unit.SOLAR_FLUX_UNIT.alias("SFU"),
72                                                Unit.SOLAR_FLUX_UNIT.alias("sfu"),
73                                                Unit.TOTAL_ELECTRON_CONTENT_UNIT,
74                                                Unit.TOTAL_ELECTRON_CONTENT_UNIT.alias("tecu"));
75          ALLOWED = new HashMap<>(base.size() * Prefix.values().length);
76          for (final Unit unit : base) {
77              ALLOWED.put(unit.getName(), new PrefixedUnit(null, unit));
78              for (final Prefix prefix : Prefix.values()) {
79                  final PrefixedUnit pu = new PrefixedUnit(prefix, unit);
80                  ALLOWED.put(pu.getName(), pu);
81              }
82          }
83  
84          // units that don't accept any prefix
85          for (final Unit noPrefix : Arrays.asList(Unit.PERCENT, Unit.ONE, Unit.ONE.alias("#"))) {
86              ALLOWED.put(noPrefix.getName(), new PrefixedUnit(null, noPrefix));
87          }
88  
89      }
90  
91      /** Simple constructor.
92       * @param prefix SI prefix (may be null)
93       * @param unit base unit
94       */
95      PrefixedUnit(final Prefix prefix, final Unit unit) {
96          super((prefix == null) ? unit.getName()  : (prefix.getSymbol() + unit.getName()),
97                (prefix == null) ? unit.getScale() : (prefix.getFactor() * unit.getScale()),
98                unit.getMass(), unit.getLength(), unit.getTime(), unit.getCurrent(), unit.getAngle());
99      }
100 
101     /** Get one of the allowed prefixed unit.
102      * @param name name of the prefixed unit
103      * @return prefixed unit with that name
104      */
105     public static PrefixedUnit valueOf(final String name) {
106         final PrefixedUnit prefixedUnit = ALLOWED.get(name);
107         if (prefixedUnit == null) {
108             throw new OrekitException(OrekitMessages.UNKNOWN_UNIT, name);
109         }
110         return prefixedUnit;
111     }
112 
113 }