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