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  
19  import org.orekit.errors.OrekitException;
20  import org.orekit.errors.OrekitMessages;
21  
22  /** Converter between units.
23   * <p>
24   * Instances of this class are immutable.
25   * </p>
26   * @author Luc Maisonobe
27   * @since 11.0
28   */
29  public class UnitsConverter {
30  
31      /** Identity converter. */
32      public static final UnitsConverter IDENTITY =
33                      new UnitsConverter(Unit.ONE, Unit.ONE);
34  
35      /** Percents to units converter. */
36      public static final UnitsConverter PERCENTS_TO_UNIT =
37                      new UnitsConverter(Unit.PERCENT, Unit.ONE);
38  
39      /** Arcseconds to radians converter. */
40      public static final UnitsConverter ARC_SECONDS_TO_RADIANS =
41                      new UnitsConverter(Unit.parse("as"), Unit.RADIAN);
42  
43      /** Milli arcseconds to radians converter. */
44      public static final UnitsConverter MILLI_ARC_SECONDS_TO_RADIANS =
45                      new UnitsConverter(Unit.parse("mas"), Unit.RADIAN);
46  
47      /** Milli seconds to seconds converter. */
48      public static final UnitsConverter MILLI_SECONDS_TO_SECONDS =
49                      new UnitsConverter(Unit.parse("ms"), Unit.SECOND);
50  
51      /** Days to seconds converter. */
52      public static final UnitsConverter DAYS_TO_SECONDS =
53                      new UnitsConverter(Unit.DAY, Unit.SECOND);
54  
55      /** Kilometres to metres converter. */
56      public static final UnitsConverter KILOMETRES_TO_METRES =
57                      new UnitsConverter(Unit.KILOMETRE, Unit.METRE);
58  
59      /** Square kilometres to square metres converter. */
60      public static final UnitsConverter KILOMETRES_2_TO_METRES_2 =
61                      new UnitsConverter(Unit.parse("km²"), Unit.parse("m²"));
62  
63      /** km³/s² to m³/s² converter. */
64      public static final UnitsConverter KM3_P_S2_TO_M3_P_S2 =
65                      new UnitsConverter(Unit.parse("km³/s²"), Unit.parse("m³/s²"));
66  
67      /** Source unit. */
68      private final Unit from;
69  
70      /** Destination unit. */
71      private final Unit to;
72  
73      /** Conversion factor. */
74      private final double factor;
75  
76      /** Simple constructor.
77       * @param from source unit
78       * @param to destination unit
79       */
80      public UnitsConverter(final Unit from, final Unit to) {
81          this.from = from;
82          this.to   = to;
83          if (!from.sameDimension(to)) {
84              throw new OrekitException(OrekitMessages.INCOMPATIBLE_UNITS,
85                                        from.getName(), to.getName());
86          }
87          this.factor = from.getScale() / to.getScale();
88      }
89  
90      /** Get the source unit.
91       * @return source unit
92       */
93      public Unit getFrom() {
94          return from;
95      }
96  
97      /** Get the destination unit.
98       * @return destination unit
99       */
100     public Unit getTo() {
101         return to;
102     }
103 
104     /** Convert a value.
105      * @param value value in the {@link #getFrom() source unit}
106      * @return value converted in the {@link #getTo() destination unit}
107      */
108     public double convert(final double value) {
109         return factor * value;
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public String toString() {
115         return from.getName() + " → " + to.getName();
116     }
117 
118 }