1 /* Copyright 2002-2022 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
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.orekit.errors.OrekitException;
24 import org.orekit.errors.OrekitMessages;
25
26 /** Container for comments in various CCSDS messages.
27 * <p>
28 * CCSDS files accept comments only at the beginning of sections.
29 * Once header/metadata/data content has started, comments in the
30 * corresponding section are refused.
31 * </p>
32 * @author Luc Maisonobe
33 * @since 11.0
34 */
35 public class CommentsContainer implements Section {
36
37 /** Comments, as one line per string. */
38 private final List<String> comments;
39
40 /** Indicator for accepting comments. */
41 private boolean acceptComments;
42
43 /** Create a new meta-data.
44 */
45 public CommentsContainer() {
46 comments = new ArrayList<>();
47 acceptComments = true;
48 }
49
50 /** Complain if a field is negative.
51 * @param field field to check
52 * @param key key associated with the field
53 */
54 public void checkNotNegative(final int field, final Enum<?> key) {
55 if (field < 0) {
56 throw new OrekitException(OrekitMessages.UNINITIALIZED_VALUE_FOR_KEY, key.name());
57 }
58 }
59
60 /** Complain if a field is NaN.
61 * @param field field to check
62 * @param key key associated with the field
63 */
64 public void checkNotNaN(final double field, final Enum<?> key) {
65 if (Double.isNaN(field)) {
66 throw new OrekitException(OrekitMessages.UNINITIALIZED_VALUE_FOR_KEY, key.name());
67 }
68 }
69
70 /** Complain if a field is null.
71 * @param field field to check
72 * @param key key associated with the field
73 */
74 public void checkNotNull(final Object field, final Enum<?> key) {
75 if (field == null) {
76 throw new OrekitException(OrekitMessages.UNINITIALIZED_VALUE_FOR_KEY, key.name());
77 }
78 }
79
80 /** Complain if a key is not allowed.
81 * @param version format version
82 * @param field field to check
83 * @param key key associated with the field
84 * @param minVersion version at which key started to be allowed
85 * @param maxVersion version at which key started to be forbidden
86 */
87 public void checkAllowed(final double version, final Object field, final Enum<?> key,
88 final double minVersion, final double maxVersion) {
89 if (field != null && (version < minVersion || version >= maxVersion)) {
90 throw new OrekitException(OrekitMessages.CCSDS_KEYWORD_NOT_ALLOWED_IN_VERSION,
91 key.name(), version);
92 }
93 }
94
95 /** {@inheritDoc} */
96 @Override
97 public void validate(final double version) {
98 // nothing to do here
99 }
100
101 /** Get the comments.
102 * @return comments
103 */
104 public List<String> getComments() {
105 return Collections.unmodifiableList(comments);
106 }
107
108 /** Check if container is still accepting comments.
109 * <p>
110 * A container that still accept comments does not contain any other data.
111 * </p>
112 * @return true if container is still accepting comments
113 */
114 public boolean acceptComments() {
115 return acceptComments;
116 }
117
118 /** Set flag to refuse further comments.
119 */
120 public void refuseFurtherComments() {
121 acceptComments = false;
122 }
123
124 /**
125 * Add comment.
126 * <p>
127 * Comments are accepted only at start. Once
128 * other content is stored in the same section, comments are refused.
129 * </p>
130 * @param comment comment line
131 * @return true if comment was accepted
132 */
133 public boolean addComment(final String comment) {
134 if (acceptComments) {
135 comments.add(comment);
136 return true;
137 } else {
138 return false;
139 }
140 }
141 }