/** *Thisclassholdsweakreferencetoanobject. * *<p>Itactuallyholdsareferencetoa<code>com.sun.star.XAdapter</code> *implementationandobtainsahardreferenceifnecessary.
*/ publicclass WeakReference
{ private OWeakRefListener m_listener; // There is no default constructor. Every instance must register itself with the // XAdapter interface, which is done in the constructors. Assume we have this code // WeakReference ref= new WeakReference(); // ref = someOtherWeakReference; // // ref would not be notified (XReference.dispose()) because it did not register // itself. Therefore the XAdapter would be kept alive although this is not // necessary.
/** *Createsaninstanceofthisclass. * *@paramobjanotherinstancethatistobecopied.
*/ public WeakReference(WeakReference obj)
{ if (obj == null) { return;
}
Object weakImpl = obj.get(); if (weakImpl == null) { return;
}
XWeak weak = UnoRuntime.queryInterface(XWeak.class, weakImpl); if (weak != null) {
XAdapter adapter = weak.queryAdapter(); if (adapter != null)
m_listener = new OWeakRefListener(adapter);
}
}
/** *Createsaninstanceofthisclass. * *@paramobjXWeakimplementation.
*/ public WeakReference(Object obj)
{
XWeak weak= UnoRuntime.queryInterface(XWeak.class, obj); if (weak != null)
{
XAdapter adapter= weak.queryAdapter(); if (adapter != null)
m_listener= new OWeakRefListener(adapter);
}
}
/** *Returnsahardreferencetotheobjectthatiskeptweakbythisclass. * *@returnahardreferencetotheXWeakimplementation.
*/ public Object get()
{ if (m_listener != null) return m_listener.get(); returnnull;
}
}
/** *Obtainsahardreferencetotheobjectwhichiskeptweakbytheadapter *andreturnsit. * *@returnhardreferencetotheotherwiseweaklykeptobject.
*/ synchronized Object get()
{
Object retVal= null; if (m_adapter != null)
{
retVal= m_adapter.queryAdapted(); if (retVal == null)
{ // If this object registered as listener with XAdapter while it was notifying // the listeners then this object might not have been notified. If queryAdapted // returned null then the weak kept object is dead and the listeners have already // been notified. And we missed it.
m_adapter.removeReference(this);
m_adapter= null;
}
} return retVal;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.