ParsedUnitsBehavior.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.files.ccsds.ndm;

  18. import org.hipparchus.util.Precision;
  19. import org.orekit.errors.OrekitException;
  20. import org.orekit.errors.OrekitMessages;
  21. import org.orekit.utils.units.Unit;

  22. /** Behavior adopted for units that have been parsed from a CCSDS message.
  23.  * @author Luc Maisonobe
  24.  * @since 11.0
  25.  */
  26. public enum ParsedUnitsBehavior {

  27.     /** Ignore parsed units, just relying on CCSDS standard.
  28.      * <p>
  29.      * When this behavior is selected having a unit parsed as second
  30.      * when CCSDS mandates kilometer will be accepted.
  31.      * </p>
  32.      */
  33.     IGNORE_PARSED {
  34.         /** {@inheritDoc} */
  35.         @Override
  36.         public Unit select(final Unit message, final Unit standard) {
  37.             return standard;
  38.         }
  39.     },

  40.     /** Allow compatible units, performing conversion.
  41.      * <p>
  42.      * When this behavior is selected having a unit parsed as second
  43.      * when CCSDS mandates kilometer will be refused, but having a unit
  44.      * parsed as meter will be accepted, with proper conversion performed.
  45.      * Missing units (i.e. units parsed as {@link Unit#NONE}) are considered
  46.      * to be standard.
  47.      * </p>
  48.      */
  49.     CONVERT_COMPATIBLE {
  50.         /** {@inheritDoc} */
  51.         @Override
  52.         public Unit select(final Unit message, final Unit standard) {
  53.             if (message == Unit.NONE) {
  54.                 return standard;
  55.             } else if (message.sameDimension(standard)) {
  56.                 return message;
  57.             } else {
  58.                 throw new OrekitException(OrekitMessages.INCOMPATIBLE_UNITS,
  59.                                           message.getName(), standard.getName());
  60.             }
  61.         }
  62.     },

  63.     /** Enforce strict compliance with CCSDS standard.
  64.      * <p>
  65.      * When this behavior is selected having a unit parsed as second
  66.      * or as meter when CCSDS mandates kilometer will both be refused.
  67.      * Missing units (i.e. units parsed as {@link Unit#NONE}) are considered
  68.      * to be standard.
  69.      * </p>
  70.      */
  71.     STRICT_COMPLIANCE {
  72.         /** {@inheritDoc} */
  73.         @Override
  74.         public Unit select(final Unit message, final Unit standard) {
  75.             if (message == Unit.NONE ||
  76.                 Precision.equals(message.getScale(), standard.getScale(), 1) &&
  77.                 message.sameDimension(standard)) {
  78.                 return standard;
  79.             } else {
  80.                 throw new OrekitException(OrekitMessages.INCOMPATIBLE_UNITS,
  81.                                           message.getName(), standard.getName());
  82.             }
  83.         }
  84.     };

  85.     /** Select the unit to use for interpreting parsed value.
  86.      * @param message unit parsed in the CCSDS message
  87.      * @param standard unit mandated by the standard
  88.      * @return selected unit
  89.      */
  90.     public abstract Unit select(Unit message, Unit standard);

  91. }