001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.nabla;
018    
019    import org.apache.commons.math3.exception.util.ExceptionContext;
020    import org.apache.commons.math3.exception.util.ExceptionContextProvider;
021    import org.apache.commons.math3.exception.util.Localizable;
022    
023    /** This class represent differentiation exceptions.
024    
025     * <p>Exceptions of this type are thrown when a class cannot be differentiated.</p>
026    
027     * @version $Id$
028    
029     */
030    
031    public class DifferentiationException extends RuntimeException implements ExceptionContextProvider {
032    
033        /** Serializable version Id. */
034        private static final long serialVersionUID = 20120924L;
035    
036        /** Context. */
037        private final ExceptionContext context;
038    
039        /**
040         * Default constructor.
041         */
042        public DifferentiationException() {
043            context = new ExceptionContext(this);
044        }
045    
046        /**
047         * Constructor with a specific message.
048         *
049         * @param pattern Message pattern providing the specific context of
050         * the error.
051         * @param args Arguments.
052         */
053        public DifferentiationException(final Localizable pattern, final Object ... args) {
054            context = new ExceptionContext(this);
055            context.addMessage(pattern, args);
056        }
057    
058        /** {@inheritDoc} */
059        public ExceptionContext getContext() {
060            return context;
061        }
062    
063        /** {@inheritDoc} */
064        @Override
065        public String getMessage() {
066            return context.getMessage();
067        }
068    
069        /** {@inheritDoc} */
070        @Override
071        public String getLocalizedMessage() {
072            return context.getLocalizedMessage();
073        }
074    
075        /** Create an {@link java.lang.IllegalArgumentException} with localized message.
076         * @param specifier format specifier (to be translated)
077         * @param parts parts to insert in the format (no translation)
078         * @return an {@link java.lang.IllegalArgumentException} with localized message
079         */
080        public static IllegalArgumentException createIllegalArgumentException(final Localizable specifier,
081                                                                              final Object ... parts) {
082            return new IllegalArgumentException() {
083    
084                /** Serializable UID. */
085                private static final long serialVersionUID = 20121001L;
086    
087                /** {@inheritDoc} */
088                @Override
089                public String getMessage() {
090                    final ExceptionContext ctx = new ExceptionContext(this);
091                    ctx.addMessage(specifier, parts);
092                    return ctx.getMessage();
093                }
094    
095                /** {@inheritDoc} */
096                @Override
097                public String getLocalizedMessage() {
098                    final ExceptionContext ctx = new ExceptionContext(this);
099                    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    }