2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com
.sun
.star
.uno
;
22 * This class holds weak reference to an object.
24 * <p>It actually holds a reference to a <code>com.sun.star.XAdapter</code>
25 * implementation and obtains a hard reference if necessary.
27 public class WeakReference
29 private OWeakRefListener m_listener
;
30 // There is no default constructor. Every instance must register itself with the
31 // XAdapter interface, which is done in the constructors. Assume we have this code
32 // WeakReference ref= new WeakReference();
33 // ref = someOtherWeakReference;
35 // ref would not be notified (XReference.dispose()) because it did not register
36 // itself. Therefore the XAdapter would be kept aliver although this is not
40 * Creates an instance of this class.
42 * @param obj another instance that is to be copied.
44 public WeakReference(WeakReference obj
)
49 Object weakImpl
= obj
.get();
50 if (weakImpl
== null) {
53 XWeak weak
= UnoRuntime
.queryInterface(XWeak
.class, weakImpl
);
55 XAdapter adapter
= weak
.queryAdapter();
57 m_listener
= new OWeakRefListener(adapter
);
62 * Creates an instance of this class.
64 * @param obj XWeak implementation.
66 public WeakReference(Object obj
)
68 XWeak weak
= UnoRuntime
.queryInterface(XWeak
.class, obj
);
71 XAdapter adapter
= weak
.queryAdapter();
73 m_listener
= new OWeakRefListener(adapter
);
78 * Returns a hard reference to the object that is kept weak by this class.
80 * @return a hard reference to the XWeak implementation.
84 if (m_listener
!= null)
85 return m_listener
.get();
91 * Implementation of com.sun.star.uno.XReference for use with WeakReference.
93 * <p>It keeps the XAdapter implementation and registers always with it.
94 * Deregistering occurs on notification by the adapter and the adapter is
97 class OWeakRefListener
implements XReference
99 private XAdapter m_adapter
;
102 * The constructor registered this object with adapter.
104 * @param adapter the XAdapter implementation.
106 OWeakRefListener( XAdapter adapter
)
109 m_adapter
.addReference(this);
113 * Method of <code>com.sun.star.uno.XReference</code>.
115 * <p>When called, it deregisteres this object with the adapter and releases
116 * the reference to it.</p>
118 synchronized public void dispose()
120 if (m_adapter
!= null)
122 m_adapter
.removeReference(this);
128 * Obtains a hard reference to the object which is kept weak by the adapter
131 * @return hard reference to the otherwise weakly kept object.
133 synchronized Object
get()
136 if (m_adapter
!= null)
138 retVal
= m_adapter
.queryAdapted();
141 // If this object registered as listener with XAdapter while it was notifying
142 // the listeners then this object might not have been notified. If queryAdapted
143 // returned null then the weak kept object is dead and the listeners have already
144 // been notified. And we missed it.
145 m_adapter
.removeReference(this);