CommentsContainer.java

  1. /* Copyright 2002-2023 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.section;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitMessages;

  23. /** Container for comments in various CCSDS messages.
  24.  * <p>
  25.  * CCSDS files accept comments only at the beginning of sections.
  26.  * Once header/metadata/data content has started, comments in the
  27.  * corresponding section are refused.
  28.  * </p>
  29.  * @author Luc Maisonobe
  30.  * @since 11.0
  31.  */
  32. public class CommentsContainer implements Section {

  33.     /** Comments, as one line per string. */
  34.     private final List<String> comments;

  35.     /** Indicator for accepting comments. */
  36.     private boolean acceptComments;

  37.     /** Create a new meta-data.
  38.      */
  39.     public CommentsContainer() {
  40.         comments       = new ArrayList<>();
  41.         acceptComments = true;
  42.     }

  43.     /** Complain if a field is negative.
  44.      * @param field field to check
  45.      * @param key key associated with the field
  46.      */
  47.     public void checkNotNegative(final int field, final String key) {
  48.         if (field < 0) {
  49.             throw new OrekitException(OrekitMessages.UNINITIALIZED_VALUE_FOR_KEY, key);
  50.         }
  51.     }

  52.     /** Complain if a field is NaN.
  53.      * @param field field to check
  54.      * @param key key associated with the field
  55.      */
  56.     public void checkNotNaN(final double field, final String key) {
  57.         if (Double.isNaN(field)) {
  58.             throw new OrekitException(OrekitMessages.UNINITIALIZED_VALUE_FOR_KEY, key);
  59.         }
  60.     }

  61.     /** Complain if a field is null.
  62.      * @param field field to check
  63.      * @param key key associated with the field
  64.      */
  65.     public void checkNotNull(final Object field, final String key) {
  66.         if (field == null) {
  67.             throw new OrekitException(OrekitMessages.UNINITIALIZED_VALUE_FOR_KEY, key);
  68.         }
  69.     }

  70.     /** Complain if a key is not allowed.
  71.      * @param version format version
  72.      * @param field field to check
  73.      * @param key key associated with the field
  74.      * @param minVersion version at which key started to be allowed
  75.      * @param maxVersion version at which key started to be forbidden
  76.      */
  77.     public void checkAllowed(final double version, final Object field, final String key,
  78.                              final double minVersion, final double maxVersion) {
  79.         if (field != null && (version < minVersion || version >= maxVersion)) {
  80.             throw new OrekitException(OrekitMessages.CCSDS_KEYWORD_NOT_ALLOWED_IN_VERSION,
  81.                                       key, version);
  82.         }
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public void validate(final double version) {
  87.         // nothing to do here
  88.     }

  89.     /** Get the comments.
  90.      * @return comments
  91.      */
  92.     public List<String> getComments() {
  93.         return Collections.unmodifiableList(comments);
  94.     }

  95.     /** Check if container is still accepting comments.
  96.      * <p>
  97.      * A container that still accept comments does not contain any other data.
  98.      * </p>
  99.      * @return true if container is still accepting comments
  100.      */
  101.     public boolean acceptComments() {
  102.         return acceptComments;
  103.     }

  104.     /** Set flag to refuse further comments.
  105.      */
  106.     public void refuseFurtherComments() {
  107.         acceptComments = false;
  108.     }

  109.     /**
  110.      * Add comment.
  111.      * <p>
  112.      * Comments are accepted only at start. Once
  113.      * other content is stored in the same section, comments are refused.
  114.      * </p>
  115.      * @param comment comment line
  116.      * @return true if comment was accepted
  117.      */
  118.     public boolean addComment(final String comment) {
  119.         if (acceptComments) {
  120.             comments.add(comment);
  121.             return true;
  122.         } else {
  123.             return false;
  124.         }
  125.     }
  126. }