com.sun.sgs.app.util
Class ManagedSerializable<T>

java.lang.Object
  extended by com.sun.sgs.app.util.ManagedSerializable<T>
Type Parameters:
T - the type of the wrapped object
All Implemented Interfaces:
ManagedObject, Serializable

public class ManagedSerializable<T>
extends Object
implements ManagedObject, Serializable

A utility class for wrapping a Serializable object within a ManagedObject instance. This class is primarily intended to allow class that does not implement ManagedObject to be persistently stored and accessed through a ManagedReference.

The serialization costs for a class largely depend on the size of the objects that it references. The ManagedReference class allows developers the ability to create breaks in the serialization graphs where not all of the fields are deserialized when a class is deserialized. The ManagedSerializable class is intended to be used for wrapping large serializable objects, such as collections, in order to break the serialization graph, thereby reducing the number of bytes read and written. Note that wrapping these types of objects does not guarantee a performance improvement.

Following is an example of where an existing class has been retrofitted to have ManagedReference references to its large fields, rather than standard references.

Before:

 
 public class MyPlayerObj {
     String name;
     Collection< Item > inventory;
     MapArea currentLocation;

     public MyPlayerObj(...) {
         ...
         inventory = new ArrayList< Item >();
     }

     ...

     public void findNearbyPlayers() {
         for (Player p : currentLocation.getPlayers())
             ...
     }
 }
 
 
After:
 public class MyPlayerObj {
     String name;
     ManagedReference< ManagedSerializable< Collection< Item >>> inventoryRef;
     ManagedReference< ManagedSerializable< MapArea >> currentLocationRef;

     public MyPlayerObj(...) {
         ...
         Collection< Item > inventory = new ArrayList< Item >();
         inventoryRef = AppContext.getDataManager().
             createReference(
                 new ManagedSerializable< Collection< Item>>(inventory));
     }

     ...

     public void findNearbyPlayers() {
         ManagedSerializable< MapArea > curLocWrapper =
             currentLocationRef.get();
         MapArea currentLocation = curLocWrapper.get();

         for (Player p : currentLocation.getPlayers())
             ...
     }
 }
 
 
Application developers are responsible for removing ManagedSerializable instances by calling DataManager.removeObject. Developers should call DataManager.markForUpdate or DataManager.getForUpdate if the application modifies objects wrapped by instances of this class.

See Also:
Serialized Form

Constructor Summary
ManagedSerializable(T object)
          Constructs an instance of this class that wraps the specified object, which must not implement ManagedObject, but must either implement Serializable or be null.
 
Method Summary
 boolean equals(Object o)
          Returns true if o is a ManagedSerializable that wraps an object that is equal to the object wrapped by this instance.
 T get()
          Returns the object wrapped by this instance, which may be null.
 int hashCode()
          
 void set(T object)
          Replaces the object wrapped by this instance with the specified object, which must not implement ManagedObject, but must either implement Serializable or be null.
 String toString()
          
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ManagedSerializable

public ManagedSerializable(T object)
Constructs an instance of this class that wraps the specified object, which must not implement ManagedObject, but must either implement Serializable or be null.

Parameters:
object - the object to wrap
Throws:
IllegalArgumentException - if object is an instance of ManagedObject, or if object is not null and does not implement Serializable
Method Detail

equals

public boolean equals(Object o)
Returns true if o is a ManagedSerializable that wraps an object that is equal to the object wrapped by this instance.

Overrides:
equals in class Object
Parameters:
o - the object to compared for equality with this instance
Returns:
true if o is a ManagedSerializable that wraps an object equal to the object wrapped by this instance

get

public T get()
Returns the object wrapped by this instance, which may be null.

Returns:
the object wrapped by this instance

hashCode

public int hashCode()

Overrides:
hashCode in class Object

toString

public String toString()

Overrides:
toString in class Object

set

public void set(T object)
Replaces the object wrapped by this instance with the specified object, which must not implement ManagedObject, but must either implement Serializable or be null.

Parameters:
object - the new object to wrap
Throws:
IllegalArgumentException - if object is an instance of ManagedObject, or if object is not null and does not implement Serializable

Project Darkstar, Version 0.9.9.6
2009-05-08 15:39:40

Copyright © 2007-2009 Sun Microsystems, Inc. All rights reserved