IIRVTermUtils.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.OrekitException;
  19. import org.orekit.errors.OrekitMessages;
  20. import org.orekit.files.iirv.IIRVVector;
  21. import org.orekit.files.iirv.terms.base.IIRVVectorTerm;

  22. /**
  23.  * Utilities class for {@link IIRVVectorTerm} subclasses.
  24.  *
  25.  * @author Nick LaFarge
  26.  * @since 13.0
  27.  */
  28. public final class IIRVTermUtils {

  29.     /** Private constructor. */
  30.     private IIRVTermUtils() {
  31.     }

  32.     /**
  33.      * Add padding characters to a string.
  34.      *
  35.      * @param string           string to pad
  36.      * @param c                padding character
  37.      * @param size             desired size
  38.      * @param addPaddingToLeft if true, the resulting string is right justified (i.e. the padding character is added to
  39.      *                         the left of the string)
  40.      * @return padded String
  41.      */
  42.     public static String addPadding(final String string,
  43.                                     final char c,
  44.                                     final int size,
  45.                                     final boolean addPaddingToLeft) {

  46.         if (size <= 0) {
  47.             throw new OrekitException(OrekitMessages.NOT_STRICTLY_POSITIVE, size);
  48.         }
  49.         final StringBuilder padding = new StringBuilder();
  50.         for (int i = 0; i < size; i++) {
  51.             padding.append(c);
  52.         }

  53.         if (addPaddingToLeft) {
  54.             final String concatenated = padding + string;
  55.             final int l = concatenated.length();
  56.             return concatenated.substring(l - size, l);
  57.         }

  58.         return (string + padding).substring(0, size);

  59.     }

  60.     /**
  61.      * Converts a list of {@link IIRVVectorTerm} instances to a String for a single line of an {@link IIRVVector}.
  62.      *
  63.      * @param terms terms to parse/convert
  64.      * @return String containing each of the inputted terms
  65.      */
  66.     public static String iirvTermsToLineString(final IIRVVectorTerm<?>... terms) {
  67.         return iirvTermsToLineStringSplitByTerm("", terms);
  68.     }


  69.     /**
  70.      * Converts a list of {@link IIRVVectorTerm} instances to a String for a single line of an {@link IIRVVector},
  71.      * where each term in the line is split by a specified delimiter.
  72.      * <p>
  73.      * For real IIRV vector, the deliminator is always empty; it is only used when creating human-readable forms
  74.      * to more readily identify specific terms within a given message.
  75.      *
  76.      * @param delimiter delimiter to insert between each IIRV vector term
  77.      * @param terms     terms to parse/convert
  78.      * @return String containing each of the inputted terms
  79.      */
  80.     public static String iirvTermsToLineStringSplitByTerm(final String delimiter, final IIRVVectorTerm<?>... terms) {
  81.         final StringBuilder lineString = new StringBuilder();
  82.         for (IIRVVectorTerm<?> term : terms) {
  83.             lineString.append(term.toEncodedString());
  84.             if (!delimiter.isEmpty()) {
  85.                 lineString.append(delimiter);
  86.             }
  87.         }
  88.         return lineString.toString();
  89.     }

  90. }