diff --git a/src/main/java/org/apache/commons/math/exception/MathRuntimeException.java b/src/main/java/org/apache/commons/math/exception/MathRuntimeException.java index fb6bc904d..841fcd281 100644 --- a/src/main/java/org/apache/commons/math/exception/MathRuntimeException.java +++ b/src/main/java/org/apache/commons/math/exception/MathRuntimeException.java @@ -26,7 +26,7 @@ import java.util.Locale; import org.apache.commons.math.exception.util.ArgUtils; import org.apache.commons.math.exception.util.Localizable; -import org.apache.commons.math.util.Pair; +import org.apache.commons.math.util.SerializablePair; /** * This class is the base class for all exceptions. @@ -41,8 +41,8 @@ public class MathRuntimeException extends RuntimeException /** * Various informations that enrich the informative message. */ - private final List> messages - = new ArrayList>(); + private final List> messages + = new ArrayList>(); /** * Arbitrary context information. */ @@ -65,7 +65,8 @@ public class MathRuntimeException extends RuntimeException /** {@inheritDoc} */ public void addMessage(Localizable pattern, Object ... arguments) { - messages.add(new Pair(pattern, ArgUtils.flatten(arguments))); + messages.add(new SerializablePair(pattern, + ArgUtils.flatten(arguments))); } /** {@inheritDoc} */ @@ -129,8 +130,9 @@ public class MathRuntimeException extends RuntimeException final StringBuilder sb = new StringBuilder(); int count = 0; final int len = messages.size(); - for (Pair pair : messages) { - final MessageFormat fmt = new MessageFormat(pair.getKey().getLocalizedString(locale), locale); + for (SerializablePair pair : messages) { + final MessageFormat fmt = new MessageFormat(pair.getKey().getLocalizedString(locale), + locale); sb.append(fmt.format(pair.getValue())); if (++count < len) { // Add a separator if there are other messages. diff --git a/src/main/java/org/apache/commons/math/util/Pair.java b/src/main/java/org/apache/commons/math/util/Pair.java index 8b3cff591..c5d79abd3 100644 --- a/src/main/java/org/apache/commons/math/util/Pair.java +++ b/src/main/java/org/apache/commons/math/util/Pair.java @@ -49,7 +49,7 @@ public class Pair { * * @param entry Entry to copy. */ - Pair(Pair entry) { + public Pair(Pair entry) { key = entry.getKey(); value = entry.getValue(); } diff --git a/src/main/java/org/apache/commons/math/util/SerializablePair.java b/src/main/java/org/apache/commons/math/util/SerializablePair.java new file mode 100644 index 000000000..512f1035f --- /dev/null +++ b/src/main/java/org/apache/commons/math/util/SerializablePair.java @@ -0,0 +1,53 @@ +/* + * 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.util; + +import java.io.Serializable; + +/** + * Generic pair. + * Immutable class. + * + * @param Key type. + * @param Value type. + * + * @version $Revision$ $Date$ + * @since 3.0 + */ +public class SerializablePair + extends Pair + implements Serializable { + /** + * Create an entry representing a mapping from the specified key to the + * specified value. + * + * @param k Key. + * @param v Value. + */ + public SerializablePair(K k, V v) { + super(k, v); + } + + /** + * Create an entry representing the same mapping as the specified entry. + * + * @param entry Entry to copy. + */ + public SerializablePair(SerializablePair entry) { + super(entry); + } +} diff --git a/src/test/java/org/apache/commons/math/ode/sampling/DummyStepInterpolatorTest.java b/src/test/java/org/apache/commons/math/ode/sampling/DummyStepInterpolatorTest.java index d9d53fd6f..65605b957 100644 --- a/src/test/java/org/apache/commons/math/ode/sampling/DummyStepInterpolatorTest.java +++ b/src/test/java/org/apache/commons/math/ode/sampling/DummyStepInterpolatorTest.java @@ -123,11 +123,8 @@ public class DummyStepInterpolatorTest { fail("an exception should have been thrown"); } catch (IOException ioe) { // expected behavior - // XXX Why was the message supposed to be empty? - // With the current code it is "org.apache.commons.math.util.Pair". - // assertEquals(0, ioe.getMessage().length()); + assertEquals(0, ioe.getMessage().length()); } - } private static class BadStepInterpolator extends DummyStepInterpolator { @@ -139,7 +136,7 @@ public class DummyStepInterpolatorTest { } @Override protected void doFinalize() throws MathUserException { - throw new MathUserException(); + throw new MathUserException(LocalizedFormats.SIMPLE_MESSAGE, ""); } } }