IIRVVectorTerm.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.base;

  18. import org.orekit.errors.OrekitIllegalArgumentException;
  19. import org.orekit.errors.OrekitMessages;

  20. import java.util.Objects;
  21. import java.util.regex.Pattern;

  22. /**
  23.  * Defines a term within an IIRV Vector, parameterized by its underlying data type.
  24.  *
  25.  * @param <T> Type of object represented in the term (e.g. integer, long, double, String, ...)
  26.  * @author Nick LaFarge
  27.  * @since 13.0
  28.  */
  29. public abstract class IIRVVectorTerm<T> implements Comparable<IIRVVectorTerm<?>> {

  30.     /**
  31.      * Regular expression pattern that validates the encoded String representation of
  32.      * {@link #value}, as computed by {@link #toEncodedString}.
  33.      */
  34.     private final String pattern;

  35.     /**
  36.      * Length of the term, measured in number characters contained in the encoded String representation of
  37.      * {@link #value}, as computed by {@link #toEncodedString}.
  38.      */
  39.     private final int length;

  40.     /** Value of the term. */
  41.     private final T value;

  42.     /**
  43.      * Constructs an IIRVVectorTerm with a given regular expression pattern, value, and length.
  44.      *
  45.      * @param pattern Regular expression pattern that validates the term
  46.      * @param value   Value of the term
  47.      * @param length  Length of the term, measured in number of characters in the String representation
  48.      */
  49.     protected IIRVVectorTerm(final String pattern, final T value, final int length) {
  50.         this.pattern = pattern;
  51.         this.length = length;
  52.         this.value = value;
  53.     }

  54.     /**
  55.      * Convert an IIRV term value into the encoded String representation, as it would appear in the IIRV message.
  56.      *
  57.      * @param termValue Value of the term
  58.      * @return Encoded String representing of the inputted IIRV term it appears in the IIRV message
  59.      */
  60.     public abstract String toEncodedString(T termValue);

  61.     /**
  62.      * Converts the stored {@link #value} of the IIRV term into the encoded String representation, as it would appear
  63.      * in the IIRV message.
  64.      *
  65.      * @return Encoded String representing of the value of the stored vector term, as it would appear in the
  66.      * IIRV message
  67.      */
  68.     public String toEncodedString() {
  69.         return toEncodedString(value);
  70.     }

  71.     @Override
  72.     public int compareTo(final IIRVVectorTerm<?> o) {
  73.         return this.toEncodedString().compareTo(o.toEncodedString());
  74.     }

  75.     @Override
  76.     public boolean equals(final Object o) {
  77.         if (this == o) {
  78.             return true;
  79.         }
  80.         if (!(o instanceof IIRVVectorTerm)) {
  81.             return false;
  82.         }
  83.         final IIRVVectorTerm<?> that = (IIRVVectorTerm<?>) o;
  84.         return this.compareTo(that) == 0;  // Equals if the encoded strings are the same
  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     public int hashCode() {
  89.         return Objects.hash(pattern, toEncodedString());
  90.     }

  91.     /**
  92.      * Validate a string value against the vector term, ensuring that it is the proper length and matches
  93.      * the specified regular expression pattern.
  94.      *
  95.      * @param valueString String to validate against the regular expression pattern
  96.      */
  97.     protected void validateString(final String valueString) {
  98.         // Check length of string (should be captured by the regex, but this is a more helpful error message)
  99.         if (valueString.length() != length) {
  100.             throw new OrekitIllegalArgumentException(OrekitMessages.INCONSISTENT_NUMBER_OF_ELEMENTS, length, valueString.length());
  101.         }

  102.         if (!Pattern.compile(this.pattern).matcher(valueString).matches()) { // Match the pattern
  103.             throw new OrekitIllegalArgumentException(OrekitMessages.IIRV_INVALID_TERM_VALUE, valueString);
  104.         }
  105.     }

  106.     /**
  107.      * Gets the value of the term in the IIRV vector.
  108.      *
  109.      * @return value of the term in the IIRV vector
  110.      */
  111.     public T value() {
  112.         return value;
  113.     }

  114.     /**
  115.      * Gets the length of the term.
  116.      * <p>
  117.      * The length is measured in number characters contained in the encoded String representation of
  118.      * {@link #value}, as computed by {@link #toEncodedString}.
  119.      *
  120.      * @return Length of the term
  121.      */
  122.     public int length() {
  123.         return length;
  124.     }
  125. }