diff --git a/src/main/java/org/apache/commons/math/exception/DimensionMismatchException.java b/src/main/java/org/apache/commons/math/exception/DimensionMismatchException.java new file mode 100644 index 000000000..765f0f839 --- /dev/null +++ b/src/main/java/org/apache/commons/math/exception/DimensionMismatchException.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception; + +import org.apache.commons.math.util.LocalizedFormats; + +/** + * Exception to be thrown when two dimensions differ. + * + * @since 2.2 + * @version $Revision$ $Date$ + */ +public class DimensionMismatchException extends MathIllegalArgumentException { + /** First dimension. */ + private final int dimension1; + + /** Second dimension. */ + private final int dimension2; + + /** + * Construct an exception from the mismatched dimensions. + * + * @param dimension1 First dimension. + * @param dimension2 Second dimension. + */ + public DimensionMismatchException(int dimension1, + int dimension2) { + super(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, dimension1, dimension2); + + this.dimension1 = dimension1; + this.dimension2 = dimension2; + } + + /** + * @return the first dimension. + */ + public int getDimension1() { + return dimension1; + } + /** + * @return the second dimension. + */ + public int getDimension2() { + return dimension2; + } +} diff --git a/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java b/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java new file mode 100644 index 000000000..a78f77a10 --- /dev/null +++ b/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception; + +import java.util.Locale; +import org.apache.commons.math.util.Localizable; + +/** + * Base class for all preconditions violation exceptions. + * This class is not intended to be instantiated directly in most case: it + * should serve as a base class to create all the exceptions that share the + * semantics of the standard {@link IllegalArgumentException}, but must also + * provide a localized message. + * + * @since 2.2 + * @version $Revision$ $Date$ + */ +public class MathIllegalArgumentException extends IllegalArgumentException { + /** + * Pattern used to build the message. + */ + private final Localizable pattern; + /** + * Arguments used to build the message. + */ + private final Object[] arguments; + + /** + * @param pattern Message pattern. + * @param arguments Arguments. + */ + protected MathIllegalArgumentException(Localizable pattern, + Object ... arguments) { + this.pattern = pattern; + this.arguments = arguments.clone(); + } + + /** {@inheritDoc} */ + @Override + public String getMessage() { + return MessageFactory.buildMessage(Locale.US, pattern, arguments); + } + + /** {@inheritDoc} */ + @Override + public String getLocalizedMessage() { + return MessageFactory.buildMessage(Locale.getDefault(), pattern, arguments); + } +} diff --git a/src/main/java/org/apache/commons/math/exception/MessageFactory.java b/src/main/java/org/apache/commons/math/exception/MessageFactory.java new file mode 100644 index 000000000..c17aa556b --- /dev/null +++ b/src/main/java/org/apache/commons/math/exception/MessageFactory.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception; + +import java.text.MessageFormat; +import java.util.Locale; +import org.apache.commons.math.util.Localizable; + +/** + * Class for constructing localized messages. + * + * @since 2.2 + * @version $Revision$ $Date$ + */ +public class MessageFactory { + /** + * Class contains only static methods. + */ + private MessageFactory() {} + + /** + * Builds a message string by from a pattern and its arguments. + * + * @param locale Locale in which the message should be translated. + * @param pattern Format specifier. + * @param arguments Format arguments. + * @return a message string. + * @since 2.2 + */ + public static String buildMessage(Locale locale, + Localizable pattern, + Object ... arguments) { + return new MessageFormat(pattern.getLocalizedString(locale), locale).format(arguments); + } +} diff --git a/src/main/java/org/apache/commons/math/exception/OutOfRangeException.java b/src/main/java/org/apache/commons/math/exception/OutOfRangeException.java new file mode 100644 index 000000000..cf297bd66 --- /dev/null +++ b/src/main/java/org/apache/commons/math/exception/OutOfRangeException.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception; + +import org.apache.commons.math.util.LocalizedFormats; + +/** + * Exception to be thrown when some argument is out of range. + * + * @since 2.2 + * @version $Revision$ $Date$ + */ +public class OutOfRangeException extends MathIllegalArgumentException { + /** Lower bound. */ + private final Number lo; + /** Higher bound. */ + private final Number hi; + /** Requested. */ + private final Number requested; + + /** + * Construct an exception from the mismatched dimensions. + * + * @param requested Requested value. + * @param lo Lower bound. + * @param hi Higher bound. + */ + public OutOfRangeException(Number requested, + Number lo, + Number hi) { + super(LocalizedFormats.OUT_OF_RANGE_SIMPLE, requested, lo, hi); + + this.requested = requested; + this.lo = lo; + this.hi = hi; + } + + /** + * @return the requested value. + */ + public Number getRequested() { + return requested; + } + /** + * @return the lower bound. + */ + public Number getLo() { + return lo; + } + /** + * @return the higher bound. + */ + public Number getHi() { + return hi; + } +} diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index 1823aee5e..b37ccd8de 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -52,6 +52,9 @@ The type attribute can be add,update,fix,remove. If the output is not quite correct, check for invisible trailing spaces! --> + + Created package "exception" to contain the new exceptions hierarchy. + Implementation of linear interpolation.