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  
19  import org.orekit.errors.OrekitException;
20  import org.orekit.errors.OrekitMessages;
21  import org.orekit.files.iirv.IIRVVector;
22  import org.orekit.files.iirv.terms.base.IIRVVectorTerm;
23  
24  /**
25   * Utilities class for {@link IIRVVectorTerm} subclasses.
26   *
27   * @author Nick LaFarge
28   * @since 13.0
29   */
30  public final class IIRVTermUtils {
31  
32      /** Private constructor. */
33      private IIRVTermUtils() {
34      }
35  
36      /**
37       * Add padding characters to a string.
38       *
39       * @param string           string to pad
40       * @param c                padding character
41       * @param size             desired size
42       * @param addPaddingToLeft if true, the resulting string is right justified (i.e. the padding character is added to
43       *                         the left of the string)
44       * @return padded String
45       */
46      public static String addPadding(final String string,
47                                      final char c,
48                                      final int size,
49                                      final boolean addPaddingToLeft) {
50  
51          if (size <= 0) {
52              throw new OrekitException(OrekitMessages.NOT_STRICTLY_POSITIVE, size);
53          }
54          final StringBuilder padding = new StringBuilder();
55          for (int i = 0; i < size; i++) {
56              padding.append(c);
57          }
58  
59          if (addPaddingToLeft) {
60              final String concatenated = padding + string;
61              final int l = concatenated.length();
62              return concatenated.substring(l - size, l);
63          }
64  
65          return (string + padding).substring(0, size);
66  
67      }
68  
69      /**
70       * Converts a list of {@link IIRVVectorTerm} instances to a String for a single line of an {@link IIRVVector}.
71       *
72       * @param terms terms to parse/convert
73       * @return String containing each of the inputted terms
74       */
75      public static String iirvTermsToLineString(final IIRVVectorTerm<?>... terms) {
76          return iirvTermsToLineStringSplitByTerm("", terms);
77      }
78  
79  
80      /**
81       * Converts a list of {@link IIRVVectorTerm} instances to a String for a single line of an {@link IIRVVector},
82       * where each term in the line is split by a specified delimiter.
83       * <p>
84       * For real IIRV vector, the deliminator is always empty; it is only used when creating human-readable forms
85       * to more readily identify specific terms within a given message.
86       *
87       * @param delimiter delimiter to insert between each IIRV vector term
88       * @param terms     terms to parse/convert
89       * @return String containing each of the inputted terms
90       */
91      public static String iirvTermsToLineStringSplitByTerm(final String delimiter, final IIRVVectorTerm<?>... terms) {
92          final StringBuilder lineString = new StringBuilder();
93          for (IIRVVectorTerm<?> term : terms) {
94              lineString.append(term.toEncodedString());
95              if (!delimiter.isEmpty()) {
96                  lineString.append(delimiter);
97              }
98          }
99          return lineString.toString();
100     }
101 
102 }