com.ibm.tspaces
Class FieldPS

java.lang.Object
  |
  +--com.ibm.tspaces.Field
        |
        +--com.ibm.tspaces.FieldPS

public class FieldPS
extends Field
implements java.io.Serializable

FieldPS is a subclass of the T Spaces Field class that supports PreSerialization of the object.. It modifies the implementation of Field so that the value of a Field that will contain a user defined object is serialized into an array of bytes prior to being sent to the server. This avoids the problem of having to have user defined class object available in the Server classpath.

At the time that the FieldPS object is created or when the value is set using the setValue() method, the user defined object is serialized to a byte[] object and this is stored in the Field theValue instance variable.

When the value is requested, the byte[] in "theValue" is deserialized back to the user defined object and returned to the caller. An Example of usage:

    Example4aObj obj = new Example4aObj("User Object 1");
    FieldPS f2 = new FieldPS(obj);    
    Tuple tup = new Tuple("Key1",f2);
    ts.write(tup);
 
At this point, the object is stored on the server only as a byte[] object. Now we will read it back.
    Tuple template = new Tuple("Key1",new FieldPS(Example4aObj.class) );
    mytuple = (Tuple)ts.take(template);    
    Example4aObj objreturned = (Example4aObj)mytuple.getField(1).value();
  

Because the Server may not know anything about these classes that are represented by the serialized byte array, one can not specify a superclass in the template. If you want to write various subclasses and then retrieve based on the superclass then you must specify the superclass on the original write.

    A_child obj = new A_Child"User Object 1");
    FieldPS f2 = new FieldPS(A_parent.class,obj);
    ---
    Tuple template = new Tuple("Key1",new FieldPS(A_parent.class) );
 

There is no reason to believe that if you took 2 equal objects (equality defined by the "equal" method) that the 2 serialized bag of bytes would be equal. Therefore you should not do indexing of a FieldPS or use a FieldPS value in a template. However, you will get no warning if you try this. Don't try it! Perhaps somebody should throw an exception if you even try it.

Author:
John Thomas
See Also:
Field, Tuple, TupleSpace, Serialized Form

Fields inherited from class com.ibm.tspaces.Field
isFormal, theName, theType, theTypeString, theValue
 
Constructor Summary
FieldPS(java.lang.Class t)
          Create a FieldPS that has the Class as its type.
FieldPS(java.lang.Class t, java.io.Serializable v)
          Create a FieldPS that has the value Object.
FieldPS(java.io.Serializable v)
          Create a FieldPS that has the value Object.
 
Method Summary
 java.lang.Class getType()
          Return the Class instance that represents the type of the FieldPS.
 java.io.Serializable getValue()
          Return the value of the FieldPS.
static void main(java.lang.String[] argv)
          Test routine for FieldPS
 boolean matches(Field f2)
          This method is used to determine if the specified FieldPS matches the current FieldPS.
 void setValue(java.io.Serializable v)
          Set the value of the FieldPS to be any Serializable.
 java.lang.String toString()
          Return a String that represents the Field.
 
Methods inherited from class com.ibm.tspaces.Field
assign, clone, formal, getName, isFormal, makeField, name, setFormal, setFormal, setName, setType, type, value
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FieldPS

public FieldPS(java.io.Serializable v)
        throws TupleSpaceException
Create a FieldPS that has the value Object. The type of the FieldPS is the type (Class) of the value.
Parameters:
v - The value of the FieldPS
Throws:
TupleSpaceException - if there is a problem creating the FieldPS

FieldPS

public FieldPS(java.lang.Class t,
               java.io.Serializable v)
        throws TupleSpaceException
Create a FieldPS that has the value Object. The Class parameter must be the same as the type of the Object. This method is used for Objects that might be null.
Parameters:
v - The value of the FieldPS
Throws:
TupleSpaceException - if there is a problem creating the FieldPS

FieldPS

public FieldPS(java.lang.Class t)
        throws TupleSpaceException
Create a FieldPS that has the Class as its type. The FieldPS does not have a value, this is used to specify a formal FieldPS that can be used in a simple query to match another FieldPS of this type that has any value. Note: this just calls the Field superclass and then sets the class to null.
Parameters:
t - The value of the class
Throws:
TupleSpaceException - if there is a problem creating the FieldPS
Method Detail

setValue

public void setValue(java.io.Serializable v)
              throws TupleSpaceException
Set the value of the FieldPS to be any Serializable. The type of the FieldPS is the type (Class) of the value.
Overrides:
setValue in class Field
Parameters:
v - The value of the FieldPS.
Throws:
TupleSpaceException - if there is a problem creating the FieldPS

getType

public java.lang.Class getType()
Return the Class instance that represents the type of the FieldPS.
Overrides:
getType in class Field
Returns:
A Class instance that represents the type of the FieldPS.

getValue

public java.io.Serializable getValue()
Return the value of the FieldPS.
Overrides:
getValue in class Field
Returns:
An Serializable instance that is the value of the FieldPS.

matches

public boolean matches(Field f2)
This method is used to determine if the specified FieldPS matches the current FieldPS. The curent Field may be a Formal Field or an Actual Field.
  
 Rules for Matching:  Field f1.matches(Field f2)

 Rule 1: Field f1 is always the Query Field.
  

 Rule 2 - If f1 is Formal and the type Strings are equal
   then return true.
         if f1.isFormal
             if f1.theTypeString.equals(f2.theTypeString) -> true
   Because the server may not have any awareness of the represented 
   class, we can handle derived classes.  The class names as String 
   values must match exactly.

 Rule 3 - if both f1 and f2 are null and the types match, then return true
   
           if f1 == null && f2 == null && typeStrings match -> true

 Rule 4 - if f1 == null || f2 == null  -> false
   
 Rule 5   return  f1.getValue().equals(f2.getValue()) 

 

Override this method to change the matching behavior.

Overrides:
matches in class Field
Parameters:
f2 - The Field to compare against.
Returns:
true if the two Fields match, false otherwise.

toString

public java.lang.String toString()
Return a String that represents the Field. If the Field does not have a value then return the name of the Class that is the type of the Field. If the Field does have a value then return the string representation of the value of the Field.
Overrides:
toString in class Field
Returns:
A String that represents the Field.

main

public static void main(java.lang.String[] argv)
Test routine for FieldPS