📄 mapmessageimpl.java
字号:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2000-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: MapMessageImpl.java,v 1.8 2003/08/17 01:32:24 tanderson Exp $
*
* Date Author Changes
* 02/26/2000 jimm Created
*/
package org.exolab.jms.message;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageFormatException;
import javax.jms.MessageNotWriteableException;
/**
* This class implements the {@link javax.jms.MapMessage} interface.
* <p>
* A MapMessage is used to send a set of name-value pairs where names are
* Strings and values are Java primitive types. The entries can be accessed
* sequentially or randomly by name. The order of the entries is undefined.
* It inherits from <code>Message</code> and adds a map message body.
* <p>
* The primitive types can be read or written explicitly using methods
* for each type. They may also be read or written generically as objects.
* For instance, a call to <code>MapMessage.setInt("foo", 6)</code> is
* equivalent to <code>MapMessage.setObject("foo", new Integer(6))</code>.
* Both forms are provided because the explicit form is convenient for
* static programming and the object form is needed when types are not known
* at compile time.
* <p>
* When a client receives a MapMessage, it is in read-only mode. If a
* client attempts to write to the message at this point, a
* MessageNotWriteableException is thrown. If {@link #clearBody} is
* called, the message can now be both read from and written to.
* <p>
* Map messages support the following conversion table. The marked cases
* must be supported. The unmarked cases must throw a JMSException. The
* String to primitive conversions may throw a runtime exception if the
* primitives <code>valueOf()</code> method does not accept it as a valid
* String representation of the primitive.
* <p>
* A value written as the row type can be read as the column type.
*
* <pre>
* | | boolean byte short char int long float double String byte[]
* |----------------------------------------------------------------------
* |boolean | X X
* |byte | X X X X X
* |short | X X X X
* |char | X X
* |int | X X X
* |long | X X
* |float | X X X
* |double | X X
* |String | X X X X X X X X
* |byte[] | X
* |----------------------------------------------------------------------
* </pre>
*
* <p>
* Attempting to read a null value as a Java primitive type must be treated
* as calling the primitive's corresponding <code>valueOf(String)</code>
* conversion method with a null value. Since char does not support a
* String conversion, attempting to read a null value as a char must
* throw NullPointerException.
*
* @version $Revision: 1.8 $ $Date: 2003/08/17 01:32:24 $
* @author <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
* @see javax.jms.MapMessage
*/
public class MapMessageImpl extends MessageImpl implements MapMessage {
/**
* Object version no. for serialization
*/
static final long serialVersionUID = 2;
/**
* The initial size of the map
*/
private static final int INITIAL_SIZE = 20;
/**
* The container for all message data
*/
private HashMap _map = new HashMap(INITIAL_SIZE);
/**
* Construct a new MapMessage
*
* @throws JMSException if the message type can't be set
*/
public MapMessageImpl() throws JMSException {
setJMSType("MapMessage");
}
/**
* Clone an instance of this object
*
* @return a copy of this object
* @throws CloneNotSupportedException if object or attributes aren't
* cloneable
*/
public final Object clone() throws CloneNotSupportedException {
MapMessageImpl result = (MapMessageImpl) super.clone();
result._map = (HashMap) _map.clone();
return result;
}
/**
* Serialize out this message's data
*
* @param out the stream to serialize out to
* @throws IOException if any I/O exceptions occurr
*/
public final void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
out.writeLong(serialVersionUID);
out.writeObject(_map);
}
/**
* Serialize in this message's data
*
* @param in the stream to serialize in from
* @throws ClassNotFoundException if the class for an object being
* restored cannot be found.
* @throws IOException if any I/O exceptions occur
*/
public final void readExternal(ObjectInput in)
throws ClassNotFoundException, IOException {
super.readExternal(in);
long version = in.readLong();
if (version == serialVersionUID) {
_map = (HashMap) in.readObject();
} else {
throw new IOException("Incorrect version enountered: " + version +
". This version = " + serialVersionUID);
}
}
/**
* Return the boolean value with the given name
*
* @param name the name of the boolean
* @return the boolean value with the given name
* @throws JMSException if JMS fails to read the message due to some
* internal JMS error
* @throws MessageFormatException if this type conversion is invalid
*/
public final boolean getBoolean(String name)
throws JMSException, MessageFormatException {
return FormatConverter.getBoolean(_map.get(name));
}
/**
* Return the byte value with the given name
*
* @param name the name of the byte
* @return the byte value with the given name
* @throws JMSException if JMS fails to read the message due to some
* internal JMS error
* @throws MessageFormatException if this type conversion is invalid
*/
public final byte getByte(String name)
throws JMSException, MessageFormatException {
return FormatConverter.getByte(_map.get(name));
}
/**
* Return the short value with the given name
*
* @param name the name of the short
* @return the short value with the given name
* @throws JMSException if JMS fails to read the message due to some
* internal JMS error
* @throws MessageFormatException if this type conversion is invalid
*/
public final short getShort(String name)
throws JMSException, MessageFormatException {
return FormatConverter.getShort(_map.get(name));
}
/**
* Return the Unicode character value with the given name
*
* @param name the name of the Unicode character
* @return the Unicode character value with the given name
* @throws JMSException if JMS fails to read the message due to some
* internal JMS error
* @throws MessageFormatException if this type conversion is invalid
*/
public final char getChar(String name)
throws JMSException, MessageFormatException {
return FormatConverter.getChar(_map.get(name));
}
/**
* Return the integer value with the given name
*
* @param name the name of the integer
* @return the integer value with the given name
* @throws JMSException if JMS fails to read the message due to some
* internal JMS error
* @throws MessageFormatException if this type conversion is invalid
*/
public final int getInt(String name)
throws JMSException, MessageFormatException {
return FormatConverter.getInt(_map.get(name));
}
/**
* Return the long value with the given name
*
* @param name the name of the long
* @return the long value with the given name
* @throws JMSException if JMS fails to read the message due to some
* internal JMS error
* @throws MessageFormatException if this type conversion is invalid
*/
public final long getLong(String name)
throws JMSException, MessageFormatException {
return FormatConverter.getLong(_map.get(name));
}
/**
* Return the float value with the given name
*
* @param name the name of the float
* @return the float value with the given name
* @throws JMSException if JMS fails to read the message due to some
* internal JMS error
* @throws MessageFormatException if this type conversion is invalid
*/
public final float getFloat(String name)
throws JMSException, MessageFormatException {
return FormatConverter.getFloat(_map.get(name));
}
/**
* Return the double value with the given name
*
* @param name the name of the double
* @return the double value with the given name
* @throws JMSException if JMS fails to read the message due to some
* internal JMS error
* @throws MessageFormatException if this type conversion is invalid
*/
public final double getDouble(String name)
throws JMSException, MessageFormatException {
return FormatConverter.getDouble(_map.get(name));
}
/**
* Return the String value with the given name
*
* @param name the name of the String
* @return the String value with the given name. If there is no item
* by this name, a null value is returned.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -