message.java

来自「cqME :java framework for TCK test.」· Java 代码 · 共 305 行

JAVA
305
字号
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.j2me.services.messagingService;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;/** * <code>Message</code> class encapsulates data * that being exchanged between Messaging components. */public class Message {    public final static int MESSAGE = 1;    public final static int GET_MESSAGE = 2;    public final static int BROADCAST = 3;     public Message(int id, String from, String to, String[] args) {        this(id, from, to, args, null);    }    public Message(int id, String from, String to, String[] args, byte[] data) {        setID(id);  // TODO: do we need this field at all?        setFrom(from);        setTo(to);        setArgs(args);        setData(data);    }        public boolean equals(Object o) {        if (!(o instanceof Message))            return false;        Message m = (Message) o;        boolean equals = (m.getID() == getID())                                 && (equals(getFrom(), m.getFrom()))                                 && (equals(getTo(), m.getTo()))                                && (objectArraysEqual(m.getArgs(), getArgs()))                                && (byteArraysEqual(m.getData(), getData()));        return equals;    }    public int hashCode() {        int result = 17;        result = result*37 + getID();        result = result*37 + getFrom().hashCode();        result = result*37 + getTo().hashCode();        for (int i = 0; i < args.length; i++) {            result = result*37 + args[i].hashCode();        }        for (int i = 0; i < data.length; i++) {            result = result*37 + data[i];        }        return result;    }    public String[] getArgs() {        return args;    }    public byte[] getData() {        return data;    }    public String getFrom() {        return from;    }    public int getID() {        return id;    }    public String getTo() {        return to;    }       public void setArgs(String[] args) {        this.args = args;    }    public void setData(byte[] data) {        this.data = data;    }    public void setFrom(String from) {        this.from = from;    }    public void setID(int id) {        this.id = id;    }    public void setTo(String to) {        this.to = to;    }    public String toString() {        return "Message[" + "id=" + id + ", From=" + from + ", To=" + to + ", Args="                      + arrayToString(args) + ", Data=" + dataToString() + "]";    }    public void write(DataOutputStream dos) throws IOException{        dos.writeInt(id);        dos.writeUTF(from);        dos.writeUTF(to);        if (args == null) {            dos.writeInt(-1);        } else if (args.length == 0) {            dos.writeInt(0);        } else {            dos.writeInt(args.length);            for (int i = 0; i < args.length; i++) {                dos.writeUTF(args[i]);            }        }        if (data == null) {            dos.writeInt(-1);        } else if (data.length == 0) {            dos.writeInt(0);        } else {            dos.writeInt(data.length);            dos.write(data);        }        dos.flush();    }     public static Message read(DataInputStream dis) throws IOException {        int newId;        String[] newArgs;        byte[] newData;        String newFrom;        String newTo;        newId = dis.readInt();        newFrom = dis.readUTF();        newTo = dis.readUTF();        // read an array of Strings        int newStrArgsLength = dis.readInt();        if (newStrArgsLength < 0) {            newArgs = null;        } else {            newArgs = new String[newStrArgsLength];            for (int i = 0; i < newArgs.length; i++) {                newArgs[i] = dis.readUTF();            }        }        // read an array of bytes        int newDataLength = dis.readInt();        if (newDataLength < 0) {            newData = null;        } else if (newDataLength == 0) {            newData = new byte[] {};        } else {            newData = new byte[newDataLength];            dis.readFully(newData);        }                   return new Message(newId, newFrom, newTo, newArgs, newData);            }    // ========= Private stuff below this line ==============    private static String arrayToString(Object[] array) {              if (array == null) return "null";                StringBuffer sb;        sb = new StringBuffer("{");        if (array.length != 0) {            sb.append(array[0]);            for (int i = 1; i < array.length; i++) {                sb.append(", " + array[i]);            }        }        sb.append("}");        return sb.toString();    }    /**     * Returns <tt>true</tt> if the two specified arrays of bytes are     * <i>equal</i> to one another.  Two arrays are considered equal if both     * arrays contain the same number of elements, and all corresponding pairs     * of elements in the two arrays are equal.  In other words, two arrays     * are equal if they contain the same elements in the same order.  Also,     * two array references are considered equal if both are <tt>null</tt>.<p>     *     * @param a one array to be tested for equality.     * @param a2 the other array to be tested for equality.     * @return <tt>true</tt> if the two arrays are equal.     */    private static boolean byteArraysEqual(byte[] a, byte[] a2) {        if (a==a2)            return true;        if (a==null || a2==null)            return false;                int length = a.length;        if (a2.length != length)            return false;                for (int i=0; i<length; i++)            if (a[i] != a2[i])                return false;                    return true;    }    // TODO: eliminate duplication with arrayToString method     private String dataToString() {              if (data == null) return "null";                StringBuffer sb;        sb = new StringBuffer("{");        if (data.length != 0) {            sb.append(data[0]);            for (int i = 1; i < data.length; i++) {                sb.append(", " + data[i]);            }        }        sb.append("}");        return sb.toString();    }    private static boolean equals(String s1, String s2) {        if (s1 == null) {            if (s2 != null)                return false;        } else {            if (!s1.equals(s2))                return false;        }        return true;    }    /**     * Returns <tt>true</tt> if the two specified arrays of Objects are     * <i>equal</i> to one another.  The two arrays are considered equal if     * both arrays contain the same number of elements, and all corresponding     * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>     * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null     * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if     * they contain the same elements in the same order.  Also, two array     * references are considered equal if both are <tt>null</tt>.<p>     *     * @param a one array to be tested for equality.     * @param a2 the other array to be tested for equality.     * @return <tt>true</tt> if the two arrays are equal.     */    private static boolean objectArraysEqual(Object[] a, Object[] a2) {        if (a==a2)            return true;        if (a==null || a2==null)            return false;                                                                                                  int length = a.length;        if (a2.length != length)            return false;                                                                                                  for (int i=0; i<length; i++) {            Object o1 = a[i];            Object o2 = a2[i];            if (!(o1==null ? o2==null : o1.equals(o2)))                return false;        }                                                                                                  return true;    }    private String[] args;    private byte[] data;    private String from;    private int id;    private String to;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?