1   /* Copyright 2002-2026 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.errors;
18  
19  import java.io.Serial;
20  
21  
22  import org.hipparchus.exception.Localizable;
23  import org.hipparchus.exception.MathRuntimeException;
24  
25  /** This class is the base class for all specific exceptions thrown by
26   * during the {@link org.orekit.utils.GenericTimeStampedCache}.
27   *
28   * @author Luc Maisonobe
29   */
30  public class TimeStampedCacheException extends OrekitException {
31  
32      /** Serializable UID. */
33      @Serial
34      private static final long serialVersionUID = 9015424948577907926L;
35  
36      /** Simple constructor.
37       * Build an exception with a translated and formatted message
38       * @param specifier format specifier (to be translated)
39       * @param parts parts to insert in the format (no translation)
40       */
41      public TimeStampedCacheException(final Localizable specifier, final Object... parts) {
42          super(specifier, parts);
43      }
44  
45      /** Simple constructor.
46       * Build an exception from a cause and with a specified message
47       * @param cause underlying cause
48       * @param specifier format specifier (to be translated)
49       * @param parts parts to insert in the format (no translation)
50       */
51      public TimeStampedCacheException(final Throwable cause, final Localizable specifier,
52                                       final Object... parts) {
53          super(cause, specifier, parts);
54      }
55  
56      /** Simple constructor.
57       * Build an exception wrapping an {@link OrekitException} instance
58       * @param exception underlying cause
59       */
60      public TimeStampedCacheException(final OrekitException exception) {
61          super(exception);
62      }
63  
64      /** Simple constructor.
65       * Build an exception from an Hipparchus exception
66       * @param exception underlying Hipparchus exception
67       */
68      public TimeStampedCacheException(final MathRuntimeException exception) {
69          super(exception);
70      }
71  
72      /** Recover a TimeStampedCacheException, possibly embedded in a {@link OrekitException}.
73       * <p>
74       * If the {@code OrekitException} does not embed a TimeStampedCacheException, a
75       * new one will be created.
76       * </p>
77       * @param oe OrekitException to analyze
78       * @return a (possibly embedded) TimeStampedCacheException
79       */
80      public static TimeStampedCacheException unwrap(final OrekitException oe) {
81  
82          for (Throwable t = oe; t != null; t = t.getCause()) {
83              if (t instanceof TimeStampedCacheException exception) {
84                  return exception;
85              }
86          }
87  
88          return new TimeStampedCacheException(oe);
89  
90      }
91  
92      /** Recover a TimeStampedCacheException, possibly embedded in a {@link MathRuntimeException}.
93       * <p>
94       * If the {@code MathRuntimeException} does not embed a TimeStampedCacheException, a
95       * new one will be created.
96       * </p>
97       * @param exception MathRuntimeException to analyze
98       * @return a (possibly embedded) TimeStampedCacheException
99       */
100     public static TimeStampedCacheException unwrap(final MathRuntimeException exception) {
101 
102         for (Throwable t = exception; t != null; t = t.getCause()) {
103             if (t instanceof OrekitException orekitException) {
104                 if (t instanceof TimeStampedCacheException cacheException) {
105                     return cacheException;
106                 } else {
107                     return new TimeStampedCacheException(orekitException);
108                 }
109             }
110         }
111 
112         return new TimeStampedCacheException(exception);
113 
114     }
115 
116 }