[Previous commit was a mistake.]

Created a "SerializablePair" for use in "MathRuntimeException".
Uncommented the test in "DummyStepInterpolatorTest".


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1079545 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-03-08 21:09:48 +00:00
parent 4a01140363
commit faed36db10
4 changed files with 64 additions and 12 deletions

View File

@ -26,7 +26,7 @@ import java.util.Locale;
import org.apache.commons.math.exception.util.ArgUtils; import org.apache.commons.math.exception.util.ArgUtils;
import org.apache.commons.math.exception.util.Localizable; 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. * 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. * Various informations that enrich the informative message.
*/ */
private final List<Pair<Localizable, Object[]>> messages private final List<SerializablePair<Localizable, Object[]>> messages
= new ArrayList<Pair<Localizable, Object[]>>(); = new ArrayList<SerializablePair<Localizable, Object[]>>();
/** /**
* Arbitrary context information. * Arbitrary context information.
*/ */
@ -65,7 +65,8 @@ public class MathRuntimeException extends RuntimeException
/** {@inheritDoc} */ /** {@inheritDoc} */
public void addMessage(Localizable pattern, public void addMessage(Localizable pattern,
Object ... arguments) { Object ... arguments) {
messages.add(new Pair<Localizable, Object[]>(pattern, ArgUtils.flatten(arguments))); messages.add(new SerializablePair<Localizable, Object[]>(pattern,
ArgUtils.flatten(arguments)));
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -129,8 +130,9 @@ public class MathRuntimeException extends RuntimeException
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
int count = 0; int count = 0;
final int len = messages.size(); final int len = messages.size();
for (Pair<Localizable, Object[]> pair : messages) { for (SerializablePair<Localizable, Object[]> pair : messages) {
final MessageFormat fmt = new MessageFormat(pair.getKey().getLocalizedString(locale), locale); final MessageFormat fmt = new MessageFormat(pair.getKey().getLocalizedString(locale),
locale);
sb.append(fmt.format(pair.getValue())); sb.append(fmt.format(pair.getValue()));
if (++count < len) { if (++count < len) {
// Add a separator if there are other messages. // Add a separator if there are other messages.

View File

@ -49,7 +49,7 @@ public class Pair<K, V> {
* *
* @param entry Entry to copy. * @param entry Entry to copy.
*/ */
Pair(Pair<? extends K, ? extends V> entry) { public Pair(Pair<? extends K, ? extends V> entry) {
key = entry.getKey(); key = entry.getKey();
value = entry.getValue(); value = entry.getValue();
} }

View File

@ -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 <K> Key type.
* @param <V> Value type.
*
* @version $Revision$ $Date$
* @since 3.0
*/
public class SerializablePair<K extends Serializable, V extends Serializable>
extends Pair<K, V>
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<? extends K, ? extends V> entry) {
super(entry);
}
}

View File

@ -123,11 +123,8 @@ public class DummyStepInterpolatorTest {
fail("an exception should have been thrown"); fail("an exception should have been thrown");
} catch (IOException ioe) { } catch (IOException ioe) {
// expected behavior // expected behavior
// XXX Why was the message supposed to be empty? assertEquals(0, ioe.getMessage().length());
// With the current code it is "org.apache.commons.math.util.Pair".
// assertEquals(0, ioe.getMessage().length());
} }
} }
private static class BadStepInterpolator extends DummyStepInterpolator { private static class BadStepInterpolator extends DummyStepInterpolator {
@ -139,7 +136,7 @@ public class DummyStepInterpolatorTest {
} }
@Override @Override
protected void doFinalize() throws MathUserException { protected void doFinalize() throws MathUserException {
throw new MathUserException(); throw new MathUserException(LocalizedFormats.SIMPLE_MESSAGE, "");
} }
} }
} }