From e1852db1cffa808714be88c48d7d23b048e9f76f Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Sun, 27 Mar 2011 21:51:31 +0000 Subject: [PATCH] MATH-551 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 --- .../math/exception/MathRuntimeException.java | 45 +++++++++------ .../commons/math/util/SerializablePair.java | 57 ------------------- 2 files changed, 27 insertions(+), 75 deletions(-) delete mode 100644 src/main/java/org/apache/commons/math/util/SerializablePair.java 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 fc1300147..150b886d6 100644 --- a/src/main/java/org/apache/commons/math/exception/MathRuntimeException.java +++ b/src/main/java/org/apache/commons/math/exception/MathRuntimeException.java @@ -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> messages - = new ArrayList>(); + private List msgPatterns = new ArrayList(); + /** + * Various informations that enrich the informative message. + * The arguments will replace the corresponding place-holders in + * {@link #msgPatterns}. + */ + private List msgArguments = new ArrayList(); + /** * Arbitrary context information. */ @@ -69,8 +74,8 @@ public class MathRuntimeException extends RuntimeException /** {@inheritDoc} */ public void addMessage(Localizable pattern, Object ... arguments) { - messages.add(new SerializablePair(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 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 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>(len); + msgPatterns = new ArrayList(len); + msgArguments = new ArrayList(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(key, args)); + msgArguments.add(args); } } diff --git a/src/main/java/org/apache/commons/math/util/SerializablePair.java b/src/main/java/org/apache/commons/math/util/SerializablePair.java deleted file mode 100644 index 46334edc6..000000000 --- a/src/main/java/org/apache/commons/math/util/SerializablePair.java +++ /dev/null @@ -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 Key type. - * @param Value type. - * - * @version $Revision$ $Date$ - * @since 3.0 - */ -public class SerializablePair - extends Pair - 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 entry) { - super(entry); - } -}