// // $Id: AttributesChangedEvent.java,v 1.8 2001/10/11 04:07:52 mdb Exp $ package com.threerings.presents.dobj; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import com.samskivert.util.StringUtil; import com.threerings.presents.dobj.io.ValueMarshaller; /** * An attributes changed event is dispatched when multiple * attributes of a distributed object have been changed in a single * transaction. * * @see DObjectManager#postEvent */ public class AttributesChangedEvent extends TypedEvent { /** The typed object code for this event. */ public static final short TYPE = TYPE_BASE + 2; /** * Constructs a new attribute changed event on the specified target * object with the supplied attribute name and value. * * @param targetOid the object id of the object whose attribute has * changed. * @param count the number of attributes that have changed (the length * of the names and values arrays need not * be exactly equal to the number of attributes changed, there can be * extra space at the end). * @param names the names of the attributes (data members) that have * changed. * @param values the new values of the attributes (in the case of * primitive types, the reflection-defined object-alternative is * used). */ public AttributesChangedEvent (int targetOid, int count, String[] names, Object[] values) { super(targetOid); _count = count; _names = names; _values = values; } /** * Constructs a blank instance of this event in preparation for * unserialization from the network. */ public AttributesChangedEvent () { } /** * Returns the number of attributes that have changed. */ public int getCount () { return _count; } /** * Returns the name of the indexth attribute that has * changed. This value is not range checked, so be sure it is between * 0 and getCount()-1. You may not receive * an ArrayIndexOutOfBounds exception if you exceed * getCount() but rather get a null name. */ public String getName (int index) { return _names[index]; } /** * Returns the new value of the indexth attribute. This value * is not range checked, so be sure it is between 0 and * getCount()-1. You may not receive an * ArrayIndexOutOfBounds exception if you exceed * getCount() but rather get a null value. */ public Object getValue (int index) { return _values[index]; } /** * Returns the new value of the indexth attribute as a * short. This will fail if the attribute in question is not a short. * The index parameter is not range checked, so be sure * it is between 0 and getCount()-1. You may * not receive an ArrayIndexOutOfBounds exception if you * exceed getCount() but rather get a * NullPointerException. */ public short getShortValue (int index) { return ((Short)_values[index]).shortValue(); } /** * Returns the new value of the indexth attribute as an * int. This will fail if the attribute in question is not an int. * The index parameter is not range checked, so be sure * it is between 0 and getCount()-1. You may * not receive an ArrayIndexOutOfBounds exception if you * exceed getCount() but rather get a * NullPointerException. */ public int getIntValue (int index) { return ((Integer)_values[index]).intValue(); } /** * Returns the new value of the indexth attribute as a * long. This will fail if the attribute in question is not a long. * The index parameter is not range checked, so be sure * it is between 0 and getCount()-1. You may * not receive an ArrayIndexOutOfBounds exception if you * exceed getCount() but rather get a * NullPointerException. */ public long getLongValue (int index) { return ((Long)_values[index]).longValue(); } /** * Returns the new value of the indexth attribute as a * float. This will fail if the attribute in question is not a float. * The index parameter is not range checked, so be sure * it is between 0 and getCount()-1. You may * not receive an ArrayIndexOutOfBounds exception if you * exceed getCount() but rather get a * NullPointerException. */ public float getFloatValue (int index) { return ((Float)_values[index]).floatValue(); } /** * Returns the new value of the indexth attribute as a * double. This will fail if the attribute in question is not a * double. The index parameter is not range checked, so * be sure it is between 0 and getCount()-1. * You may not receive an ArrayIndexOutOfBounds exception * if you exceed getCount() but rather get a * NullPointerException. */ public double getDoubleValue (int index) { return ((Double)_values[index]).doubleValue(); } /** * Applies this attribute change to the object. */ public boolean applyToObject (DObject target) throws ObjectAccessException { // pass the new values on to the object for (int i = 0; i < _count; i++) { target.setAttribute(_names[i], _values[i]); } return true; } public short getType () { return TYPE; } public void writeTo (DataOutputStream out) throws IOException { super.writeTo(out); out.writeInt(_count); for (int i = 0; i < _count; i++) { out.writeUTF(_names[i]); ValueMarshaller.writeTo(out, _values[i]); } } public void readFrom (DataInputStream in) throws IOException { super.readFrom(in); _count = in.readInt(); _names = new String[_count]; _values = new Object[_count]; for (int i = 0; i < _count; i++) { _names[i] = in.readUTF(); _values[i] = ValueMarshaller.readFrom(in); } } protected void toString (StringBuffer buf) { buf.append("CHANGES:"); super.toString(buf); buf.append(", names=").append(StringUtil.toString(_names)); buf.append(", values=").append(StringUtil.toString(_values)); } protected int _count; protected String[] _names; protected Object[] _values; }