org.openide.util
Class SharedClassObject

java.lang.Object
  |
  +--org.openide.util.SharedClassObject
All Implemented Interfaces:
Externalizable, Serializable
Direct Known Subclasses:
DataLoader, ModuleInstall, SystemAction, SystemOption

public abstract class SharedClassObject
extends Object
implements Externalizable

Shared object that allows different instances of the same class to share common data.

The data are shared only between instances of the same class (not subclasses). Thus, such "variables" have neither instance nor static behavior.

See Also:
Serialized Form

Constructor Summary
protected SharedClassObject()
          Create a shared object.
 
Method Summary
protected  void addNotify()
          Notify subclasses that the first listener has been added to this action.
 void addPropertyChangeListener(PropertyChangeListener l)
           
protected  boolean clearSharedData()
          Indicate whether the shared data of the last existing instance of this class should be cleared when that instance is finalized.
 boolean equals(Object obj)
          Test whether the classes of the compared objects are the same.
protected  void finalize()
           
static SharedClassObject findObject(Class clazz)
          Obtain an instance of the desired class, if there is one.
static SharedClassObject findObject(Class clazz, boolean create)
          Find an existing object, possibly creating a new one as needed.
protected  void firePropertyChange(String name, Object oldValue, Object newValue)
          Fire a property change event to all listeners.
protected  Object getLock()
          Obtain lock for synchronization on manipulation with this class.
protected  Object getProperty(Object key)
          Get a shared variable.
 int hashCode()
          Get a hashcode of the shared class.
protected  void initialize()
          Initialize shared state.
protected  Object putProperty(Object key, Object value)
          Set a shared variable.
protected  Object putProperty(String key, Object value, boolean notify)
          Set a shared variable available only for string names.
 void readExternal(ObjectInput oi)
          Reads nothing from the stream.
protected  void removeNotify()
          Notify subclasses that the last listener has been removed from this action.
 void removePropertyChangeListener(PropertyChangeListener l)
           
 void writeExternal(ObjectOutput oo)
          Writes nothing to the stream.
protected  Object writeReplace()
          This method provides correct handling of serialization and deserialization.
 
Methods inherited from class java.lang.Object
clone, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SharedClassObject

protected SharedClassObject()
Create a shared object. Typically shared-class constructors should not take parameters, since there will conventionally be no instance variables.
See Also:
initialize()
Method Detail

finalize

protected final void finalize()
                       throws Throwable
Overrides:
finalize in class Object

clearSharedData

protected boolean clearSharedData()
Indicate whether the shared data of the last existing instance of this class should be cleared when that instance is finalized. Subclasses may perform additional tasks on finalization if desired. This method should be overridden in lieu of finalize().

The default implementation returns true. Classes which have precious shared data may want to return false, so that all instances may be finalized, after which new instances will pick up the same shared variables without requiring a recalculation.

Returns:
true if all shared data should be cleared, false if it should stay in memory

equals

public final boolean equals(Object obj)
Test whether the classes of the compared objects are the same.
Overrides:
equals in class Object
Parameters:
obj - the object to compare to
Returns:
true if the classes are equal

hashCode

public final int hashCode()
Get a hashcode of the shared class.
Overrides:
hashCode in class Object
Returns:
the hash code

getLock

protected final Object getLock()
Obtain lock for synchronization on manipulation with this class. Can be used by subclasses when performing nonatomic writes, e.g.
Returns:
an arbitrary synchronizable lock object

putProperty

protected final Object putProperty(Object key,
                                   Object value)
Set a shared variable. Automatically locks.
Parameters:
key - name of the property
value - value for that property (may be null)
Returns:
the previous value assigned to the property, or null if none

putProperty

protected final Object putProperty(String key,
                                   Object value,
                                   boolean notify)
Set a shared variable available only for string names. Automatically locks.
Parameters:
key - name of the property
value - value for that property (may be null)
notify - should all listeners be notified about property change?
Returns:
the previous value assigned to the property, or null if none

getProperty

protected final Object getProperty(Object key)
Get a shared variable. Automatically locks.
Parameters:
key - name of the property
Returns:
value of the property, or null if none

initialize

protected void initialize()
Initialize shared state. Should use putProperty(java.lang.Object, java.lang.Object) to set up variables. Subclasses should always call the super method.

This method need not be called explicitly; it will be called once the first time a given shared class is used (not for each instance!).


addPropertyChangeListener

public final void addPropertyChangeListener(PropertyChangeListener l)

removePropertyChangeListener

public final void removePropertyChangeListener(PropertyChangeListener l)

addNotify

protected void addNotify()
Notify subclasses that the first listener has been added to this action. The default implementation does nothing.

removeNotify

protected void removeNotify()
Notify subclasses that the last listener has been removed from this action. The default implementation does nothing.

firePropertyChange

protected void firePropertyChange(String name,
                                  Object oldValue,
                                  Object newValue)
Fire a property change event to all listeners.
Parameters:
name - the name of the property
oldValue - the old value
newValue - the new value

writeExternal

public void writeExternal(ObjectOutput oo)
                   throws IOException
Writes nothing to the stream.
Specified by:
writeExternal in interface Externalizable
Parameters:
oo - ignored

readExternal

public void readExternal(ObjectInput oi)
                  throws IOException,
                         ClassNotFoundException
Reads nothing from the stream.
Specified by:
readExternal in interface Externalizable
Parameters:
oi - ignored

writeReplace

protected Object writeReplace()
This method provides correct handling of serialization and deserialization. When serialized the method writeExternal is used to store the state. When deserialized first an instance is located by a call to findObject (clazz, true) and then a method readExternal is called to read its state from stream.

This allows to have only one instance of the class in the system and work only with it.

Returns:
write replace object that handles the described serialization/deserialization process

findObject

public static SharedClassObject findObject(Class clazz)
Obtain an instance of the desired class, if there is one.
Parameters:
clazz - the shared class to look for
Returns:
the instance, or null if such does not exists

findObject

public static SharedClassObject findObject(Class clazz,
                                           boolean create)
Find an existing object, possibly creating a new one as needed. To create a new instance the class must be public and have a public default constructor.
Parameters:
clazz - the class of the object to find (must extend SharedClassObject)
create - true if the object should be created if it does not yet exist
Returns:
an instance, or null if there was none and create was false
Throws:
IllegalArgumentException - if a new instance could not be created for some reason


Built on December 12 2001.  |  Portions Copyright 1997-2001 Sun Microsystems, Inc. All rights reserved.