View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF 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.apache.commons.nabla;
18  
19  import org.apache.commons.math3.exception.util.ExceptionContext;
20  import org.apache.commons.math3.exception.util.ExceptionContextProvider;
21  import org.apache.commons.math3.exception.util.Localizable;
22  
23  /** This class represent differentiation exceptions.
24  
25   * <p>Exceptions of this type are thrown when a class cannot be differentiated.</p>
26  
27   * @version $Id$
28  
29   */
30  
31  public class DifferentiationException extends RuntimeException implements ExceptionContextProvider {
32  
33      /** Serializable version Id. */
34      private static final long serialVersionUID = 20120924L;
35  
36      /** Context. */
37      private final ExceptionContext context;
38  
39      /**
40       * Default constructor.
41       */
42      public DifferentiationException() {
43          context = new ExceptionContext(this);
44      }
45  
46      /**
47       * Constructor with a specific message.
48       *
49       * @param pattern Message pattern providing the specific context of
50       * the error.
51       * @param args Arguments.
52       */
53      public DifferentiationException(final Localizable pattern, final Object ... args) {
54          context = new ExceptionContext(this);
55          context.addMessage(pattern, args);
56      }
57  
58      /** {@inheritDoc} */
59      public ExceptionContext getContext() {
60          return context;
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public String getMessage() {
66          return context.getMessage();
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public String getLocalizedMessage() {
72          return context.getLocalizedMessage();
73      }
74  
75      /** Create an {@link java.lang.IllegalArgumentException} with localized message.
76       * @param specifier format specifier (to be translated)
77       * @param parts parts to insert in the format (no translation)
78       * @return an {@link java.lang.IllegalArgumentException} with localized message
79       */
80      public static IllegalArgumentException createIllegalArgumentException(final Localizable specifier,
81                                                                            final Object ... parts) {
82          return new IllegalArgumentException() {
83  
84              /** Serializable UID. */
85              private static final long serialVersionUID = 20121001L;
86  
87              /** {@inheritDoc} */
88              @Override
89              public String getMessage() {
90                  final ExceptionContext ctx = new ExceptionContext(this);
91                  ctx.addMessage(specifier, parts);
92                  return ctx.getMessage();
93              }
94  
95              /** {@inheritDoc} */
96              @Override
97              public String getLocalizedMessage() {
98                  final ExceptionContext ctx = new ExceptionContext(this);
99                  ctx.addMessage(specifier, parts);
100                 return ctx.getLocalizedMessage();
101             }
102 
103         };
104 
105     }
106 
107     /** Create an {@link java.lang.RuntimeException} for an internal error.
108      * @param cause underlying cause
109      * @return an {@link java.lang.RuntimeException} for an internal error
110      */
111     public static RuntimeException createInternalError(final Throwable cause) {
112 
113         /** Format specifier (to be translated). */
114         final Localizable SPECIFIER = NablaMessages.INTERNAL_ERROR;
115 
116         /** URL for reporting problems. */
117         final String REPORT_URL = "https://issues.apache.org/jira/browse/SANDBOX/component/12312280";
118 
119         return new RuntimeException() {
120 
121             /** Serializable UID. */
122             private static final long serialVersionUID = 20121001L;
123 
124             /** {@inheritDoc} */
125             @Override
126             public String getMessage() {
127                 final ExceptionContext ctx = new ExceptionContext(this);
128                 ctx.addMessage(SPECIFIER, REPORT_URL);
129                 return ctx.getMessage();
130             }
131 
132             /** {@inheritDoc} */
133             @Override
134             public String getLocalizedMessage() {
135                 final ExceptionContext ctx = new ExceptionContext(this);
136                 ctx.addMessage(SPECIFIER, REPORT_URL);
137                 return ctx.getLocalizedMessage();
138             }
139 
140         };
141 
142     }
143 
144 }