What kind of object to store changing values?

John Zinky jzinky at bbn.com
Thu Sep 13 09:43:19 EDT 2007


On Sep 13, 2007, at 7:29 AM, Guillaume Liegard wrote:
> Thank you very much.
> I think simple POJOs will answer our needs.
>

Actually you do not want a POJO, you want an UniqueObject.
Generality aside, it is now recommended that ALL Blackboard object  
implement UniqueObject.

The org.cougaar.core.util.UniqueObject interface allows your  
Blackboard objects to be viewed by the task servlet.
Also, in order to move the object to another blackboard (via a  
relay), the object must be Serializable.

A base implementation is in org.cougaar.core.util.UniqueObjectBase  
(in HEAD as of Sept 12th, 2007)

import org.cougaar.core.util.UniqueObjectBase;

public class UniqueObjectBase implements UniqueObject {
   private final UID uid;

   public UniqueObjectBase(UID uid) {
     if (uid == null) {
       throw new IllegalArgumentException("null uid");
     }
     this.uid = uid;
   }

   public final UID getUID() {
     return uid;
   }
   public final void setUID(UID uid) {
     throw new IllegalStateException("UID cannot be changed");
   }

   public final boolean equals(Object o) {
     return
       ((o == this) ||
        ((o instanceof UniqueObject) &&
         uid.equals(((UniqueObject) o).getUID())));
   }
   public final int hashCode() {
     return uid.hashCode();
   }
   public String toString() {
     return "("+getClass().getName()+" uid="+uid+")";
   }
}


Also you may want to use the following trick to get the UID, which  
needs to be made in the context of the creating plugin.
You save a little text if you pass in the UID service and not a UID  
itself.

import org.cougaar.core.service.UIDService;
import org.cougaar.core.util.UniqueObjectBase;

public class PingQuery  extends UniqueObjectBase {
     private int count;

     public PingQuery(UIDService uids, int count, ) {
         super(uids.nextUID());
         this.count = count;
     }

}

In HEAD in org.cougaar.core.plugin.ParametrizedPlugin the UIDService  
is obtained automatically as field variable
  public UIDService uids;

So to make a new PingQuery the call would be
   PingQuery query = new  PingQuery(uids,0);

Using alternate implementation where you pass in the UID instead of  
the UIDService would be:
    PingQuery query = new  PingQuery(uids.nextID(),0);





More information about the Cougaar-developers mailing list