CheckSumTerm.java

  1. /* Copyright 2024-2025 The Johns Hopkins University Applied Physics Laboratory
  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.iirv.terms;


  18. import org.orekit.errors.OrekitIllegalArgumentException;
  19. import org.orekit.errors.OrekitMessages;
  20. import org.orekit.files.iirv.IIRVVector;
  21. import org.orekit.files.iirv.terms.base.IIRVVectorTerm;
  22. import org.orekit.files.iirv.terms.base.LongValuedIIRVTerm;

  23. /**
  24.  * Three-character checksum to validate message.
  25.  * <p>
  26.  * Calculated by summing the decimal equivalent of the preceding characters in the line, counting spaces as 0 and
  27.  * negative signs as 1:
  28.  * <ul>
  29.  * <li> 0 through 9 = face value
  30.  * <li> Minus (-)   = 1
  31.  * <li> ASCII Space = 0
  32.  * </ul>
  33.  * <p>
  34.  * Valid Values: 000-999
  35.  *
  36.  * @author Nick LaFarge
  37.  * @since 13.0
  38.  */
  39. public class CheckSumTerm extends LongValuedIIRVTerm {

  40.     /** The length of the IIRV term within the message. */
  41.     public static final int CHECK_SUM_TERM_LENGTH = 3;

  42.     /** Regular expression that ensures the validity of string values for this term. */
  43.     public static final String CHECK_SUM_TERM_PATTERN = "\\d{3}";

  44.     /**
  45.      * Constructor.
  46.      * <p>
  47.      * See {@link LongValuedIIRVTerm#LongValuedIIRVTerm(String, String, int, boolean)}
  48.      *
  49.      * @param value value of the coordinate system term
  50.      */
  51.     public CheckSumTerm(final String value) {
  52.         super(CHECK_SUM_TERM_PATTERN, value.replace(IIRVVector.LINE_SEPARATOR, ""), CHECK_SUM_TERM_LENGTH, false);
  53.     }

  54.     /**
  55.      * Constructor.
  56.      * <p>
  57.      * See {@link LongValuedIIRVTerm#LongValuedIIRVTerm(String, long, int, boolean)}
  58.      *
  59.      * @param value value of the coordinate system term
  60.      */
  61.     public CheckSumTerm(final long value) {
  62.         super(CHECK_SUM_TERM_PATTERN, value, CHECK_SUM_TERM_LENGTH, false);
  63.     }

  64.     /**
  65.      * Constructs an IIRV checksum from a series of IIRVTerm instances.
  66.      *
  67.      * @param terms IIRVTerms to compute checksum
  68.      * @return newly created CheckSum instance
  69.      */
  70.     public static CheckSumTerm fromIIRVTerms(final IIRVVectorTerm<?>... terms) {
  71.         final String lineString = IIRVTermUtils.iirvTermsToLineString(terms);
  72.         return new CheckSumTerm(computeChecksum(lineString));
  73.     }

  74.     /**
  75.      * Computes the sum of the decimal equivalent of characters in the line, counting spaces as 0 and
  76.      * negative signs as 1.
  77.      *
  78.      * @param input input string to compute checksum from
  79.      * @return computed checksum integer value
  80.      */
  81.     public static int computeChecksum(final String input) {
  82.         // Compute the sum based on the characters
  83.         int sum = 0;
  84.         for (int i = 0; i < input.length(); i++) {
  85.             final char c = input.charAt(i);
  86.             final int valueToAdd;
  87.             if (Character.isDigit(c)) {
  88.                 valueToAdd = Character.getNumericValue(c);  // Convert the digit character to its numeric value
  89.             } else if (c == ' ') {
  90.                 valueToAdd = 0;  // Space counts as 0
  91.             } else if (c == '-') {
  92.                 valueToAdd = 1;  // Sign character counts as 1
  93.             } else {
  94.                 throw new OrekitIllegalArgumentException(OrekitMessages.IIRV_INVALID_TERM_VALUE, c);
  95.             }
  96.             sum += valueToAdd;  // Increment the sum
  97.         }
  98.         return sum;
  99.     }

  100.     /**
  101.      * Validate a line's embedded checksum value.
  102.      *
  103.      * @param line string line of an IIRV message (including checksum as the final three characters)
  104.      * @return true if the derived and embedded checksum values are equal
  105.      */
  106.     public static boolean validateLineCheckSum(final String line) {
  107.         // Don't include carriage/line returns in checksum
  108.         final String strippedLine = line.replace(IIRVVector.LINE_SEPARATOR, "");

  109.         // Separate message from checksum
  110.         final String message = strippedLine.substring(0, strippedLine.length() - 3);
  111.         final String checkSum = strippedLine.substring(strippedLine.length() - 3);

  112.         return CheckSumTerm.computeChecksum(message) == Integer.parseInt(checkSum);
  113.     }

  114.     /**
  115.      * Validate the checksum from a line based on the object's checksum integer value.
  116.      *
  117.      * @param line string line of an IIRV message (including checksum as the final three characters)
  118.      * @return true if the extracted checksum value matches this object's integer value
  119.      */
  120.     public boolean validateAgainstLineString(final String line) {
  121.         final String strippedLine = line.replace(IIRVVector.LINE_SEPARATOR, "");
  122.         final String message = strippedLine.substring(0, strippedLine.length() - 3);
  123.         final int computedChecksum = computeChecksum(message);
  124.         return value() == computedChecksum;
  125.     }

  126. }