public abstract class SharedClassObject extends Object implements Externalizable
The data are shared only between instances of the same class (not subclasses). Thus, such "variables" have neither instance nor static behavior.
Modifier | Constructor and Description |
---|---|
protected |
SharedClassObject()
Create a shared object.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addNotify()
Notify subclasses that the first listener has been added to this object.
|
void |
addPropertyChangeListener(PropertyChangeListener l)
Adds the specified property change listener to receive property
change events from this object.
|
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 <T extends SharedClassObject> |
findObject(Class<T> clazz)
Obtain an instance of the desired class, if there is one.
|
static <T extends SharedClassObject> |
findObject(Class<T> 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 object.
|
void |
removePropertyChangeListener(PropertyChangeListener l)
Removes the specified property change listener so that it
no longer receives property change events from this object.
|
protected void |
reset()
Is called by the infrastructure in cases when a clean instance is requested.
|
void |
writeExternal(ObjectOutput oo)
Writes nothing to the stream.
|
protected Object |
writeReplace()
This method provides correct handling of serialization and deserialization.
|
protected SharedClassObject()
SharedClassObject.initialize()
protected final void finalize() throws Throwable
protected boolean clearSharedData()
SharedClassObject.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.
true
if all shared data should be cleared,
false
if it should stay in memorypublic final boolean equals(Object obj)
public final int hashCode()
protected final Object getLock()
protected final Object putProperty(Object key, Object value)
locks
.key
- name of the propertyvalue
- value for that property (may be null)null
if noneprotected final Object putProperty(String key, Object value, boolean notify)
locks
.
Important: remember that SharedClassObject
s
are like singleton beans; when you use putProperty
with a value
of true
, or call SharedClassObject.firePropertyChange(java.lang.String, java.lang.Object, java.lang.Object)
, you must consider that
the property name should match the JavaBeans name for a natural (introspected) property
for the bean, if such a property uses this key. For example, if you have a method
getFoo
which uses SharedClassObject.getProperty(java.lang.Object)
and a method setFoo
which uses putProperty(..., true)
, then the key used must
be named foo
(assuming you did not override this name in a BeanInfo).
Otherwise various listeners may not be prepared for the property change and may just
ignore it. For example, the property sheet for a BeanNode based on a
SharedClassObject
which stores its properties using a misnamed key
will probably not refresh correctly.
key
- name of the propertyvalue
- value for that property (may be null)notify
- should all listeners be notified about property change?null
if noneprotected final Object getProperty(Object key)
locks
.key
- name of the propertynull
if noneprotected void initialize()
SharedClassObject.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!).
public final void addPropertyChangeListener(PropertyChangeListener l)
l
- the property change listenerpublic final void removePropertyChangeListener(PropertyChangeListener l)
l
- the property change listenerprotected void addNotify()
protected void removeNotify()
protected void firePropertyChange(String name, Object oldValue, Object newValue)
name
- the name of the propertyoldValue
- the old valuenewValue
- the new valuepublic void writeExternal(ObjectOutput oo) throws IOException
writeExternal
in interface Externalizable
oo
- ignoredIOException
public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException
readExternal
in interface Externalizable
oi
- ignoredIOException
ClassNotFoundException
protected Object writeReplace()
This allows to have only one instance of the class in the system and work only with it.
public static <T extends SharedClassObject> T findObject(Class<T> clazz)
clazz
- the shared class to look fornull
if such does not existspublic static <T extends SharedClassObject> T findObject(Class<T> clazz, boolean create)
clazz
- the class of the object to find (must extend SharedClassObject
)create
- true
if the object should be created if it does not yet existnull
if there was none and create
was false
IllegalArgumentException
- if a new instance could not be created for some reasonprotected void reset()
SharedClassObject
are singletons, there is
no way how to create new instance that would not contain the same data
as previously existing one. This method allows all subclasses that care
about the ability to refresh the settings (like SystemOption
s)
to be notified about the cleaning request and clean their settings themselves.
Default implementation does nothing.