Modified "MathRuntimeException" class such that "SerializablePair" class has
become unnecessary.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1086045 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-03-27 21:51:31 +00:00
parent 33959a8a4e
commit e1852db1cf
2 changed files with 27 additions and 75 deletions

View File

@ -30,7 +30,6 @@ 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.SerializablePair;
/**
* This class is the base class for all exceptions.
@ -45,8 +44,14 @@ public class MathRuntimeException extends RuntimeException
/**
* Various informations that enrich the informative message.
*/
private List<SerializablePair<Localizable, Object[]>> messages
= new ArrayList<SerializablePair<Localizable, Object[]>>();
private List<Localizable> msgPatterns = new ArrayList<Localizable>();
/**
* Various informations that enrich the informative message.
* The arguments will replace the corresponding place-holders in
* {@link #msgPatterns}.
*/
private List<Object[]> msgArguments = new ArrayList<Object[]>();
/**
* Arbitrary context information.
*/
@ -69,8 +74,8 @@ public class MathRuntimeException extends RuntimeException
/** {@inheritDoc} */
public void addMessage(Localizable pattern,
Object ... arguments) {
messages.add(new SerializablePair<Localizable, Object[]>(pattern,
ArgUtils.flatten(arguments)));
msgPatterns.add(pattern);
msgArguments.add(ArgUtils.flatten(arguments));
}
/** {@inheritDoc} */
@ -133,11 +138,13 @@ public class MathRuntimeException extends RuntimeException
String separator) {
final StringBuilder sb = new StringBuilder();
int count = 0;
final int len = messages.size();
for (SerializablePair<Localizable, Object[]> pair : messages) {
final MessageFormat fmt = new MessageFormat(pair.getKey().getLocalizedString(locale),
final int len = msgPatterns.size();
for (int i = 0; i < len; i++) {
final Localizable pat = msgPatterns.get(i);
final Object[] args = msgArguments.get(i);
final MessageFormat fmt = new MessageFormat(pat.getLocalizedString(locale),
locale);
sb.append(fmt.format(pair.getValue()));
sb.append(fmt.format(args));
if (++count < len) {
// Add a separator if there are other messages.
sb.append(separator);
@ -173,7 +180,7 @@ public class MathRuntimeException extends RuntimeException
}
/**
* Serialize {@link #messages}.
* Serialize {@link #msgPatterns} and {@link #msgArguments}.
*
* @param out Stream.
* @throws IOException This should never happen.
@ -181,14 +188,14 @@ public class MathRuntimeException extends RuntimeException
private void serializeMessages(ObjectOutputStream out)
throws IOException {
// Step 1.
final int len = messages.size();
final int len = msgPatterns.size();
out.writeInt(len);
// Step 2.
for (int i = 0; i < len; i++) {
SerializablePair<Localizable, Object[]> pair = messages.get(i);
final Localizable pat = msgPatterns.get(i);
// Step 3.
out.writeObject(pair.getKey());
final Object[] args = pair.getValue();
out.writeObject(pat);
final Object[] args = msgArguments.get(i);
final int aLen = args.length;
// Step 4.
out.writeInt(aLen);
@ -205,7 +212,7 @@ public class MathRuntimeException extends RuntimeException
}
/**
* Deserialize {@link #messages}.
* Deserialize {@link #msgPatterns} and {@link #msgArguments}.
*
* @param in Stream.
* @throws IOException This should never happen.
@ -216,11 +223,13 @@ public class MathRuntimeException extends RuntimeException
ClassNotFoundException {
// Step 1.
final int len = in.readInt();
messages = new ArrayList<SerializablePair<Localizable, Object[]>>(len);
msgPatterns = new ArrayList<Localizable>(len);
msgArguments = new ArrayList<Object[]>(len);
// Step 2.
for (int i = 0; i < len; i++) {
// Step 3.
final Localizable key = (Localizable) in.readObject();
final Localizable pat = (Localizable) in.readObject();
msgPatterns.add(pat);
// Step 4.
final int aLen = in.readInt();
final Object[] args = new Object[aLen];
@ -228,7 +237,7 @@ public class MathRuntimeException extends RuntimeException
// Step 5.
args[j] = in.readObject();
}
messages.add(new SerializablePair<Localizable, Object[]>(key, args));
msgArguments.add(args);
}
}

View File

@ -1,57 +0,0 @@
/*
* 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 {
/** serializable UID. */
private static final long serialVersionUID = 1573515225013398712L;
/**
* 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);
}
}